On-the-fly Localization

August 18th, 2008 by Rob

So, what if you want your users to be able to change locales on-the-fly? I’ve put together a simple component that loads and manages translations via XML.

Here’s how it works…

Somewhere in your code, you must set up your languages:

import com.robskelly.text.LocalTextField;

LocalTextField.addLanguage('en_us','lang/en_us.xml');
LocalTextField.addLanguage('es','lang/es.xml');

Now you can set a language:

LocalTextField.setLanguage('en_us');

LocalTextField won’t load the language file until you call setLanguage.

LocalTextField itself is a component that wraps a flash.text.TextField instance. You can drag an instance of LocalTextField onto the stage, set its ID parameter, and off you go!

The language files are XML and look like this:

<strings>
	<string id="1">This is a string.</string>
	<string id="2">This is also a string.</string>
	<string id="3" html="true">This is a string with <b>HTML<b /></string>
</strings>

Download a sample here.