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.

2 Answers 2

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

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 :)
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
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.

2 Comments

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<String>? what do you think?
I meant the corresponding indexes of the arrays

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.