Wednesday, December 3, 2014

Android Localization tutorial with example


Introduction:             
                Android devices run across several regions meaning, its being used in multiple languages. So its very important that we make our application appropriate for regional languages. Below image depicts Settings in English and Italino.

English

Italino
Tutorial :
               This tutorial aims to explains a simple example where in we will display Hello World in multiple languages.

Localizing Strings :
                Basically in this tutorial we are trying to localize strings, for which make a new folder under res folder as values-local, where local indicates the code of respective language. es represents Spanish, it representsItalino and so on..



Spanish, /values-es/strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello_world">Hola Mundo!</string>
</resources>

Italy, /values-it/strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello_world">Ciao mondo!</string>
</resources>

Executing application :
              When you run the application normally you would see the text applicable to native language. My native language is English and hence I see Hello World !. Now, go to Settings - Language & Input and change Language to Italino, you should be able to Ciao mondo! as output as shown below :

Simple.. ! We are done with localization tutorial in android. Try executing this application in your machine... Source code is available at the bottom for download..


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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


</RelativeLayout>

MainActivity.java

package com.example.androidlocalisation;
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;
}


}

values-es/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">AndroidLocalization</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hola Mundo!</string>
</resources>

values-it/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">AndroidLocalization</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Ciao mondo!</string>
</resources>

2 comments:

  1. Hello! Strings can be easily localized using a collaborative translation management platform like https://poeditor.com

    ReplyDelete