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 – >

 

About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

COMMENTS (14)
Reply

Dear Dr. Droid,
Thank you, thank you, THANK YOU for your amazing work! You just helped me a great deal. Pls kindly give directions on how to implement this in a project with more than one activity? Your help is much appreciated. Thanks again.

    Hi Byron,

    To implement the string localisation in whole app/project you need to re-start the app again. Like a button in your app is changing language then on your button click you need to call below method:

    Intent i = new Intent(Your_Activity.this, Splash.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
    finish();

    Thanks

      Dear Dr. Droid,
      Thanks for your quick reply. I am assuming that “splash.class” is just an example? Also, can i put your new code inside your “changeLocale” method? it will look something like this;

      public void changeLocale(String lang) {
      if (lang.equalsIgnoreCase(“”))
      return;
      myLocale = new Locale(lang);//Set Selected Locale
      saveLocale(lang);//Save the selected locale
      Locale.setDefault(myLocale);//set new locale as default
      Configuration config = new Configuration();//get Configuration
      config.locale = myLocale;//set config locale as selected locale
      getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
      updateTexts();//Update texts according to locale
      Intent i = new Intent(Your_Activity.this, MainActivity.class);
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(i);
      finish();
      }

      Also, what about extending your mainactivity from other activities?
      i tried it when i added a second activity to your project, and it seemed to work well. is it safe to do this?
      I ask because in my project, most of the activities are using different color themes. if it will work, perhaps i can make your project a “settings” page and extend it from other activities? Thanks again, Dr. Droid, for your valued help.

    Hi Byron,

    Below are the answers of your queries:

    1. I am assuming that “splash.class” is just an example? – YES
    2. Do not call this updateTexts(); method because as you are restarting app so this method no more required to call.
    3. i added a second activity to your project, and it seemed to work well. is it safe to do this? – YES
    4. In manifest you can change the theme of my project activity if it is not matching with yours.

    Thanks

Reply

Hi Dr. Droid, it was great to see your knowledge on Android. I need your help as I am preparing a project and I am also a freelancer and very keen to learn new things. In my project I want multilingual feature but as you said that there is another method without android localisation means I do not want to change the language from device’s settings so I want to try your alternate method as you defined in your point no. 1. So how could it be done and what will be the code? Please help me.

Reply

hello
how are ?
how to implement this in a project with more than one activity?
if we use 3 activity

Reply

and what we need to work for all activity this change

    Hi Samir,

    You have to use this changeLocale(String lang) method to update the language of your app. This method you need to put into your first activity like your SplashActivity or your MainActivity rest android will handle itself to show particular changed language.

    Thanks

Reply

very thanks my bro to answer me
if send me to i put with first activity

thanks

Reply

if upload code for me my bro is very good
to all activity i want use for some activity

thanks

    Hi Samir,

    Put the below code to your starting activity and call it onCreate method. You need to pass a locale(language) that you want to change to this method and also create a shared preference so that whenever you start your app again it will pick the saved locale.

    //Change Locale
    public void changeLocale(String lang) {
    if (lang.equalsIgnoreCase(“”))
    return;
    myLocale = new Locale(lang);//Set Selected Locale
    saveLocale(lang);//Save the selected locale
    Locale.setDefault(myLocale);//set new locale as default
    Configuration config = new Configuration();//get Configuration
    config.locale = myLocale;//set config locale as selected locale
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());//Update the config
    updateTexts();//Update texts according to locale
    }

    Try this, if you face any issue let me know.

    Thanks

Reply

my bro i’m very try not succeeded
if make for me and send very thanks

    Hi Samir,

    Can you share your code with me or can you connect with me via TeamViewer so that i can help you properly.
    My email id is at my blog.

    Thanks

Reply

Hi
my bro tomorrow i send my code for you

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)