Tutorial #96: Implement multi-language support in Android

Hey friends!

Today, Android has become one of the dominant mobile operating systems in the world. Android applications are now used by people living across the globe. It is important for developers to build applications that can support multiple languages. In order to reach maximum users, an application should handle text, audio files, numbers, currency, and graphics in ways appropriate to the locales where it will be used.

By default Android considers English as the primary language and loads the string resources from res/values/strings.xml file. In order to add support for another language, one needs 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.

As mentioned on the developer’s page, it’s always a good practice to extract UI strings from your application code and keep them in an external file. Through this tutorial, we will learn how to implement multi-language support in Android.

Pre-requisites: Eclipse IDE, Android SDK

Step 1: Create Android project

Launch Eclipse IDE and create a new Android application project called AndroidMultiLanguageSupportDemo. Let’s keep the package name as com.app.multilanguage. Choose the target SDK as Android 4.4 (API level 19)

Step 2: Add multi-language support

As mentioned earlier, we need to create multiple values folder for the appropriate language depending on the language code as follows!

res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello_world">Hello world!</string>
</resources>

res/values-fr/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="hello_world">bonjour le monde!</string>
</resources>

MainActivity.java

package com.app.multilanguage;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

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

res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}">

    <TextView
        android:id="@+id/txtView"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#FE2E2E"
        android:text="@string/hello_world" />

</RelativeLayout>

Finally, save all changes. Make sure no errors are present. Run the application on an Android device and you should see the following output for the respective languages.

English

English

Japanese

Japanese

Hindi

Hindi

French

French

That’s it for this tutorial. Hope it helps! 🙂

Reference: Supporting different languages

Karan Balkar About Karan Balkar
self proclaimed extraterrestrial fandroid, computer engineer, amateur gamer and die hard punk rock fan!

One thought on “Tutorial #96: Implement multi-language support in Android

Leave a Reply

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