Question

I'm building a WebWorks version of an Android app that's localised into 39 languages.

At the moment all the localisations are in xml files of key-value pairs, one file per language . Each language file has about 400 lines (roughly 40k per file).

Users can change the language used in app.

What options are there in WebWorks to solve this kind of situation?

I'd be more than happy to convert the resource files into any other kind of format to make working with it a better experience on the platform.

Was it helpful?

Solution

You could store each language set in a JavaScript file that you include/load as needed. (I've converted the XML data to a "Map" since it is just key/value pairs)

e.g. (just ignore my translations... I just Googled this, I'm by no means fluent in Spanish)

//Spanish File "lang_spanish.js"
var translations = {
  "lose_a_turn":     "pierde un turno",
  "do_not_pass_go":  "huele como un camello",
  "take_a_card":     "tener una tarjeta de",
  "you_win_the_game":"sin motocicletas en la biblioteca",
  "you_can_not_move":"desbordamiento de la pila puede ser un lugar divertido"
};

In your <head> you can then have a generic script tag, that you just change the source of as needed.

e.g.

<script id="langFile" src="js/lang_english.js"></script>

When you want a different language, just remove this script from the DOM and add your new one. e.g.

function switchLang(langName){
    var headTag = document.getElementsByTagName('head')[0];
    var scriptFile = document.getElementById('langFile');
    headTag.removeChild(scriptFile);
    var newScript = document.createElement('script');
      newScript.id = 'langFile';
      newScript.src = 'js/lang_' + langName + '.js';
    headTag.appendChild(newScript);
}
//call with:
switchLang('spanish');

The alternative would be to load all 39 languages by default... but that seems like overkill considering most will only ever want 1 or 2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top