Android Localization example and tutorial


Introduction:            
                Android devices run across several regions meaning, its being used in different languages. So its very important that we make our application support different languages. Android has provisions to support internationalization of our applications. Below image depicts Settings in English and Italino.
                Android language support has become a key feature of our applications since Android devices are used across the world.

English

Italino
Tutorial :
               This Android Localization 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 represents Italino 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 in android with clear 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>

Source code of this application




Reactions:

0 comments :

Post a Comment