Pass JSON from php to javascript -


i want localize webapp. since localization through javascript not recommended thought using php alternative.

so php read messages.json file stores localization data.

$json = file_get_contents("_locales/en/messages.json"); 

in header of webapp generate javascript php according user's browser language.

echo "var localeobj = " . $json . ";"; 

so var holds data messages.json file looks that

{     "exttitle": {         "message": "test1"     },     "extname":{         "message": "test2"     } } 

now want able access each item json like

var title = getitem("exttitle"); 

and returns test1. idea how that?

i not familar json if alert localeobj gives me [object object].

var getitem = function(item) {    return localobj[item].message; }; 

you encapsulate i18n strings too...

(function() {     var localobj = { ... };     window.getitem = function(item) {        return localobj[item].message;    };  })(); 

this way, no other variables can possibly clobber localobj.


Comments

Popular posts from this blog

Javascript line number mapping -

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -