5

I have defined a string array in my values folder in this way for default English language

<string-array name="movie">
        <item>11001#Action#1</item>
        <item>11002#Animation#1</item>
        <item>11003#Comedy#1</item>
        <item>11004#Documentary#1</item>
        <item>11005#Family#1</item>
        <item> 11006#Film-Noir#1</item>
        <item>11007#Horror#1</item>
        <item>11008#Musical#1</item>
        <item>11009#Romance#1</item>
    </string-array>

My applicaiton is suported for many language like chainese simplified thats why i have defined another string-array in values-zh-rCN/string.xml for chainese simplified langue like this way

<string-array name="movie">
        <item>11001#动作片#1</item>
        <item>11002#动画片#1</item>
        <item>11003#喜剧#1</item>
        <item>11004#记录片#1</item>
        <item>11005#家庭#1</item>
        <item> 11006#黑色电影#1</item>
        <item>11007#恐怖片#1</item>
        <item>11008#音乐剧#1</item>
        <item>11009#爱情片#1</item>
    </string-array>

when my app is running and seleted Default english langue shown the array data but when i have seleted phone language in chainese simplified or other langue instead of english the array data does not shown. what are the possible solutionn for these problem ?

1
  • Did you find the solution ? If yes, please share the answer. Commented Mar 18, 2021 at 0:27

3 Answers 3

4

Write your texts in string.xml as strings items, then use them into arrays.xml, example:

string.xml (English):

<string name="array_power_unit_item_00">By Watt.</string>
<string name="array_power_unit_item_01">By KiloWatt.</string>
<string name="array_power_unit_item_02">By Ampere and Volt.</string>
<string name="array_power_unit_item_03">By MilliAmpere and Volt.</string>

string.xml (Arabic):

<string name="array_power_unit_item_00">بالواط.</string>
<string name="array_power_unit_item_01">بالكيلو واط.</string>
<string name="array_power_unit_item_02">بالأمبير و الفولت.</string>
<string name="array_power_unit_item_03">بالميلي أمبير و فولت.</string>

arrays.xml:

<string-array name="arraypowerunit" translatable="false">
<item>@string/array_power_unit_item_00</item>
<item>@string/array_power_unit_item_01</item>
<item>@string/array_power_unit_item_02</item>
<item>@string/array_power_unit_item_03</item>
</string-array>
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work for me, after locale manual change in app settings and recreating activity I still receive strings in arrays in phone's locale. Everything else works fine.
1
<string name="abcd">喜剧</string>
<string name="efgh">记录片</string>

<array name="array_abcd">
     <item>@string/abcd</item>
     <item>@string/efgh</item>
</array>

6 Comments

if i change this way do i need to change in jave code ? where i need to difine string array and string value ?
are you sure this is the solution for this problem ?
try this by yourself. I am sure for 110%... If this will work then accept this answer
sure . but where I need to define the array and single string . in string valie ? or i need to define array.xml file for array
are you there now ?
|
1

Use this method for get resource and get array from this resource. this will work 100%

public static Resources getResourcesBasedOnLanguage(Activity activity) {
    Configuration confTmp =new Configuration(activity.getResources().getConfiguration());
    confTmp.locale = new Locale("EN");
    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return new Resources(activity.getAssets(), metrics, confTmp);
}

1 Comment

This works for me, thank you. Only thing that concers me is that Resources constructor is deprecated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.