Thursday, 5 April 2012

Multiple language support in Android

Some users expect to use the application in the local language so today I have created a demo application for support multiple languages in Android application. In Android, support of multiple language is quite simple.

Here is a screen shot of the project directory structure.


In this image you can see there are two values folder 
  1. values
  2. values-de
In both folders there is same name file strings.xml. The first values folder is for support default language for the application. values-de is for support German language in which de indicates that this is the resource for Gernan language.There are some predefined codes which you can refer from below tables.

Locale Code Language / Country Location of strings.xml Location of flag.png
Default English / United Kingdom res/values/ res/drawable/
de-rDE German / Germany res/values-de/ res/drawable-de-rDE/
fr-rFR French / France res/values-fr/ res/drawable-fr-rFR/
fr-rCA French / Canada res/values-fr/ res/drawable-fr-rCA/
en-rCA English / Canada (res/values/) res/drawable-en-rCA/
ja-rJP Japanese / Japan res/values-ja/ res/drawable-ja-rJP/
en-rUS English / United States (res/values/) res/drawable-en-rUS/

Here is my layout file 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/text" />
 
    <Button
        android:id="@+id/btnChangeLanguage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/select_language"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>

In this layout there is a one Button to open the list of the languages and Text-view to display text in the language which user selects from the list.

Here I have created two strings.xml files. One is for English and second is for German.

Strings.xml in English language

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">MultiLanguage</string>
    <string name="select_language">Select Language</string>
    <string name="text">Welcome! this is the english language</string>
</resources> 

Strings.xml in German language

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="text">Herzlich Willkommen! dies ist die Sprache Englisch</string>
    <string name="select_language">Wählen Sie Sprache</string>
</resources>


Here is my Main.java activity 

package com.example.android.multilanguage;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
Button btnChangeLanguage;
private static final  int REQUEST_CHANGE_LANGUAGE = 1;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        btnChangeLanguage = (Button)findViewById(R.id.btnChangeLanguage);
        btnChangeLanguage.setOnClickListener(onChangeLanguageButtonListener);
    }
 
    OnClickListener onChangeLanguageButtonListener = new OnClickListener() {
public void onClick(View v) {
startActivityForResult(new Intent(Main.this, Languages.class), REQUEST_CHANGE_LANGUAGE);
}
};
 
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK && requestCode == REQUEST_CHANGE_LANGUAGE) {
    Configuration c = new Configuration(getResources().getConfiguration());
    String language = data.getStringExtra("language");
    if(language.equals("English")) {
    c.locale = Locale.ENGLISH;
    }else if(language.equals("German")) {
    c.locale = Locale.GERMAN;
    }
    getResources().updateConfiguration(c, getResources().getDisplayMetrics());
    Intent intent = getIntent();
    overridePendingTransition(0, 0);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    finish();
    overridePendingTransition(0, 0);
    startActivity(intent);
    }
   }
}

Here is my List-Activity Languages .java in which I am displaying list of languages which I want to support in my application.Currently I have supported for English and German languages.

package com.example.android.multilanguage;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Languages extends ListActivity{
String[] arrLanguages = new String[]{"English", "German"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrLanguages));
}
     @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
setResult(RESULT_OK, new Intent().putExtra("language", arrLanguages[position]));
finish();
}
}

You can refer the Developer site for study in details for support multiple languages in Android application.

3 comments:

  1. If you wish to localize Android software, you can import your .xml to https://poeditor.com/ and smoothly translate the strings in there, with the help of others or not, and then easily export it in the desired format. Cheers, hope this was helpful!

    ReplyDelete
  2. Android device comes in many shapes and size all over the world. With a vast wide range of device types .In order to be as successful as possible our app need to be compatible with different device configurations. Some of the important variations that we must consider includes different languages, screen sizes, and versions of the Android platform.

    Here we are going to know how to use basic platform features that leverage alternative resources and features so our app provide an optimized user experience on variety of Android-compatible device , using a single application package(APK)

    Thumb Rule for Strings: The best practice is to extract the UI strings from the app code and keep them in an external file. Android makes this easy with a resource directory in each Android Project.

    for full solution of the sample refer here:
    http://www.mindstick.com/blog/700/Android%20support%20for%20different%20Languages

    ReplyDelete
  3. Tamil language not supported in samsung galaxy s advance in gingerbread

    ReplyDelete