i18n URIs with CI Routing or mod_rewrite

笑着哭i 提交于 2019-12-25 01:26:59

问题


I'm trying to clean up my URIs on a multi-language CI site by changing the segment containing the language name to just the two-character language code.

Currently, my URIs look like this:

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)

I have experimented both with application/config/routes.php and .htaccess, but I feel I'm not making much progress. Which should I be using? How can I achieve my desired results?

As it stands, my files look like this:

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

    }
}
?>

回答1:


Try this in routes.php:

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

Controller constructor:

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

View function:

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

}



回答2:


Above your current rules, add these:

# You already had this one
RewriteEngine On

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

Then the ones you already have:

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



回答3:


You can also:
In a first Hook(pre_system) file, detect the language in 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');
}
    }

Now in an another hook file(pre_controller) set language config item

    class Set_language {

        public function set() { 
             global $CFG;

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

    }

That's all.
http://site.com/fr/home and http://site.com/home will work as same..
For more infos about CI Hook: http://codeigniter.com/user_guide/general/hooks.html
Sorry for my poor english..



来源:https://stackoverflow.com/questions/12212580/i18n-uris-with-ci-routing-or-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!