I want to localize my webapp. Since localization through javascript is not recommended, I thought using php would be an alternative.
So, with php, I read the messages.json file, which stores all the localization data.
$json = file_get_contents("_locales/en/messages.json");
In the header of my webapp, I generate javascript with php according to the user's browser language.
echo "var localeObj = " . $json . ";";
So this is just a var that contains all the data from the m essages.json file, which looks like this:
{ "extTitle": { "message": "Test1" }, "extName":{ "message": "Test2" } }
Now I want to have access to every element from json, like
var title = getItem("extTitle");
and it returns Test1 . Any idea how to do this?
I am not very familiar with json, but if I just warn localeObj , it gives me just [object Object].
source share