0

我试图通过将包含语言名称的段更改为仅两个字符的语言代码来清理多语言 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);

    }
}
?>
4

3 回答 3

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);

}
于 2012-08-31T10:25:47.900 回答
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]
于 2012-08-31T09:48:42.853 回答
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
抱歉对于我糟糕的英语..

于 2012-09-03T06:22:05.647 回答