tl/dr: How can I
- internationalize strings in a html5/javascript application
- while using a json file or something similar with key/value pairs (easy to translate)
- without using javascript vars for every language string (ugly)
- and if possible, without complex frameworks or packages
- on Chrome (or something with same-origin-policy)
- without a (local) webserver
- without internet connection
Details:
I am developing a html5 touch game for older useres on an embedded IE system that will be changed to an embedded chrome system soon. Using a webserver is currently no option and I can't assume I have an internet connection all the time. Since the application should be in different languages, I currently have a json file that is accessed like this (irrelevant stuff left out):
//...
var language = "en"; //the language we want, same as the json file name
var key = "key"; //the key to the value we like to obtain
var languageMap;
var langFile = $.getJSON(language + ".json", function(data){
languageMap = data;
});
var langFileStatus = $.when(langFile);
langFileStatus.done(function () {
var value = languageMap[key];
//use the value of "key" here for awsome stuff
});
//...
the language file (e.g. "en.json") looks like this:
{
"key":"value",
"otherKey":"otherValue",
}
which works pretty well for IE and FF, but not on Chrome, because of the same-origin-policy. I read about an awsome trick to bypass that here, but I couldn't make it work in this case. I have never used JSON before in connection with JS, so maybe its an easy question. Different solutions for the whole problem are also very welcome (thats why I posted the complete problem). Thanks in advance!
$.getand related functions perform ajax requests, which means they're doing http requests, which means you need a webserver to handle those http requests. Standard client JS has no way to access the file system or load files. you COULD have a<script type="text/json" src="somefile.json">and then use DOM operations to access the content of that script block.