Worldmaker

Localization

I usually just hardcode the text on buttons, menus, and text fields. This is fast and for most of my purposes definitely good enough, but makes any app really hard to translate into different languages.

In my professional career as a software developer, I've come across a couple different methods of implementing localization and wanted to try implementing a simple form of it for my Worldmaker app. I decided on three objectives.

  1. Accessing strings needs to be simple. LanguageManager.getText("foo", [], 0, false) is ugly and confusing. Much easier: Local.text("foo")
  2. It has to be in JSON. No XML, no custom arrangement, just simple JSON that can be parsed in. I also happen to know the popular localization web platform POEditor lets you import JSON which means outsourcing newly added languages would be a breeze.
  3. Adding new dictionaries and language entries should be straightforward. If I want to localize to a new language, I should be able to easily scale up my dictionaries to meet that need.

First, I created a class called Local.as which stores all the logic needed for the dictionaries and language-related string manipulation.

Let's break down what the class does. Local.as contains references to the JSON dictionaries, a property to keep track of the selected language, and the current dictionary of keys and terms.

Apart from that, a function set language(id:String) sets the selected language and loads the appropriate dictionary, while the static function get text(id:String, args:Array = null):String allows other classes to easily access the current dictionary's key entries. If an unknown key is accessed, a trace lets me know exactly what key is missing so I can enter it into the dictionary.

I implemented a little popup to change the language - finally the options button on the navigation bar has some use! I added pig-latin after a suggestion from a friend for testing purposes; it's easier to produce pig-latin on the fly than German or French and it looks better than lorem ipsum.

The highly awaited options popup
Left: English, Right: Pig-latin

I'm very happy with how it turned out. One thing that I want to address in the future is the ability to change the language without having to reload the app.