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:
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.
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.
ReplyDeletePretty 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...
ReplyDeleteAndroid App Development Sydney | wordpress development Sydney
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.
ReplyDeleteIt 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.
Thanks.
ReplyDeleteYou explain very well about Localization in Android. Thanks Author.