Android Support Multiple Languages in Android
Post
Cancel

Support Multiple Languages in Android

A - Introduction

In Android development, you can keep the UI strings seperately with app code by storing all the strings in external files.

You can find the res folder in Android project structure. Within this res directory are subdirectories for various resource types.

There’re some default xml files in res folder such as res/values/strings.xml which contails all application’s strings.

To support multiple languages in Android, simply, we have to provide different string.xml files for each language.

B - Create Locale Directories and String Files

To support multiple languages in Android, firstly you have to create different res/values folders for each language. The res/values folder must include the ISO country code at the end of the its name. For example: res/values-fr will contains all values for French apps.

Inside each res/values folder, you must define the string.xml file with appropriate supported language.

multiple languages

The content of string.xml files:

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

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

</resources> 
  • Spanish:
<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="app_name">Demostración Múltiples idiomas</string> 
    <string name="hello_world">¡Hola, mundo </string> 

</resources> 
  • French:
<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="app_name">Demo plusieurs langues</string> 
    <string name="hello_world">Bonjour tout le monde </string> 

</resources> 

P/S: I used Google Translate for Spanish and French so please don’t mind if there’re any rediculous mistakes about translations.

C - Use String Resources

It’s super easy to get the String resource value:

  • Using in app code:
// Get a string resource from your app's Resources 
String hello = getResources().getString(R.string.hello_world); 

// Or supply a string resource to a method that requires a string
TextView textView = new TextView(this);
textView.setText(R.string.hello_world);
  • Using in XML layout:
<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="ice.tea09.demomultiplelanguages.MainActivity$PlaceholderFragment" > 

    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/hello_world" /> 

</RelativeLayout> 

And the result for this demo:

  • Default:

dèault

  • French:

france

  • Spanish:

spanish

D - Download Source Code Support Multiple Languages in Android

https://drive.google.com/file/d/0Bw3dwdSezn6fNGVoVDh0dXNORFE/edit?usp=sharing

This post is licensed under CC BY 4.0 by the author.