使用CodeIgniter动态设置语言 [英] Dynamically set language with CodeIgniter
问题描述
我想将默认的应用程序语言设置为我的访客国家/地区语言。我有一个API来获取IP前缀惯用语,并根据此信息设置语言。
I want to set the default application language as my visitor countries language. I have an API to get the IP prefix idiom and based in this information, I want to set the language.
执行此操作的最佳方法是什么?我想制作一个cookie来存储语言标识符,但是我必须在哪里设置呢?
What is the best way to do this? I want to make a cookie to store the language identifier, but where do I have to set this?
谢谢。
推荐答案
您是否看到 CodeIgniter的语言库?
语言类提供了检索语言文件和文本行以实现国际化的功能。
在您的CodeIgniter系统文件夹中,您会找到一种包含多种语言文件的称为语言的语言。您可以根据需要创建自己的语言文件,以显示其他语言的错误和其他消息。
语言文件通常存储在您的系统/语言目录。或者,您可以在应用程序文件夹中创建一个名为language的文件夹,并将其存储在该文件夹中。 CodeIgniter将首先出现在您的application / language目录中。如果目录不存在或找不到指定的语言,则CI会在您的全局system / language文件夹中查找。
在您的情况下。
- 您需要创建
polish_lang.php
和english_lang.php
在application / language / polish内 - 然后在该文件内创建密钥(例如
$ lang ['hello'] = Witaj
; - 然后将其加载到控制器中,如
$ this-> lang-> load('polish_lang' ,'polish');
- 然后获取
$ this-> lang-> line('hello')之类的行;
只需将此函数的返回值存储在变量中,即可在视图中使用它。
- you need to create a
polish_lang.php
andenglish_lang.php
inside application/language/polish - then create your keys inside that file (e.g.
$lang['hello'] = "Witaj"
; - then load it in your controller like
$this->lang->load('polish_lang', 'polish');
- then fetch the line like
$this->lang->line('hello');
Just store the return value of this function in a variable so you can use it in your view.
为英语和您需要的所有其他语言重复步骤。
Repeat the steps for the english language and all other languages you need.
这篇关于使用CodeIgniter动态设置语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!