Search code examples
androidarraysdata-bindingmultiple-languages

Data binding for multiple languages


I have an android application, with 2 supported languages (greek & english). I have my string.xml for each language and they work correctly. My problem is that I have a Viewpager which gets its items from arrays like:

rank = new String[] { "one", "two", "three", "four"};

after binding, following this tutorial http://www.androidbegin.com/tutorial/android-viewpager-gallery-images-and-texts-tutorial/. I tried to do something like this, into my Viewpage_activity:

if(Locale.getDefault().toString()=="el_GR")
{
   rank = new String[] { "ένα", "δύο", "τρία"};
}
else
{
   rank = new String[] { "one", "two", "three"};
}

But it doesn't work. Which is the best way to do data binding, when you have multiple languages?


Solution

  • You can simply create that array in both string.xml where rank is the name of the array in both xml files

    <string-array name="rank">
        <item>One</item>
        <item>Two</item>
        <item>Three</item>
    </string-array>
    

    and in greek strings

    <string-array name="rank">
        <item>ένα</item>
        <item>δύο</item>
        <item>τρία</item>
    </string-array>
    

    then use the array in your java code like :

    String[] rank = getResources().getStringArray(R.array.rank);
    

    hope it's what you're looking for :)