Android localization example
To build multilingual Android apps we need to put bit effort in translating the content of the App and putting them in the appropriate folders.
Once we provide the translation, Android OS will choose the resources that match user’s locale (English/Hindi/Tamil/Korean). If the application is available in several languages, Android will select the language that the device uses.
In this tutorial, we’ll cover:
- Application localization process
- Translating strings
- Localizing images
- Running and testing the localized application
- Localization checklist
What are we going to develop at the end of this post?
Simple Android application which greets Sachin tendulkar for the 100th century he scored. . Yes we will localize the app so that we can greet him in all languages. . Application will look something like this:
Either you can proceed with the below listings, or you can directly download code.
1. Application Localization Process
Android loads text and media resources from the project’s ‘res’directory. Additionally, Android can select and load resources from different directories, based on the current device configuration and locale.
For example, if the code loads a string called ‘R.string.title’, Android will choose the correct value for that string at runtime by loading the appropriate strings.xml file from a matching‘res/values’ directory.
In order to have a multilingual Android app you need to provide Android with the localized resource files.
If the locale is ‘en’, Android will look for a value of“R.string.title” by searching the files in the following order:
When it manages to find the string in the resource file, it uses that value and stops searching. This means that the default language acts as a fall-back option and when translation exists, it’s used instead. It also means that you should either localize everything or nothing, as seeing half translated applications is usually worse than apps that are not translated at all.
2. Translating Strings
The application requires threestrings.xml files, one for each in English, Tamil, and Hindi.
The procedure for creating strings.xml files is as follows:
- Translate the strings.xml file to each language.
- Create three new folders under res – values-en, values-ta and values-hi
- Place the translated strings.xml files in the respective folders.
|String.xml||Destination folder in project workspace|
3. Localizing Images
Our application also needs three more drawable folders, each containing a variant of the image.
Place image that has to appear for a respective language in corresponding folders under the project workspace ‘/res/drawable.
If your images contain texts, we recommend creating copies of the texts in your strings.xml file. This way, translators can translate just the texts, and you can rebuild the localized images.
|Image||Destination folder in project workspace|
After you save the strings.xml and background image files in their target folders, the project workspace looks like this:
4. Running and Testing the Localized Application
Once the localized string and image resources are added to the project workspace, the application is ready for testing. To test it, we can set different locales via Settings > Locale & text or Language & input > Select Language.
Depending on configuration of the device, some devices may offer only a few locales or may not offer any alternate locales via the settings application. In our case, select ‘Hindi’ as the locale language (make sure you know how to revert it back to English) and run the application.
5. Localization Checklist
Now that we understand how to use the Android development framework for multilingual apps, let’s go over a short checklist which reminds us the steps we need to take.
- Never hard-code strings or string constants; Instead use the R.string and strings.xml files.
- Similarly, don’t hard-code images or layouts; use R.drawable and R.layout
- Translate the strings.xml files and localize your images.
- Place your localized resources in the appropriate directories under ‘res/’.
Congratulations, We are done.
Let us test the application:
Right click on the project >> Run as >> Android application >> Choose emulator or device
Tap on the button and see what happens.
*apk in Android is the installation file simliar to exe in windows.
I hope you enjoyed the post!!