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:

  1. Application localization process
  2. Translating strings
  3. Localizing images
  4. Running and testing the localized application
  5. Localization checklist

Quick Links

Code Listings

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.

[pgsubscribe]

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:

  1. ‘res/values-en/strings.xml’
  2. ‘res/values-ta/strings.xml’
  3. ‘res/values-hi/strings.xml’

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 EnglishTamil, and Hindi.
The procedure for creating strings.xml files is as follows:

  1. Translate the strings.xml file to each language.
  2. Create three new folders under res – values-envalues-ta and values-hi
  3. Place the translated strings.xml files in the respective folders.
String.xml Destination folder in project workspace
English values-en/strings.xml
Tamil values-ta/strings.xml
Hindi values-hi/strings.xml

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
English drawable-en-hdpi/sachin.png
Tamil drawable-ta-hdpi/sachin.png
Hindi drawable-hi-hdpi/sachin.png

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.

  1. Never hard-code strings or string constants; Instead use the R.string and strings.xml files.
  2. Similarly, don’t hard-code images or layouts; use R.drawable and R.layout
  3. Translate the strings.xml files and localize your images.
  4. Place your localized resources in the appropriate directories under ‘res/’.

[pglinkadssmall1]

Demo

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.



Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android is the installation file simliar to exe in windows.

[pglinkadssmall]

I hope you enjoyed the post!! :)

Suggested posts for further reading


[pgfeedback]
[pgwriteforus]

Related Articles

Author: Android Guru

Share This Post On
  • Nora

    Thank you!

    Super helpful :)

  • Taj Marlow

    Really nice exercise! I suggest using a localization tool for translating the Android apps, as is saves a lot more time. https://poeditor.com/ works great with .xml files, try it.

  • Jayshree

    hi..will u please tell me how to add hindi,tamil languages to locale.

  • Saj

    Thank u,
    Nice & helpful example
    :)

  • Hong

    Nice post. I tried to add “zh-rCN” for simplified Chinese, but the app crashed. I created folders /res/values-zh-rCN, /res/drawable-zh-rCN-hdpi, and added corresponding resources. Any clue why it doesn’t work?

Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares