Skip to main content

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 in the assests folder. We need to get a TypeFace instance as below.
          Typeface face = Typeface.createFromAsset(getAssets(),"DroidSansRegionalAAD.ttf");
Now set this face to the textview on which you want to have the text to be displayed using the .ttf files as below.
            
           mTV_names.setTypeface(face);

For the text to be displayed in the particular language, we need to have the particular word in the string.xml file as below : 
                 <string-array name="languages">
     <item>భారతదేశం</item>  
      <item >పాకిస్తాన్</item>
        <item >అమెరికా</item>
        <item >कनाडा</item>
        <item >இலங்கை</item>
        <item >చైనా</item>
        <item >ఆస్ట్రేలియా</item>
        <item >జపాన్</item>
   </string-array>
                    

Consider my code in which i used a list to display the above string array as below :

  •  @Override
  •     public void onCreate(Bundle savedInstanceState) {
  •         super.onCreate(savedInstanceState);
  •         setContentView(R.layout.localization);
  •         mList = (ListView)findViewById(R.id.localization_LV_list);
  •         mBTN_next = (Button)findViewById(R.id.localization_BTN_next);
  •         countries = getResources().getStringArray(R.array.languages);
  •         
  •         mList.setAdapter(new CustomAdapter(this));
And my CustomAdapter Class is as below 


  • class CustomAdapter extends BaseAdapter{

  •      Context context;
  •      LayoutInflater inflater ;
  • public CustomAdapter(Context c) {
  • context = c;
  • }
  • public int getCount() {
  • return countries.length;
  • }
  • public Object getItem(int arg0) {
  • return arg0;
  • }
  • public long getItemId(int arg0) {
  • return arg0;
  • }
  • public View getView(int arg0, View arg1, ViewGroup arg2) {
  • View convertView = arg1;
  • if(convertView == null)
  • {
  • inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  • }
  • convertView = inflater.inflate(R.layout.inflating, arg2, false);
  • TextView mTV_names = (TextView)convertView.findViewById(R.id.inflating_TV_names);
  • // typeface used to display the text in different language other than english 
  • // here it is tamil, hindi and telugu.
  • Typeface face = Typeface.createFromAsset(getAssets(), "DroidSansRegionalAAD.ttf");
  • //setting the typeface to the particular text 
  • mTV_names.setTypeface(face);
  • mTV_names.setText(""+countries[arg0]);
  • return convertView;
  • }

  •     }

That's it.
We can get the language we wish to onto our mobile UI besides providing the correct .ttf file that supports the language.




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

Comments

  1. Where i get DroidSansRegionalAAD.ttf ple give suggestion to me

    ReplyDelete
    Replies
    1. Try searching in google with " DroidSansRegionalAAD.ttf download" and you will find it.
      Not posting the link where you can find it because, I am afraid they might or might not be in future.
      If you still are not finding it let me know. If will help you in some way or the other.

      Delete
  2. Hi How to display type face in listview using custom array adapter. I am stuck in that please help with a small example

    ReplyDelete
    Replies
    1. Hi Shanu,
      As far as i remember, even for array adapter you can override getview() similar to the example above to make it work.
      Give it a try.

      Delete
  3. Does this work with Right to left languages like Hebrew and Arabic?

    ReplyDelete
    Replies
    1. Check devaju or Roboto ttf files.. They should be working well

      Delete
  4. your code is good. same topic may I use in slideout menubar items????

    ReplyDelete
    Replies
    1. For sure the same code works even for the slideout menu bar items.

      Delete

Post a Comment

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