Localize your Android Application

Deepak Goyal
4 min readMay 8, 2020

What does this mean? It means to make your app support multiple locales or regions so that it can reach most of the users. Some users don’t understand English but they know Hindi, so your app also has to support the Hindi language.

When you create a new Android Studio project a strings.xml file is automatically created under res->values. We must write all the default strings there. For e.g. the app_name.

res->values->strings.xml

To insert the translation of another language, you need to create a new file named strings.xml but for the different locales.

creating strimgs.xml file for a different locale

After creating the strings.xml file for different locales

folders

As you can see there are three folders related to values

values, values-pt and values-pt-rBR

Each folder contains the file named strings.xml

values -> strings.xml: contains the default strings for all locales.

values-pt -> strings.xml: contains the strings in Portuguese but for all regions such as Brazil, Portugal, etc.

values-pt-rBR -> strings.xml: contains the strings in Portuguese but for Brazil region only.

Note: We can have as many files as we want in those folders. To maintain the project structure and easy to understand, we can create different files for different features. Writing all the strings of the app in just one file can be messy.

For e.g: You can have the file named validations.xml in those folders just for the validation messages in the app. But the name of the string must be unique in all files.

You cannot have a string with the name title in the strings.xml as well as in validations.xml file.

How Android system choose the string from those files?

Suppose we have the string with the name “text” in all three files among different folders which can be referenced as R.string.text in the code.

  • First, the system will check the string in specific language_region folder
  • Second, the system will check the string in the language folder only
  • Last, the system will look in the default values folder

Examples

If your device setting is pt-BR

The app will try to fetch the string with name text from the files in the folder values-pt-rBR and the string is found so it will be displayed.

If your device setting is pt-PT

The app will check the values-pt-rPT folder but there is no such folder. So the app will check the folder values-pt and the string is found so it will be displayed.

if your device setting is el-GR

The app will check the values-el-rGR folder but there is no such folder. So the app will check the folder values-el folder but this folder also not exists. Finally, the app will display the string from the values folder.

Question

Suppose in the app there are two folders values and values-pt-rBR. Now if your device setting is pt-AO then from which folder the app will display the string?

Answer

If your answer is values folder then you are right but if your answer is values-pt-rBR then you are also right.

What??????????

Don’t get confused 😕. Let me explain.

values folder is the right answer until Android 7.0 but starting from Android 7.0, values-pt-rBR is the right answer.

From Android 7.0 and above, the system tries to find the string in all the regions/children of language. The app first check in the folder values-pt-rAO but this folder does not exist. Second it will try the folder values-pt but this folder also does not exist. Now the app will check all the children of values-pt because the language is pt, so here it will also check the folder values-pt-rBR and will display the string from there.

Official Documentation

Read the official documentation at https://developer.android.com/guide/topics/resources/multilingual-support

Before Android 7.0
Starting from Android 7.0

Change the language of App

To change the language of the app, we need to change the device settings to that language. But we can do the same without changing the device settings. And the solution is to implement this feature in the app itself.

I already have a GitHub repo with that functionality. Please check and try the source code at https://github.com/deepak786/SettingsLocaleSample

Change the language of the app inside the app

Thank you for reading the article. If you have any queries please let me know.

Happy Coding :)

--

--

Deepak Goyal

I’m a Software Engineer. I love programming. #java #android #kotlin #dart #flutter #firebase