Saturday, August 15, 2015

Provide Multiple Language(Localization) in Android Application


In this post, i will explain how to make your application available for different languages.

To put a string/word is easy in android, if too lazy,
just use this line, android:text = "YOUR TEXT", in XML Layout file or
use this, view.setText("Your Text"), in Java File.



But, if  you use above method that just make code management become hardest and to make an application may support different language is impossible or difficult.



Then, please store all text in value/strings.xml...
then call the value like this, android:text = "@string/your_text" in XML file, or
like this, view.setText(getResource().getString(R.string.your_text)), in Java File.

Firstly, create a layout and all text store in  a single value/string.xml, like below :

strings.xml :



fragment_mail.xml : 





Then, create a new values resource file for other language,

1) Right-click on values folder, and select "Values resource file"



2) set file name as "strings", btw you can set any other name, it is not really important but recommendation please use same name as default one,




3) add Locale as chosen qualifier,




4) Now, after you add locale as qualifier, list of languages and region is appear.




5) For this example, I used japanese as second language, and doesn't specific the region (Any Region)




6) Finaly, click ok and new values resource file created, same name with default but in different folderDefault strings store in values and japanese string store in  values-ja,




7) copy values\strings.xml content to values-ja\strings.xml, then replace every word in the xml with second language, like below :







Now,  the app can support two different language. To make the app display second language(Japanese), the user need change the device's language to Japanese language, otherwise the app will use default string resource. You may see the image below, to know how to change the app language,


Source Code : language 1.zip

Sample Running : 



Start-up



To change language go to Setting > Language&input




Select second language



Now, open the app again





Continue, with second part, how to force app change language, in other word, change language inside the application without go to setting,

Now, I will add two more button, for "default" language button and "Japanese" language button in fragment_main.xml. Here the updated code :



The more complex part is inside MainActivity.java file. Here you need edit configuration of the app and tell it what language should use. This process better done during activity execution, if not, you need to write many line of code to set every text/item of the view exist in the activity.

In this example, when the user click on the button, the app store the prefer language in SharedPreferences, and restart the app, The configuration updated at the top of the activity in onCreate() method, here MainActivity.java file code :




At line 33, you can see that I set a string in SharedPreferences, that string is a language code. Japanese language code is "ja"..how to know the code, you can find it at the values resources file you created, the folder the file store will end with two code and that is the language code. For example, "values-ja", so, "ja" is the language code.

And here the segment code to edit configuration and change the language,



used this line, <item name="android:windowAnimationStyle">@null</item>,
to remove the animation of activity at styles.xml,





Now, the app can change the language inside the app and here also the end of the explanation, this force method usually used in game not in application. So, force change language is not recommended by me, if used it in application and it just my opinion. Localization not only for word, you also can use it for image, layout or any XML file. Btw, below the full project code for this part and the sample running,

Source Code : language 2.zip

Sample Running :


Start-up



"Japanese" button clicked




If you have problem or something to ask that related to java...
you can email or comment on this post...
I will try to find the solution and share it with you..


you can test it by yourself..
i hope you like this souce code...
and please give some comment....
Created By : Z-man, 2015

No comments:

Post a Comment