0

I'm working an Android app and want to translate it to Persian language. All String values in the project are saved into an resource XML file. Some String values are used in layout files and some in classes (R.String). When using Persian text in Android, it has to be reshaped to be displayed correctly.

I want to reshape all resource String values without calling reshape method for every String value.

Can anyone explain me how to do this?

Some idea:
override String class
override getResource class

firedev
  • 20,898
  • 20
  • 64
  • 94
Micle
  • 159
  • 10

1 Answers1

2

Android provides already functionality (well documented) to have a multilanguage app. Instead of replacing your text and having severall versions of your app for each language, its better to use the frameworks function. In that case the app chooses a suitable language from the available languages you provided depending on the device locale settings.

So basically you would start by creating the required directory structure (link above):

MyProject/
    res/
       values/
           strings.xml
       values-fa/
           strings.xml

and then you fill in String values into the files, e.g.:

English (default locale), /values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">My Application</string>
    <string name="hello_world">Hello World!</string>
</resources>

Persian, /values-fa/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">...</string>
    <string name="hello_world">...</string>
</resources>

(quoted and adapted from the link above)

Solution for reshape farsi language
The reshape function has to be used for every Farsi String value, so setting string in the XML layout isn't possible (AFAIK), so the following proposals assume, that all String values are set programatically.

Using a global static wrapper function #1

public static final String getLocalizedString(int resId) {
  if(Locale.getDefault().toString().equals("fa_IR")) {
       return PersianReshape.reshape(getResources().getString(resId));
  }else{
      return getResources().getString(resId);
}

You can now use this function to load the String (you have to change each occurence) or you override e.g. the getRessource method. I personally would prefer using a static function instead of overriding because of possible other problems regarding loading resources of other type than String, side effects etc.

Creating a custom class with overriding setText() for each used ui widgets #2
Another possibility is to create custom ui widgets that do a call to PersianReshape.reshape() when display. E.g. for EditText:

class CustomTextField extends EditText {
    @Override
    public void setText(String text) {
        super.setText(PersianReshape.reshape(text));
    }
}
[...]
CustomTextField myTextBox = new CustomTextField();
myTextBox.setText("....");
Community
  • 1
  • 1
alex
  • 5,516
  • 2
  • 36
  • 60
  • thank's alex,project has values-fa resource directory,but problem in reshaping.reshape method will call every time that use Persian string. – Micle Apr 22 '14 at 08:43
  • I'm not sure if i understand your issue. You use something like String hello = getResources().getString(R.string.hello_world); to get a string and its always in the desired alnguage, isn't it? – alex Apr 22 '14 at 09:03
  • yes,PersianReshape.reshape(getResources().getString(R.string.hello_world)); PersianReshape.reshape call every time in Persian string. – Micle Apr 22 '14 at 09:05
  • Ok, i think i got it: You mean reshape = inverse order / mirror ? – alex Apr 22 '14 at 09:50
  • no, Persian language correct text with reshape: متن آزمایشی; without reshape: م ت ن آزم ا ی ش ی; blow link show what i'm say: http://stackoverflow.com/questions/15855875/dipaly-farsi-text-in-android-webview-for-phones-not-supporting-farsi – Micle Apr 22 '14 at 10:18
  • Ok, great then my solution above should work for you. – alex Apr 22 '14 at 10:42
  • but how override getresource().getString() that usable in all class and activity layout(android:text="@string/about") – Micle Apr 22 '14 at 10:58
  • Try it like in the following link. But this isn't good because you have to do it in all your activities (a lot of work) and even then you can't handle string values in your layout (xml). So the best way is, to use the function i provided above to load strings in your java code. For your layout UI objects you have to use e.g. the setText() method to load string values instead of setting it via xml http://stackoverflow.com/a/18642210/1965084 – alex Apr 22 '14 at 11:33
  • Maybe the second possible solution is easier for you to implement (see above) ? – alex Apr 22 '14 at 11:55
  • another way:reshape all string just one time in different Java project and save result in resource directory. – Micle Apr 23 '14 at 17:03