Welcome Guest, Not a member yet? Register   Sign In
Handling Language Internalization in javascript files
#1

[eluser]Trankh1[/eluser]
Hello,

I'm using Language class features to handle internalization of my application.
It is super convenient for handling that in PHP but now I want to handle it in my javascript files.

So far, i'm doing the stuff like this:

in a php file :

Code:
// opened js tag

   var caption1 = '<?=lang('caption1') ?>';
   var caption2 = '<?=lang('caption2') ?>';

// closed js tag

[removed]

[removed]

But i'm not happy with this way.

Have you guys a solution to avoid this?

I want to load all my language file (general_lang) in one shot.
I want to avoid that my source code displays all these assignations.
I see that carabiner library makes the stuff for js files (but so far, my file is .php)


Thank you very much for your time.

#2

[eluser]porquero[/eluser]
Hi,

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:
Code:
$this->load->helper('language');
$this->lang->load('calendar', 'spanish');

In the view:
[php]
Code:
echo langJs(array('cal_wednesday', 'cal_january'));

[javascript]
Code:
alert(_msg.cal_wednesday);

You can see that is very easy to pass messages to javascript.

I hope that help you.
#3

[eluser]Trankh1[/eluser]
Thanks for your contribution.
But, it's not really the point because you still need to say which keys you want to load at least of just saying the name file you wanna load.
#4

[eluser]porquero[/eluser]
Load entire file into javascript is possible, but is not very optimum. If you load all names, the page size will be unnecessarily weighed.
So if you want load all names you can do it:

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

View
Code:
echo langJs($lang_keys);
#5

[eluser]Trankh1[/eluser]
Ok, thank you very much.
It is the best solution I think.




Theme © iAndrew 2016 - Forum software by © MyBB