Welcome Guest, Not a member yet? Register   Sign In
using loaded language file inside controller, possible?

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

in the language file I'm loading I have an entry:
$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):
$config['next_link'] = 'Next'

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

Does anyone have a solution for this?

[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.

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.

$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.

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

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.

Theme © iAndrew 2016 - Forum software by © MyBB