Android Building Multi-Language Supported App

Android Building Multi-Language Supported App

Android is one of the few popular mobile operating systems having millions of users over 190 countries and growing day by day. So when you are aiming your app to be globally successful, it is always a good idea to make the app localised by supporting different languages.

While localising, you should consider using appropriate text, audio, currency, numbers and graphics depending upon the region or country. But this tutorial only covers localising strings i.e supporting multiple languagesLocalizing with Resources explains about other things should be considered when localizing your app.

How String Localization Works

By default android considers English as primary language and loads the string resources from res ⇒ values ⇒ strings.xml. When you want to add support for another language, you need to create a values folder by appending an Hyphen and the ISO language code. For example if you want to add support for Russian, you should create a values folder named values-ru under res directory and keep a strings.xml file in it with all the strings translated into Russian language.

In brief the localization works as follows :

1. When user changes the device language through Settings ⇒ Language & Input, android OS itself checks for appropriate language resources in the app. (Let’s say user is selecting Russian)

or

If you don’t want to change language from settings then you have to create language options in your app and save that locale in Shared Preferences for future use. (This method will change language of your app only not whole device like above one.)

2. If the app supports selected language, android looks for it’s string resources in values-(ISO language Code) folder in the project. (For russian it loads the string values from values-ru/string.xml)

3. If the supported language strings.xml misses any string value, android always loads the missing strings from default strings.xml file i.e values/strings.xml

So it is mandatory that the default stings.xml file should contains all the string values that app uses. Other wise the app will crash with Force Close error.

Below are two things that you have to follow:

1. Never hard code the string in xml or in java code which make the translation difficult.

2. While you are supporting multiple languages, you should consider below as a best practice while defining the strings. Always declare the string in strings.xml only.

When referring declared strings in xml, use @string notation.

When defining the string through java code, use R.string

Example

In this tutorial, we are going to build a Multi-Language (Multilingual) supported app that supports EnglishFrench, Deutsch (German), Hindi and Russian.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Open strings.xml located under res=>values folder and add following strings. These are default English language strings.

3. Now under res folder create three folders named values-de, values-fr, values-hi, values-ru and a strings.xml file in each of the folders.

Your project should look like this once you created the required files/folders.

Multilingual App String Values

Multilingual App String Values

Now translate the strings into their respected languages and place them in appropriate strings.xml files You can use Google Translator for strings conversion.

  • Hindivalues-hi/strings.xml
  • Russian – values-ru/strings.xml
  • Deutsch(German) – values-de/strings.xml
  • French – values-fr/strings.xml
4. Now create a layout naming activity_main.xml and add the following code. In this there are several buttons for different language selection.
5. Finally come to your MainActivity.java and add the following code. In this we are saving the selected language in SharedPreferences and loading saved language by refreshing texts.

The below method is important for changing selected language.

Full MainActivtiy.java Source Code

6. Finally, run the application and you will get the output as shown in video.

Thanks. :)

Download Source Code – >

 

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)