doukanxi4246 2012-04-17 12:14
浏览 5
已采纳

CodeIgniter语言选择

I want the user to select his language from any sub page on my website. The best solution, to store each user language are sessions.

My website default language is English, but how should I load other language that user select? I guess I should do it like this:

$this->lang->load('my_language_file', $this->session->userdata('language'));

But this way, I'd have to load the language in every controller. Isn't there a way, to select user language from session globally? So I could use $this->lang->line('some-message'); in every place on my website?

  • 写回答

1条回答 默认 最新

  • douzoudang1511 2012-04-17 12:33
    关注

    Either do a

    $CI =& get_instance();
    $CI->lang->load('my_language_file', $CI->session->userdata('language'));
    

    in the beginning of a custom helper and have it autoloaded in your config/autoload.php.

    Else you can do it through hooks.
    Basicly use

    $CI =& get_instance();
    $CI->lang->load('my_language_file', $CI->session->userdata('language'));
    

    here aswell inside the __construct(), and have the Hook Point post_controller_constructor.

    This will make you include the lang file in all controllers. However it's up to you how much lang-text you want to load automaticly, since it exhausts the server even if only to a small extent.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序