Search code examples
phpcodeignitermultilingual

What is the simplest way to load language keys to a view in Codeigniter?


I'm using codeigniter and I'm building a multilingual site, so I use Language class like this:

$this->lang->load('index', 'english');

Then, in the controller, to load all the data into my view I do this:

$data["var1"] = $this->lang->line('language_key1');
$data["var2"] = $this->lang->line('language_key2');
$data["var3"] = $this->lang->line('language_key3');
$data["var4"] = $this->lang->line('language_key4');
$data["var5"] = $this->lang->line('language_key5');
...
$this->load->view('index', $data);

The problem is that in some controllers I have to load more than 100 language keys, and I wonder if there is a simpler way to do this.


Solution

  • There is a simple way to do this instead of language class you can use language helper.

    Here is you can see the documentation

    In you view you can use this

    <p><?php echo lang('language_key1')?></p>
    

    Also if you want to use language class you can use it like this

    $data['language'] = $this->lang->load('index', 'english',true);
    

    Passing third parameter as true will return the array of language items. Then in the view you can use like this

    echo $language['language_key1'];