When we develop an Android App, our main target is to make it accessible to a larger user base. Initially, we put up all our strings and descriptions in English but not all our users can read the language and understand it. And if our userbase is spread over users from various countries, it becomes quite important for us to add Localization Support in Android apps.

A fun fact — An Indian app called ShareChat launched its application only in the local languages excluding English but was able to get more than 100M+ downloads in less than a year.

There are so many languages to reach out and it is all done through common practice. Once the base is set up, it becomes very easy to change the languages dynamically as per the user’s needs.

Steps To Add Localization Support in Android apps —

  1. Put up all the strings used in XML or Java inside strings.xml. In doing so, it is very easy.

In res->values->strings.xml

Add a string for “Hello” as

<string name="hello">Hello</string>

For using it in XML files, you can use it as

="@string/hello"

For using it in java,

getString(R.string.hello)

2. Now, we will add another language to our application. I will add the Hindi language and for doing so, we will have to find the language code. You can find the reference for language code at — https://web.archive.org/web/20120814113314/http://colincooper.net/blog/2011/02/17/android-supported-language-and-locales/

For Hindi, the language code is “hi”.

We will create a new strings.xml file. Right-click on res->values and select New->Value Resource File.

Enter the File Name — strings

In Directory Name enter — values-hi and press OK.

Localization support in Android apps - strings.xml

You will see that the strings turn into a folder and now has 2 files in it. One will be strings.xml and the other will be strings.xml(hi).

3. Now in the strings.xml(hi), we will add our new string.

<resources>
    <string name="hello">नमस्ते</string>
<resources>

Ta-Da! We are nearly done. Now we just have to let the user change the language.

4. In the MainActivity.java or wherever you want your user to change the language, we will throw a dialog and ask them which they prefer. Here, the code is written to change the language from either,

a. English to Hindi

b. Hindi to English

Here, we will define two functions —

a. changeLanguageDialog() — Function will help us to display an AlertDialog for the users to confirm if they want to change the language.

private void changeLanguageDialog() {
    new AlertDialog.Builder(MainActivity.this)
            .setTitle(getString(R.string.language_header))
            .setMessage(getString(R.string.language_description))
            .setCancelable(false)
            .setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    setLocale();
                }
            })
            .setNegativeButton(getString(R.string.cancel), null)
            .show();
}

b. setLocale() — Function will help us to change the language and reload the activity.

public void setLocale() {
    Locale locale;
    int language_type = sharedPreferences.getInt("language_type", 1);
if (language_type == 1) {
        locale = new Locale("hi");
        sharedPreferences.edit().putInt("language_type", 0).apply();
    } else {
        locale = new Locale("en");
        sharedPreferences.edit().putInt("language_type", 1).apply();
    }

    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    //restart Activity
    finish();
    startActivity(getIntent());
}

Explanation — Here, we have an integer value stored in “language_type”. This helps us to determine which language to change to. Once decided, we use the Locale to change the language and update the configuration. After doing this, the important step is to Reload the Activity.

A common mistake here is that if there are any activities present in the stack, the language will not be updated for them. Those activities will continue to show the previous language. To eliminate that, we can launch the activity using the flags,

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

This will clear the stack and keep the activity on top and hence any activity opened after this will have the changed language strings.

Also, sometimes we would want to prevent some strings from being translated. Let’s say, we want to keep the app name in English alone. For doing so, in the strings.xml, we will add the string as —

<string name="app_name" translatable="false">Sample App</string>

Note — Every time the app is opened, the Locale has to be set manually. For this, a sharedPreference integer can be used to check the set value and update the locale.

Conclusion-

Congratulations! We have successfully added localization support within our application. Extending the localization to other languages is very easy as we just need to add another strings.xml for the language code and let the user access it through a menu. Once we put up all the strings in strings.xml, the work becomes very easy and we can provide various language support in our application.


If you are planning to publish Android App Bundle with localization support in android apps, this article might be helpful for you to avoid common mistakes .

Leave a Reply

Your email address will not be published. Required fields are marked *