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?
-
Check the answer in this post http://stackoverflow.com/questions/31895341/codeigniter-dynamic-language-functionality/41661355#41661355 – Nasz Njoka Sr. Jan 15 '17 at 13:12
5 Answers
an easier way is to reset the language data and is_loaded
$this->lang->is_loaded = array();
$this->lang->language = array();

- 718
- 7
- 9
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..

- 1,100
- 2
- 20
- 31
-
Works for me :D ... This should be the accepted answer, I believe, coz it's the easiest solution out of all the answers given. – Robin Hood Apr 28 '16 at 19:26
-
That is the way to do it, and it is never to late for the right solution! – Dejan Dozet Oct 13 '18 at 14:36
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
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!)
Reference here per forum posts: http://codeigniter.com/forums/viewthread/176223/

- 20,418
- 8
- 65
- 92
-
yes, i have loaded the default language (English) inside MY_Controller which is extended by other controllers. I tried loading a secondary (Chinese) language hoping it will override the English language inside this ABC controller which also extends MY_Controller, but still, only the English language is accessible. – coder Sep 27 '11 at 02:46
-
I have updated my answer, looks like you need to fix your controllers, cannot declare 2 languages in 1 controller (or a controller you extend). – Jakub Sep 27 '11 at 03:26
-
Cool thx. I figured i just need to implement another optional param to my MY_Controller constructor to determine which language to load. That should work for my case. Thx for the effort!!! – coder Sep 27 '11 at 03:49
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.

- 1,597
- 1
- 9
- 7
-
This is the only working solution when working with language files that have the same keys – Pepijn Olivier Feb 13 '14 at 11:12
-
If you have any application installed built in codeigniter
and you want add a language pack, just follow these steps:
- 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:
<div class="controls">
<?php /*
$lang = array (
'english' => 'English',
'arabic' => 'Arabic', // +++ Add this line
'spanish' => 'Español'
Now save and run the application.

- 5,993
- 2
- 18
- 28

- 11
- 1