带有CI路由或mod_rewrite的i18n URI

时间:2012-08-31 09:30:05

标签: .htaccess codeigniter url-routing

我尝试通过将包含语言名称的段更改为双字符语言代码来清理多语言CI网站上的URI。

目前,我的URI看起来像这样:

http://example.com               //  Home (English)
http://example.com/english/home  //  Home (English)
http://example.com/home          //  404 (should go to english/home)

http://example.com/sv            //  404 (should go to swedish/home)
http://example.com/swedish/home  //  Home (Swedish)
http://example.com/sv/home       //  404 (should go to swedish/home)

我已经使用application/config/routes.php.htaccess进行过实验,但我觉得我没有取得多大进展。我应该使用哪个?我怎样才能达到预期的效果?

目前,我的文件看起来像这样:

// .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example/index.php/$1 [L]

// application/config/routes.php (minus docs)
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "pages/view/home";
$route['([a-z]+)/([a-z]+)'] = "pages/view/$2";
?>

// application/controllers/page
<?php

class Pages extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->language = $this->uri->segment(1);
    }

    public function view ($page = 'home') {

        if (!file_exists('application/views/pages/'.$page.'.php'))
            show_404();

        $data['title'] = $page;

        $this->lang->load('general',$this->language);
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);

    }
}
?>

3 个答案:

答案 0 :(得分:2)

在routes.php中试试这个:

$route['default_controller'] = "pages/view/home";
$route['(en|sv)'] = 'pages/view/home';
$route['(en|sv)/([a-z]+)'] = 'pages/view/$2';

控制器构造函数:

function __construct(){
    parent::__construct();
    $this->lang_array = array('en' => 'english', 'sv' => 'swedish');
    $this->current_lang = $this->uri->segment(1, 'en');
}

查看功能:

public function view ($page = 'home') {
            $lang_folder = $this->lang_array[$this->current_lang];
            // $lang_folder will be 'english' by default
            // other stuff
            $this->lang->load('general', $lang_folder);

}

答案 1 :(得分:0)

高于您当前的规则,请添加以下内容:

# You already had this one
RewriteEngine On

RewriteRule ^home(.*)$ /english/home$1 [L]
RewriteRule ^sv(/home)?(.*)$ /swedish/home$1 [L]

然后你已经拥有的那些:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /kappahl-hilys/index.php/$1 [L]

答案 2 :(得分:0)

您还可以:
在第一个Hook(pre_system)文件中,检测URI中的语言:

    <?php

   class Lang_uri {

 public function run() {

    $languages = array('fr', 'en');

    //try to find language in $_SERVER['REQUEST_URI']
    foreach($languages as $lang)
    {
        if(strpos($_SERVER['REQUEST_URI'], '/'.$lang)===0)
        {
            //Store language founded
            define('URI_LANG', $lang);

            //Delete it, codeigniter will don't know is exists !
            $_SERVER['REQUEST_URI'] = substr_replace($_SERVER['REQUEST_URI'], '', strpos($_SERVER['REQUEST_URI'], '/'.$lang)+1, strlen($lang)+1);

            break;
        }
    }

    if(!defined('URI_LANG'))
        define('URI_LANG', 'fr');
}
    }

现在在另一个钩子文件(pre_controller)中设置语言配置项         

    class Set_language {

        public function set() { 
             global $CFG;

             $CFG->set_item('language', str_replace(array('en', 'fr'), array('english', 'french'), URI_LANG));
        }

    }

就是这样 http://site.com/fr/homehttp://site.com/home的工作方式相同。
有关CI Hook的更多信息:http://codeigniter.com/user_guide/general/hooks.html 抱歉我的英语很差..