简体   繁体   English

如何在Codeigniter中更改站点语言

[英]How to change the site language in codeigniter

I have developed website in codeigniter and now i want it to be multi-language. 我已经在codeigniter中开发了网站,现在我希望它是多语言的。

I have followed a tutorial working with hooks. 我遵循了使用钩子的教程。 Here is my hook.php 这是我的hook.php

$hook['post_controller_constructor']=array(
                                   'class'=>'LanguageLoader',
                                   'function'=>'initialize',
                                   'filename'=>'LanguageLoader.php',
                                   'filepath'=>'hooks'
                                   );

and I have created a class and placed it inside the hooks folder. 并且我创建了一个类并将其放在hooks文件夹中。

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

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

and here is another file which is also placed inside the hooks folder 这是另一个文件,它也放置在hooks文件夹中

class LanguageLoader
{
    function initialize()
    {
        $CI=&get_instance();
        $CI->load->helper('language');
        $site_lang=$CI->session->userdata('site_lang');
        if($site_lang)
        {
            $CI->lang->load('dari','dari');
        }
        else
        {
            $CI->lang->load('english','english');
        }
    }
}

and here is my view file. 这是我的查看文件。

<a href='<?=site_url('LangSwitcher/switchLanguage/english')?>'>English</a>

and it says that "The requested page was not found ". 它说“找不到所请求的页面”。 Can any body find it what is happening ? 任何人都能找到它发生了什么吗?

Your controller is called LangSwitch but in your url, you generate "LangSwitch er " in site_url(...) . 您的控制器称为LangSwitch但在您的url中,您在site_url(...)生成“ LangSwitch er ”。 If you don't have routes set up for this then it's probably a typo there. 如果您没有为此设置路线,则可能是拼写错误。

Also, the if($site_lang) condition looks wrong in LanguageLoader . 另外,在LanguageLoaderif($site_lang)条件看起来错误。 Maybe you wanted if ($site_lang == "dari") and elseif s to other supported languages or a switch() 也许您想要if ($site_lang == "dari")elseif到其他支持的语言或switch()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM