3

I have an Android application which I wrote for english language now I want to convert it to farsi/persian language. I want to know how can i type persian text for the TextView text.How can i maintain both the english and persian String.xml.please help.

cheers Zolf

Locale locale = new Locale("fa-IR"); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    getBaseContext().getApplicationContext().getResources().updateConfiguration(config, null); 

where do i put this code,now i put this code in the OnCreate and it does not change the text of the TextView to persian

Bob
  • 22,810
  • 38
  • 143
  • 225
ZAJ
  • 793
  • 3
  • 23
  • 50

2 Answers2

2

For showing correct form of Persian characters use this solution.

Update

And for changing current resources that is loaded in your UI:

tf = Farsi.GetFarsiFont(this);

tvTitle01 = (TextView) findViewById(R.id.tvTitle01);
tvTitle01.setTypeface(tf);
tvTitle01.setText(Farsi.Convert(tvTitle01.getText().toString()));
Community
  • 1
  • 1
Bob
  • 22,810
  • 38
  • 143
  • 225
  • thanks for your comment.I had a look at that link and it works good for EditText and creating it from code. what I have is that my activity UI is build using xml and I want to put persian text for TextView which is in the xml file.how do i do it. – ZAJ Jun 30 '12 at 08:12
  • cheers,one question now my TextView looks like this in the xml file. – ZAJ Jun 30 '12 at 08:25
  • select an id for your view like this: `android:id="@+id/tvTitle01` – Bob Jun 30 '12 at 08:33
  • cheers mate it worked as expected. one thing,I have 2 folder values and values-fa. in the values folder i have eng text and in the fa folder i have persian text,but the problem is that the persian String.xml is not getting called.am i missing something. – ZAJ Jun 30 '12 at 08:33
  • 'System.out.println("My locale::"+Locale.getDefault().getDisplayLanguage()); Locale locale = new Locale("fa"); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); Toast.makeText(this, "Locale in Persian !", Toast.LENGTH_LONG).show(); System.out.println("My locale::"+Locale.getDefault().getDisplayLanguage());' – ZAJ Jun 30 '12 at 08:34
  • Maybe your device does not support Persian language. – Bob Jun 30 '12 at 08:35
  • i am testing on the emulator,but in the System.out.println i get this "My locale::English My locale::فارسی " – ZAJ Jun 30 '12 at 08:36
1

Android has built-in mechanism of localization.

http://developer.android.com/guide/topics/resources/localization.html

you can create the different folder of values as per language and can keep different string.xml file for each language. Example:

res/values/strings.xml Contains English text for all the strings that the application uses, including text for a string named title.

res/values-fr/strings.xml Contain French text for all the strings, including title.

res/values-ja/strings.xml Contain Japanese text for all the strings except title.

If your Java code refers to R.string.title, here is what will happen at runtime:

If the device is set to any language other than French, Android will load title from the res/values/strings.xml file.

If the device is set to French, Android will load title from the res/values-fr/strings.xml file.

KitKat
  • 1,495
  • 14
  • 15
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • thanksfor your comments.where do to set the language of the device.for e.g. i want to use persian/farsi font. – ZAJ Jun 30 '12 at 06:05
  • by code : http://stackoverflow.com/questions/9679574/android-is-there-any-way-to-change-the-default-language-of-android-to-new-lang – Dheeresh Singh Jun 30 '12 at 06:07
  • I have a TextView and want to change its text to persian text.how can i do it. in the String.xml when i change the english text to the perisan text it puts spaces between the character,this i am testing on gingerbread 2.3.3 – ZAJ Jun 30 '12 at 06:28
  • sorry but not have idea about this, can try http://translate.google.co.in/?hl=en&tab=wT – Dheeresh Singh Jun 30 '12 at 06:32