php Codeigniter 更改加载的语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7563390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter Change loaded language
提问by coder
Currently i have a language loaded inside MY_Controller which extends CI_Controller. But inside a special page which controller (let's call it ABC controller) extends MY_Controller, I need to override the loaded language with another language. I tried loading another language inside this ABC controller, but unsuccessful. Is there a way to unload the loaded language and load another language?
目前我在 MY_Controller 中加载了一种语言,它扩展了 CI_Controller。但是在控制器(我们称之为 ABC 控制器)扩展 MY_Controller 的特殊页面中,我需要用另一种语言覆盖加载的语言。我尝试在这个 ABC 控制器中加载另一种语言,但没有成功。有没有办法卸载加载的语言并加载另一种语言?
采纳答案by Jakub
Have you tried just loading the language file you need?
你试过只加载你需要的语言文件吗?
$this->lang->load('filename', 'language');
It should be then accessible just like your default language. I haven't tested this tho, but from my understanding this should be the way to go about it.
然后它应该可以像您的默认语言一样访问。我还没有测试过这个,但根据我的理解,这应该是解决问题的方法。
Reference: http://codeigniter.com/user_guide/libraries/language.html
参考:http: //codeigniter.com/user_guide/libraries/language.html
REVISED
修改
I ended up digging a bit more for you, and found that you CANNOT load a default language (define it as default in your controller) and then later try to change it to something else.
我最终为您挖掘了更多内容,发现您无法加载默认语言(在控制器中将其定义为默认语言),然后尝试将其更改为其他语言。
Follow these steps:
按着这些次序:
- If you need a language OTHER than english (default), set that in your config.
- If you want to load ANOTHER language on a controller basis, you need to define that (most commonly in your
constructor
using something like session array / user selection. - You cannot load 2 languages (1 in the constructor, then another in a different class.. won't work!)
- 如果您需要英语以外的语言(默认),请在您的配置中进行设置。
- 如果你想在控制器的基础上加载另一种语言,你需要定义它(最常见的是在你
constructor
使用诸如会话数组/用户选择之类的东西。 - 你不能加载 2 种语言(1 种在构造函数中,然后另一种在不同的类中......不会工作!)
Reference here per forum posts: http://codeigniter.com/forums/viewthread/176223/
每个论坛帖子在此处参考:http: //codeigniter.com/forums/viewthread/176223/
回答by Damian Dennis
an easier way is to reset the language data and is_loaded
更简单的方法是重置语言数据和 is_loaded
$this->lang->is_loaded = array();
$this->lang->language = array();
回答by Anupam
I know it's a bit late to answer this but I think you can change the config item 'language' dynamically based on page requirement.
我知道回答这个问题有点晚了,但我认为您可以根据页面要求动态更改配置项“语言”。
$this->config->set_item('language', 'chinese');
$this->config->set_item('language', 'english'); // based on the language folder of course holding language files
I had a requirement to send newsletters in users base lang, and this helped me change the language on the fly, hope this might help..
我需要在用户群 lang 中发送时事通讯,这帮助我即时更改语言,希望这可能会有所帮助..
回答by jeffsama
I encounter this problem and find a tricky solution.
我遇到了这个问题并找到了一个棘手的解决方案。
$this->lang->load('text', 'english');
echo $this->lang->line('__YOUR_LANG_VARIABLE__');
//CI will record your lang file is loaded, unset it and then you will able to load another
//unset the lang file to allow the loading of another file
if(isset($this->lang->is_loaded)){
for($i=0; $i<=sizeof($this->lang->is_loaded); $i++){
unset($this->lang->is_loaded[$i]);
}
}
$this->lang->load('text', 'chinese');
echo $this->lang->line('__YOUR_LANG_VARIABLE__');
Hope it helps.
希望能帮助到你。
回答by snakehead
If you have any application installed built in codeigniter
and you want add a language pack, just follow these steps:
如果您安装了任何内置应用程序codeigniter
并且想要添加语言包,只需按照以下步骤操作:
- Add language files in folder
application/language/arabic
(I added arabic lang in sma2 built in ci)
- Go to the file named
setting.php
In
application/modules/settings/views/setting.php
you will find the array:
- 在文件夹中添加语言文件
application/language/arabic
(我在 ci 内置的 sma2 中添加了阿拉伯语 lang)
- 转到名为的文件
setting.php
在
application/modules/settings/views/setting.php
你会发现数组:
<div class="controls">
<?php /*
$lang = array (
'english' => 'English',
'arabic' => 'Arabic', // +++ Add this line
'spanish' => 'Espa?ol'
Now save and run the application.
现在保存并运行应用程序。