Android localization: Step-by-step

You may be building your first Android app or you may already have a few. Why are you creating them? Hopefully, you want your apps to grow and be used worldwide. But most of the world does not speak English. Sooner or later, you will need to support multiple languages. In this article, we will explain how to get started with Android localization using step by step examples.

Prepare your Android app for localization

Create a simple Android project from a template in Android Studio. It should look like this:

Note: We removed some stuff that we don’t need for now.

The”activity_main.xml” is where you put your text. For now, let’s put a simple “TextView” with a string “Hello world” in it:

There’s a problem here. The string “Hello world!” is hard-coded in. You do not want “Hello world!” to be embedded into the source code. You want the text to change if your user changes to another language.

To localize your text, use Android Resource system. Open the “res/values” folder. Inside, look for the “strings.xml” file. We will save your default strings here:

We named our sample text string resource (key) “my_string_name”. This is how we will refer to “Hello world!” string in the future. Make sure you come up with descriptive names for your keys, so you don’t forget what they denote, e.g. “settings_language_title”, “settings_language_description” etc.

Add more languages

The wizard will let you choose from a list of available Resource qualifiers for your new folder. Qualifiers are used by the system to decide which resources to load based on the user’s device configuration such as selected language, screen size, country code etc. You can read more about Providing Resources here. Keep in mind that these qualifiers can be used for other types of resources as well such as images or layouts for example.

Select “Locale” from the list and add it to the chosen qualifiers:

You can now choose the language you want. We want “ru: Russian”, “Any Region”.

Click the OK button. A folder named “values-ru” should now be available to you.

Create a strings.xml file in “values-ru” folder. Your directory structure should now look like this:

Change application Locale programmatically

By default, Android will try to load resources depending on the System language that is set on your user’s phone. So if your user wants to use the Russian version on a phone that is set to English, he will not be able to. If there are no resources available for current device configuration, Android will fall back to the default resources in your “values” folder without qualifiers.

There is a workaround. Here’s how to set the Locale of your application to override the Locale set in user’s system:

4. Now reference your Application class in the AndroidManifest.xml. See the “android:name” attribute within the “application” element? You also have “app_name” string resource used as a label. Your user will see whatever is written here as your app’s name on his phone:

As you see, different languages have a different number of plural forms. English only has two, Russian has three, Arabic goes up to six! Android supports all of them – zero, one, two, few, many and other.

Read more about plural rules for different languages here and more about plurals in Android here.

Look at the highlighted lines above. Numbers 1 and 5 are repeated twice for a reason. The first number is only used to calculate the required plural form. The second number is used for display purposes: it replaces the “%s” in our display text.

The result should look like this:

How to manage many text strings in different languages in Android

Easy to manage a couple of strings in two languages? Sure. But as you add more features and expand to more markets, your app will grow to thousands of strings in a dozen of languages. Here’s how to automate your text string localization:

Upload your translation files. Yes, the ones you created at the beginning of this tutorial.

1. Click the “Upload files” button.

2. Drag and drop string.xml files to upload:

Once your files are in Lokalise, they will look like this:

Lokalise editor is the most feature-rich and includes cross-platform keys, multi-lingual view, cross-key referencing, proofreading, automatic translation suggestions, translation memory and much more.

How to edit your translations

Edit any other text that you need to change. Let’s say you are a dog person. Click on the “cat” and replace it with “dog” in the popup.

Then click the “Download” button to download your new translations:

Choose the “Android Resources” file format from the list;

Press the “Build and download” button to obtain the .zip archive with your new translations;

Replace the old files in your project with the new ones.

Lokalise automatically generates the requested translation files with all your project languages for the specified platform.

The next step in your Android localization is to integrate the Lokalise Android SDK with your apps. It provides for instant updates of text strings without submitting new app versions to the Google Play.

Was this tutorial helpful? If you still have questions about localization on Android, talk to us in the live support chat. Otherwise just sign up or sign in to start localizing your app!