Wednesday, 8 August 2012

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.

8 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