CodeIgniter Forums
using loaded language file inside controller, possible? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: using loaded language file inside controller, possible? (/showthread.php?tid=2504)



using loaded language file inside controller, possible? - El Forum - 08-09-2007

[eluser]moonbeetle[/eluser]
In my constructor I load the language file according to the user's preference like this:
Code:
// Load language file
$this->lang->load('products', $this->db_session->userdata('lang'));

in the language file I'm loading I have an entry:
Code:
$lang['next_link'] = 'Volgende';
("Volgende" means "Next" in Dutch)

This works fine and when I load a view file I can use data from my loaded language file. So the controller is perfectly capable of loading the language file and passing the data to the view file. However, what I can't do or can't figure out is how to use the loaded language data directly inside the controller. Why would I want to do this? For setting the config values for pagination where language dependancy is needed for instance.

This works (obviously):
Code:
$config['next_link'] = 'Next'

But this doesn't:
Code:
$config['next_link'] = $this->lang->line('next_link');
nor will this:
Code:
$config['next_link'] = $lang['next_link'];

Does anyone have a solution for this?


using loaded language file inside controller, possible? - El Forum - 08-09-2007

[eluser]Phil Sturgeon[/eluser]
Move the language load from constructor into the methods, I had some trouble with something like this before. Theres no obvious error with your code. If that does nothing try hardcoding the 2nd param of load to 'english' or something, see if that helps.


using loaded language file inside controller, possible? - El Forum - 08-09-2007

[eluser]esra[/eluser]
You can assign the language string to a variable in your constructor or index() method using the language libraries line method. For example, the 'articles_title' string is assigned to the $title variable.

Code:
$title = $this->lang->line('articles_title');

Then use the variable where needed in your controller or view. Note that the constructor method code would be inheirited by all subclassed controllers. The index() method handles local construction for the current controller and would not be inheirited by subclassed controllers.

For some better examples, you might download BambooInvoice from Derek Allard's bamboinvoice site to take a look how language strings are handled in FreakAuth.


using loaded language file inside controller, possible? - El Forum - 08-09-2007

[eluser]Derek Allard[/eluser]
You can autoload language files as of 1.5.4 also.


using loaded language file inside controller, possible? - El Forum - 08-09-2007

[eluser]moonbeetle[/eluser]
Thanks all for replying. The problem I experienced was due to the fact that I also loaded a model in my controller and that this model also had a property $this->lang (for dynamic queries on tables that contain translations or language dependent data). After renaming that to $this->language my problem was solved. Tok me a while to figure out what was wrong.