Localizing an Android App

It is an easy task to implement localization in an Android App, you just need to create the resource files for each language and later Android will be responsible to present the correct one for each device configuration.

This article cover the steps to create the resources and explain how to force a specific language to be used if needed. For this example I managed to provide localization for english, spanish and portuguese. Android Studio was used to implement all steps.

When you create a new project in Android Studio (blank activity template), you will be presented to a project structure as seen in the image, with a folder /res/values and inside it a file strings.xml. In this file you will put all keys to strings that must be translated and used in your layouts. This unique resource repository will be very useful to organize your project.
localization_1

 

Create inside /res, a new folder and give it the name values-en and inside it create a file and name it strings.xml. Now create another folder inside /res named values-es and inside it create another file named strings.xml. Now you have the project structure like the image.

localization_2

 

The file /values/strings.xml will store the keys and translations for the default language (in this sample it will be portuguese)

Open the file /values-es/strings.xml and insert the translation for spanish.

And finally open the file /values-en/strings.xml and insert the translation for english.

In Android Studio you will find a very useful tool to help in the localization process. To use it, click with your right mouse button over any of the strings.xml files and select the item Open Translation Editor

localization_3

 

Now a window like this will be presented and you will see that each key have a place to receive the translation for each language you created folder for. With this you can see all translation keys and current values in the same place.

localization_4

Now we will use the activity to show the TextView with key “ola_mundo”.

Android Studio already created a layout called activity_main.xml. Just update the content to :

It create the associated class (no changes here).

Now, if your device is configured to english, this will be presented:

localization_5

Or, if it is configured to spanish, this will be presented:

localization_6

So, as the user use a different configuration for language, Android will choose the best one to be presented. If we don’t provide a localization for the user language, the default value will be used. So, for example, if the user device is configured to “fr” (french), the default language (the one that is stored in the folder /res/values will be presented (in our example, portuguese).

But there is more, you can give the user the choice to select the desired language to be used in the app even if the device is configured to another one. In our example, even if the user has french as device default language, he may want to use english besides portuguese (our default language resource). So you can implement a feature to give the user this option, using this simple function to change current app Locale:

And call it this way (change to spanish )

Important to note that this function doesn’t change the settings in a definitive way. It only changes for the current app session. If the user closes the app, next time it starts the app, the language of the device will be used again. So if you need to maintain the selected language you will need to implement a way to store the user preferences and when starting the app check and set the correct language again.

Finally, to test this features in an emulator, you can change the language this way:

Step 1
Start the emulator
Step 2
Run

adb shell

or

adb -e shell you you have a device connect

Step 3
When the prompt appear run

setprop persist.sys.language [langcode];setprop persist.sys.country [ccode];stop;sleep 5;start

changing the [langcode] with the language code and [ccode] with the country code

ex.

setprop persist.sys.language es;setprop persist.sys.country ES;stop;sleep 5;start

will change the language to spanish and the country to Spain.