Jump to content

In-game texts and localization or preloading text possible?


lukaMis
 Share

Recommended Posts

You can load JSON files just by doing game.load.json("gametext", "/path/to/gametext.json") and then retrieve it later with game.cache.getJSON("gametext") which will give you an object representing the JSON you loaded. From there you can built your own localisation routine; maybe a wrapper function which returns the specified language version of a string:

// json file contents{  "hello": {    "en": "Hello",    "fr": "Bonjour",    "sk": "Ahojte"  }}

Translate function (bare bones, would need to be smarter than this for real use):

function localise(str, lang) {  lang = lang || "en"; // default language if none specified  // get our localisation file contents as a JS object  var loc = game.cache.getJSON("gametext");  // return the specified string in the specified language dynamically using array notation  return loc[str][lang];}console.log(localise("hello") + " Luka!"); // "Hello Luka!";console.log(localise("hello", "fr") + " Luka!"); // "Bonjour Luka!";console.log(localise("hello", "sk") + " Luka!"); // "Ahojte Luka!";
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...