Codeigniter multilanguage route

发布于 2022-08-25 07:40:39 字数 331 浏览 0 评论 1

Hi all is there anyone can help me routing my app urls according to $_SESSION 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

划一舟意中人 2022-08-25 14:54:16 1 楼

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击“接受”或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。