Skip to main content

Multi-Language Supported Application by Localization

To build multilingual Android apps we need to put bit effort in translating the content of the App and putting them in the appropriate folders.

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

1. 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 French, you should create a values folder named values-fr and keep a strings.xml file in it with all the strings translated into French 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 French)
2. If the app supports selected language, android looks for it’s string resources in values-(ISO language Code) folder in the project. (For french it loads the string values from values-fr/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.

2. Check the Sample for Localization 
This sample will explain you by displaying "Hello World" in different languages when changing the language in setting menu(Tool Bar menu)

Create a new project and follow the coding
Open strings.xml located under values folder and add following strings. These are default English language strings.
<?xml version="1.0" encoding="utf-8"?><resources>
    <string name="app_name">Localizing Sample</string>
    <string name="hello">Hello World</string>
</resources>

Now under res folder create three folders named values-tavalues-frvalues-hi and a strings.xml file in each of the folders.

Your project should look like this once you created the required files/folders.
Now translate the strings into respected languages and place them in appropriate strings.xml files.

Tamil values-ta/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">à®®ாதிà®°ியான à®®ாதிà®°ி</string>
    <string name="hello">ஹலோ à®‰à®²à®•à®®்</string>
</resources>
Hindi values-hi/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">नमूनाकरण à¤•ा à¤¸्थानीयकरण</string>
    <string name="hello">नमस्ते à¤¦ुनिया</string>
</resources>
French values-fr/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Localisation de l\'échantillon</string>
    <string name="hello">Bonjour le monde</string>
</resources>

Open your main activity layout file (in my case activity_main.xml) and add the following content to create a simple layout. This layout contains a textview at center of a screen 

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context="com.mrbrown.locale_sample.MainActivity">

    <TextView
        android:id="@+id/idText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textStyle="bold"
        android:textColor="@color/colorPrimaryDark"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
Open your MainActivity.java and make sure that it has following code. This code will be added automatically when you create new project.

public class MainActivity extends AppCompatActivity {

    public static String languageToLoad = "en";
    public static SharedPreferences sharePrefs;
    SharedPreferences.Editor sharePref_Editor;
    private TextView txtView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sharePrefs = PreferenceManager
                .getDefaultSharedPreferences(MainActivity.this);
        txtView = (TextView) findViewById(R.id.idText);

        if (sharePrefs.getString("language", "").equalsIgnoreCase("hi")) {
            String languageToLoad = "hi"; // your language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());
            setTitle(getResources().getString(R.string.app_name));
            txtView.setText(getResources().getString(R.string.hello));

        } else if (sharePrefs.getString("language", "").equalsIgnoreCase("en")){
            String languageToLoad = "en";
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());
            setTitle(getResources().getString(R.string.app_name));
            txtView.setText(getResources().getString(R.string.hello));
        }else if (sharePrefs.getString("language", "").equalsIgnoreCase("fr")){
            String languageToLoad = "fr";
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());
            setTitle(getResources().getString(R.string.app_name));
            txtView.setText(getResources().getString(R.string.hello));
        }else {
            String languageToLoad = "ta";
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());
            setTitle(getResources().getString(R.string.app_name));
            txtView.setText(getResources().getString(R.string.hello));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.lang_setting_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.eng:
                languageToLoad = "en"; // English
                Locale locale = new Locale(languageToLoad);
                Locale.setDefault(locale);
                Configuration config = new Configuration();
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(config,
                        getBaseContext().getResources().getDisplayMetrics());
                sharePref_Editor = sharePrefs.edit();
                sharePref_Editor.putString("language", "en");
                sharePref_Editor.commit();
                Intent intHome1 = new Intent(MainActivity.this, MainActivity.class);
                startActivity(intHome1);
                finish();
                overridePendingTransition(R.anim.bottomin, R.anim.leftout);
                break;
            case R.id.hn:
                languageToLoad = "hi"; // Hindi
                locale = new Locale(languageToLoad);
                Locale.setDefault(locale);
                config = new Configuration();
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(config,
                        getBaseContext().getResources().getDisplayMetrics());
                sharePref_Editor = sharePrefs.edit();
                sharePref_Editor.putString("language", "hi");
                sharePref_Editor.commit();
                Intent intHome = new Intent(MainActivity.this, MainActivity.class);
                startActivity(intHome);
                finish();
                overridePendingTransition(R.anim.bottomin, R.anim.leftout);
                break;

            case R.id.fr:
                languageToLoad = "fr"; // French
                locale = new Locale(languageToLoad);
                Locale.setDefault(locale);
                config = new Configuration();
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(config,
                        getBaseContext().getResources().getDisplayMetrics());
                sharePref_Editor = sharePrefs.edit();
                sharePref_Editor.putString("language", "fr");
                sharePref_Editor.commit();
                Intent intHome2 = new Intent(MainActivity.this, MainActivity.class);
                startActivity(intHome2);
                finish();
                overridePendingTransition(R.anim.bottomin, R.anim.leftout);
                break;

            case R.id.ta:
                languageToLoad = "ta"; // Tamil
                locale = new Locale(languageToLoad);
                Locale.setDefault(locale);
                config = new Configuration();
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(config,
                        getBaseContext().getResources().getDisplayMetrics());
                sharePref_Editor = sharePrefs.edit();
                sharePref_Editor.putString("language", "ta");
                sharePref_Editor.commit();
                Intent intHome3 = new Intent(MainActivity.this, MainActivity.class);
                startActivity(intHome3);
                finish();
                overridePendingTransition(R.anim.bottomin, R.anim.leftout);
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
Now if you run the project you should see the app in English (assuming that your device is set to English language)

