Skip to main content

Localization

Android supports a wide range of languages.
If you want to know all the supported languages you can visit here .

Using localization concept one can display a text in a any language corresponding to his Locale.
Suppose a client(may be a person from France) is interested in getting his app design in English when he is in a country where the locale is English and  in his country he would be looking to see his app in French. In this situation we can use Localization.

Consider the following screen shots :




The first snapshot is HELLO WORLD in French.
When ever we change the Locale to France we can see the HELLO WORLD in French language.

For this to happen we must do the following :


  • Create a folder under the res folder with name  values-fr. Here 'fr' refers to French language.
  • Create a strings.xml file similar to the one in the values folder. 
  • If we need to display a TextView in different languages in different Locales, set the text of the textview using the strings folder.
        Consider the following : 
         

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.         android:layout_width="fill_parent"
  3.         android:layout_height="fill_parent" >

  4.         <TextView
  5.             android:id="@+id/main_TV_text"
  6.             android:layout_width="wrap_content"
  7.             android:layout_height="wrap_content"
  8.             android:layout_alignParentLeft="true"
  9.             android:layout_alignParentTop="true"
  10.             android:layout_marginLeft="38dp"
  11.             android:layout_marginTop="29dp"
  12.             android:text="@string/text"
  13.             

  14.     </RelativeLayout>


If u consider the 13-th line : Here i am setting the text to the TextView using a value in strings.xml file like below : 
  
       <string name="text">HELLO WORLD</string>


  • Now in the strings.xml file of values-fr folder write HELLO WORLD in french (use Google translator ) 

        <string name="text">BONJOUR TOUT LE MONDE</string>

Whenever the Locale changes the appropriate values folder will be used.

Testing it in emulator :
                Go to settings --> Language & keyboard --> Select Language -->  and select Français(France).

Later  when you run your application again you can see  BONJOUR TOUT LE MONDE displayed on the screen.

We can also use .ttf files which i will discuss in the next post.

Any suggestions are welcomed.
Please comment if anyone has some or the other problem with this one.
























Comments

Popular posts from this blog

Image Slide Show

Hi everyone. In this tutorial i am going to show you all the code which when run will display the images as a slideshow. Below is the attached screen shot :  For this i am considering the images from the Sdcard's images folder where i am having all the images. Next to display the images i am using View Flipper concept. I am adding ImageViews to the viewflipper so that the images can be displayed onto the screen. Now lets start  Design an xml layout as below (say 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" >     <ViewFlipper         android:id="@+id/main_flipper"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentLeft="true"  

Template literals

Template literals: ES6   introduced many cool things among which one is ‘ Template literals ’   This is a new way of representing strings.    Normally we use either “(double quotes) or ‘(single quote) to represent a string in  JavaScript .   For example:   var name=”literal”;   Or    var name=’literal’;   But from ES6 there is a new way of doing it. We can use `(back tick) symbol which is present left to key ‘1’ in keyboard.    Ie.,   var name = `literal`;   Well when we already have “(double quotes) or ‘(single quote), what extra does `(back tick) do?   Template literals  shines well when we have some concatenation of strings or when creating multi-line strings.   Look at below to understand the power of Template literals.   Let's  take an example where we need to form a string where you are forming a string based on a button clicked. For example - “You clicked on login button”.     Old way:   var 

Localization using .ttf files

In my previous post  here  i gave some information on how to use the concept of localization for the languages that are supported by Android. Now suppose if u want to display a language that is not supported by android.......? Then what might be solution? I will be discussing a small example for doing this now. Suppose if you want to display some text like this " हिंदी " or  " தமிழ் " or  some thing like the text   here  any other language that is  not supported by android we can use .ttf files so that we can print the font we are interested. To achieve this first we need to download few .ttf files. You can google for a file you need, download it and place this into the assets folder. In the picture i am having few other ttf files which i included, But for the time being only the DroidSansRegionalAAd.ttf is discussed by me which i used to display the words of few languages of India as in the following picture. Now we need to refer to the ttf file i