0

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!

1
  • not possible. $.get and 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. Commented Jul 5, 2016 at 16:33

1 Answer 1

0

Download Web Server for Chrome App from here. This is not exactly a server, but a handy simulation of the same that allows you to run your files locally as if they are running on a server.

It works without any Internet connection. More importantly, it has configuration options for CORS request thanks to recent updates Install it, select the folder in which your files are present, and you are ready! It's a really good way to test your code locally on Chrome.

Sign up to request clarification or add additional context in comments.

2 Comments

Did not work yet, I will try at at the customers computer
Works fine for me, let me know where are you getting stuck .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.