In order to see the app in other languages follow below steps
Click the option menu in tool bar and select a language



Screen of corresponding selected language will be like
 

3. Android Localization Language ISO Codes

Below table give you ISO languages codes for all the languages that android supports.
LanguageLocalevalues/strings.xml
Germandevalues-de/strings.xml
Chinesezhvalues-zh/strings.xml
Czechcsvalues-cs/strings.xml
Dutchnlvalues-nl/strings.xml
Frenchfrvalues-fr/strings.xml
Italianitvalues-it/strings.xml
Japanesejavalues-ja/strings.xml
Koreankovalues-ko/strings.xml
Polishplvalues-pl/strings.xml
Russianruvalues-ru/strings.xml
Spanishesvalues-es/strings.xml
Arabicarvalues-ar/strings.xml
Bulgarianbgvalues-bg/strings.xml
Catalancavalues-ca/strings.xml
Croatianhrvalues-hr/strings.xml
Danishdavalues-da/strings.xml
Finnishfivalues-fi/strings.xml
Greekelvalues-el/strings.xml
Hebrewiwvalues-iw/strings.xml
Hindihivalues-hi/strings.xml
Hungarianhuvalues-hu/strings.xml
Indonesianinvalues-in/strings.xml
Latvianlvvalues-lv/strings.xml
Lithuanianltvalues-lt/strings.xml
Norwegiannbvalues-nb/strings.xml
Portugueseptvalues-pt/strings.xml
Romanianrovalues-ro/strings.xml
Serbiansrvalues-sr/strings.xml
Slovakskvalues-sk/strings.xml
Slovenianslvalues-sl/strings.xml
Swedishsvvalues-sv/strings.xml
Tagalogtlvalues-tl/strings.xml
Thaithvalues-th/strings.xml
Turkishtrvalues-tr/strings.xml
Ukrainianukvalues-uk/strings.xml
Vietnamesevivalues-vi/strings.xml

4. Translation Services
Right now I used Google Translate service to translate the strings into other languages. But if you want more accurate and meaningful translation always go for professional services like Professional translations through Google Play

Finally Localization Checklist gives you list of things to be verified before the app goes live when localization considered.

________________________________________________________________________

Happy Coding...














Comments

Popular posts from this blog

Get Phone Number from Contact List - Android Tutorial

When you create an application to send sms or an application to make calls, getting a destination number from the contacts list is a common task. In this Android tip, I am going to show the code to fetch a number from the contacts list. Now let me tell you how to achieve the goal. First, you need to create an Intent object for the PICK_ACTION action. To open the contacts list, the table that contains the contacts information must be specified as a parameter of the constructor of the Intent class. You can refer to the table using ContactsContract.Contacts.CONTENT_URI. Then call the startActivityForResult () method passing the Intent object and request code to open the contacts list. After a contact is selected from the contacts list, to get the result, you need to override the onActivityResult(int reqCode, int resultCode, Intent data) method of the activity. You can call the getData() method of the data parameter to get the table or uri that contains the selected contact. From the t

Spinner with Search on DropDown - Android Tutorial

If you have more values on Dropdown of Spinner its hard to select the last item by making a long scroll. To overcome this issue Android introduced a component called  AutoCompleteTextView Yes it is!!! Then why Spinner with Search? There may be some requirement even though gave much knowledge about it. There is a simple and good library that helps us to achieve this -  SearchableSpinner Gradle dependencies {     ...     implementation 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1' } Usage Now replace your Normal Android Spinner on XML with the following < com.toptoche.searchablespinnerlibrary.SearchableSpinner     android:id="@+id/id_city"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="@android:color/transparent"     android:padding="5dp" /> ___________________________________________________________

Set Focus on Spinner when select Item on Vertical Scroll - Android Tutorial

We may face an issue on Spinner lies on long vertical scroll, (i.e.) when selected and item from dropdown the focus moves to top of scroll. To avoid this please follow this piece of code spinner.setFocusableInTouchMode( true ); spinner.setOnFocusChangeListener( new View.OnFocusChangeListener() {     @Override     public void onFocusChange(View v, boolean hasFocus) {         if (hasFocus) {             if (spinner.getWindowToken() != null ) {                 spinner.performClick();             }         }     } });   _______________________________________________________________________________ Happy Coding...