Monday, May 16, 2011

i10n and L8n in Android

One of the strongest features of open source software is its global reachability. But sometimes language and local convention might be a hindrance. That's where Internationalization (i10n) and Localization (L8n) come in. The combination of both - known as Globalization - is a process which allows software to be molded according to local languages and convention without making any changes to the codebase, for example, changing the language from English to French or changing the currency symbol from the dollar to the pound.
Android by default allows the programmer to decouple the code and the non-code assets such as text, images and formatting. These "resources" are present under the res/ directory [1]. The default text for example is present inside res/values/strings.xml. For instance, if your default language is English and you want to allow your application's text to localize to French when the device's language is changed to French, the application would need to have an alternative locale-specific resource directory, res/values-fr/strings.xml in this case [2].
Let's now consider an example. We have an application interface which contains a button which should have the label "Send" in English and "Envoyer" in French. We first add the following line to res/values/strings.xml:

<string name="string_send">Send</string>

And then add

<string name="string_send">Envoyer</string>

to res/values-fr/strings.xml. Inside the layout XML for this button's interface, within the button's definition tag (<button></button>), we add android:text="@string/string_send". Essentially, what we have done is that by way of indirection, we have pointed the Android runtime to access our string resource using @string. To access the same string programmatically in Java we would use getString(R.string.string_send). Our button has now been localized to French. The same strategy can also be used to localize graphics, i.e. by providing graphics in both res/drawable/ and res/drawable-fr/. The two-letter ISO 639-1 code for other languages can be found at [3].

An extra tip: If you want to post XML code in your blog post for example, encode it using [4] instead of posting it directly because otherwise it will be treated as actionable HTML.

No comments:

Post a Comment