开发者

Codeigniter multilanguage route

Hi all is there anyone can help me routing my app urls according to $_S开发者_运维知识库ESSION param?

which is the best way to switch lang routes?

what about use many route files? (i mean route_ch.php,route_en.php,route_sp.php,)

is there any link to understand how to route according to site lang?

thanks guys


You need to extend your view Controller with a session check of selected language. Then you can redirect() the user accordingly.

create a controller i.e LangSwitch.php

<?php
class LangSwitch extends CI_Controller
{
    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
    }

    function switchLanguage($language = "") {
        $language = ($language != "") ? $language : "en";
        $this->session->set_userdata('site_lang', $language);
        redirect(base_url());
    }
}

Links to switch each of the available languages:

<a href='<?php echo $base_url; ?>langswitch/switchLanguage/en'>English</a>

<a href='<?php echo $base_url; ?>langswitch/switchLanguage/fr'>French</a>

Defines/create a hook class[LanguageLoader.php] under application/hooks directory with the necessary information to execute it.

<?php
class LanguageLoader
{
    function initialize() {
        $ci =& get_instance();
        $ci->load->helper('language');

        $site_lang = $ci->session->userdata('site_lang');
        if ($site_lang) {
            $ci->lang->load('message',$ci->session->userdata('site_lang'));
        } else {
            $ci->lang->load('message','en');
        }
    }
}

This is just a running idea that worked for me well. I hope it is useful to you too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