Internationalization in CakePHP JavaScript files

cakephp, internationalization, php

Solution

You can also do it using JavaScript translation files with this format:

lang = {
    no: "No",
    yes: "Ja",
    agreed: "Akkoord"
}

One file per language, for example: lang.nl.js, lang.es.js, lang.en.js...

Then, you can check the current language and, depending on it, load one or another file:

if($this->Session->read('Config.language') == 'es'){
    $this->Html->script('lang.es', array('inline' => false));
}else{ 
    $this->Html->script('lang.en', array('inline' => false));
}

And inside your javascripts, instead of using something like this:

alert("Yes");

You should use this:

alert(lang.yes);

And that's it :)

Problem

I'm wrapping up a project using Cakes internationalization features to allow our application to be translated into different languages. That's worked great. A problem I've noticed though is there are a few places where text is added via JavaScript and this text does not currently come from the server at all. It's for things like dialogue boxes and a few pieces of text that change based on a users selection. How have you handled this in your own applications? How would you handle this? Is there a library or component that handles this. What about any jQuery libraries?

Original source