Tuesday, February 07, 2012

Codeigniter: Language lines into javascript


I’ve extend language_helper and I’ve added a langJs function.

For use it you only call it with an array including lines for pass to javascript.

Example

In the controller
$this->load->helper('language');
$this->lang->load('calendar', 'spanish');

In the view
[php]

echo langJs(array('cal_wednesday', 'cal_january'));

[javascript]
alert(_msg.cal_wednesday);

Load all lines
If you need load all lines, you can do something like this:

In the Controller

$this->load->helper('language');
$this->lang->load('calendar', 'spanish');
$data['lang_keys'] = array_keys($this->lang->language);

In the View
echo langJs($lang_keys);

As you can see, is very easy passing messages to javascript.
Download Language Helper

No comments: