How to create multilingual android application? OR How String Localization Works in android ?

Samsul Hoque
4
How to create multilingual android application? OR How String Localization Works in android ?





In this tutorial we are going to build a Multi-Language supported app that supports Italiano, Francese, Bangla,Deutsch,Hindi,Japanese.

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.

Do’s:

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.
<string name="note_email">Enter your email address</string>
When referring it in xml, use @strings notation.
<TextView ...   android:text="@string/note_email"  />
When defining the string through java code, use R.string
emailNote.setText(R.string.note_email);

Don’ts:

Never hard code the string in xml or in java code which make the translation difficult.
<TextView ...   android:text="Enter your email address"  />
emailNote.setText("Enter your email address");   


So let’s create a new project and try with an example.

2. Creating New Project

1. Create a new project in Eclipse by going to File ⇒ New ⇒ Android Application Project and fill the required information.
2.Now we must create a resource file for each localized folder, where we will insert the text as a string resource.


Created our texts, we are going to implement graphically our selection language Activity:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start activity" android:onClick="onClick"/> </LinearLayout>
The Activity has consisted of only one spinner, which allows the choice of language, and a button to start the Activity containing our text:


public class MultilingualLocalizationUpdater extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setPrompt("select language");


ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, languages);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);


spinner.setOnItemSelectedListener(new OnItemSelectedListener() {


public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Configuration config = new Configuration();
Locale locale;
switch (arg2) {
case 0:
config.locale = Locale.ENGLISH;
break;
case 1:
locale = new Locale("it"); 
config.locale =locale;
//config.locale = Locale.ITALIAN;
break;
case 2:
locale = new Locale("fr");
config.locale =locale;
//config.locale = Locale.FRENCH;
break;
case 3:
locale = new Locale("bn");
config.locale =locale;
break;
case 4:
locale = new Locale("de");
config.locale =locale;
break;
case 5:
locale = new Locale("hi");
config.locale =locale;
break;
case 6:
locale = new Locale("ja");
config.locale =locale;
break;
default:
config.locale = Locale.ENGLISH;
break;
}
getResources().updateConfiguration(config, null);
}


public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});




}

public void onClick(View v) {
startActivity(new Intent(getBaseContext(), TextActivity.class));
}
private String[] languages = { "English", "Italiano", "Francese", "Bangla","Deutsch","Hindi","Japanese" };
}

We can observe what happens when we click an element in spinner: depending on the item clickedc we understand wich Locale we must create, so then, we invoke the method updateConfiguration to update the resource configuration! WOW!
That is going to change all the values ​​of resources based on location.
The layout of the Activity that shows the text contains only a TextView that refers to our resource of type String, then by the choice of language made ​​we can see the different texts.


Post a Comment

4Comments
  1. Hello! My suggestion is to try using a collaborative translation platform to localize your strings. https://poeditor.com/ is an online software localization platform that includes many useful features such as translation memory, API, GitHub and BitBucket integration, automatic translation. There is also a WordPress plugin https://wordpress.org/plugins/poeditor/ so POEditor users manage easily translations directly from WordPress via the POEditor API.

    ReplyDelete
  2. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing...
    Android App Development Sydney | wordpress development Sydney

    ReplyDelete
  3. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog.
    It is very nicely explained. Your article adds best knowledge to our Java Online Training from India.
    or learn thru Java Online Training from India Students.

    ReplyDelete
  4. Thanks.
    You explain very well about Localization in Android. Thanks Author.

    ReplyDelete
Post a Comment