Android Localization Tutorial

Advertisements


An android application can run on many devices in many different regions. In order to make your application more interactive, your application should handle text,numbers,files e.t.c in ways appropriate to the locales where your application will be used.

In this chapter we will explain , how you can localize your application according to different regions e.t.c. We will localize the strings used in the application, and in the same way other things can be localized.

Localizing Strings

In order to localize the strings used in your application , make a new folder under res with name of values-local where local would be the replaced with the region.

For example, in the case of italy, the values-it folder would be made under res. It is shown in the image below:

Anroid Localization Tutorial

Once that folder is made, copy the strings.xmlfrom default folder to the folder you have created. And change its contents. For example, i have changed the value of hello_world string.

Italy, res/values-it/strings.xml

<;?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">Ciao mondo!</string>
</resources>

Spanish, res/values-it/strings.xml

<;?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">Hola Mundo!</string>
</resources>

French, res/values-it/strings.xml

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

Apart from these languages, the region code of other languages have been given in the table below:

Sr.NoLanguage & code
1Afrikanns
Code: af. Folder name: values-af
2Arabic
Code: ar. Folder name: values-ar
3Bengali
Code: bn. Folder name: values-bn
4Czech
Code: cs. Folder name: values-cs
5Chinese
Code: zh. Folder name: values-zh
6German
Code: de. Folder name: values-de
7French
Code: fr. Folder name: values-fr
8Japanese
Code: ja. Folder name: values-ja

Example

Here is an example demonstrating the use of Localization of strings. It creates a basic application that allows you to customize your application according to US and Italy region.

To experiment with this example , you can run this on an actual device or in an emulator.

StepsDescription
1You will use Eclipse IDE to create an Android application and name it as Locals under a package com.example.locals. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2Modify src/MainActivity.java file to add necessary code.
3Modify the res/layout/activity_main to add respective XML components
4Modify the res/values/string.xml to add necessary string components
5Create the res/values-it/string.xml to add necessary string components
6Run the application and choose a running android device and install the application on it and verify the results

Following is the content of the modifed main activity file src/com.example.locals/MainActivity.java.

package com.example.locals;

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

public class MainActivity extends Activity {

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

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

}

Following is the modified content of the xml 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"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="174dp"
      android:text="@string/hello_world"
      android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Following is the content of the res/values/string.xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>

   <string name="app_name">Locals</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>

</resources>

Following is the content of the res/values-it/string.xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>

   <string name="app_name">Locals</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Ciao mondo!</string>

</resources>

Following is the content of AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.locals"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="17" />

   <application
   android:allowBackup="true"
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
      <activity
         android:name="com.example.locals.MainActivity"
         android:label="@string/app_name" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Let's try to run our Localization application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Eclipse, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Eclipse installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window:

Anroid Localization Tutorial

Now change your device language setting from menu/system-settings/language to italy.

Now opne the application again and this time you will see hello world in italian language. It has been shown below::

Anroid Localization Tutorial

Advertisements
Advertisements