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
Post a Comment