1

I have an application that gives the user the right to choose an item from array in string.xml:

<array-string name="countries">
    <item>USA</item>
    <item>UK</item>
</array-string>

and another array in the translated-to-Chinese string.xml:

<array-string name="countries">
    <item>美国</item>
    <item>联合王国</item>
</array-string>

For example, the app has two TextViews, I want the user to select a certain country in Chinese, the first TextView shows the value in Chinese & the second shows its value in English (from default string.xml).

I hope my intent is clear.

khalid3e
  • 399
  • 3
  • 14

2 Answers2

1

If you have various res folders for different locales, you can do something like this:

Configuration conf = getResources().getConfiguration();
conf.locale = new Locale("pl");
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Resources resources = new Resources(getAssets(), metrics, conf);
String str = resources.getString(id);

If your min sdk > 17 try this

@NonNull Resources getLocalizedResources(Context context, Locale desiredLocale) {
    Configuration conf = context.getResources().getConfiguration();
    conf = new Configuration(conf);
    conf.setLocale(desiredLocale);
    Context localizedContext = context.createConfigurationContext(conf);
    return localizedContext.getResources();
}
Zachary Sweigart
  • 1,051
  • 9
  • 23
  • Thanks for the code, but my concern is about the single item in an array-string (which is not defined by a "name" or id) & from what I noticed your code is cool if I want to get a normal string resource, could you please edit it for me :) – khalid3e Aug 28 '18 at 00:40
  • 1
    Well at that point your lists should be parallel so you can `String[] chineseCountries = getLocalizedResources(context, Locale.CHINA).getStringArray(R.array. countries)` and `String[] englishCountries = getLocalizedResources(context, Locale.US).getStringArray(R.array. countries)` then `chineseCountries[n]` will equal `englishCountries[n]` where n is any int 0 to the size of your list as long as you keep the translations in order – Zachary Sweigart Aug 28 '18 at 02:34
1

Translated versions of strings.xml are not used for this purpose.
They are used to provide locale-language string resources.
Since you need in the same language version of your app both the Chinese and the English values you should have put both the string-arrays in the same default strings.xml and give them proper ids like countries_chinese and countries_english.
Then load them in 2 separate arrays with proper names and use the corresponding values of each.

  • Good. That's a way to do it, but for "The corresponding values" how can I get them. As you know, items in array-string & array don't have name="" property! So should I use an item index in array-string? or put both array-strings in a ArrayList? what do you think? – khalid3e Aug 28 '18 at 00:28
  • I meant the corresponding indexes of the arrays –  Aug 28 '18 at 05:09