Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Localize the NAME of a string-array (and it's items)

I’ve got a string-array

<string-array name="Color">
    <item>@string/red</item>
    <item>@string/green</item>
    <item>@string/purple</item>
</string-array>

<string name="red">red></string>
<string name="green">green></string>
<string name="purple">purple></string>

And I asses it in Java:

int id = R.array.Color;
String[] colors = getResources().getStringArray(id);
    
variantTextView.setText(getResources().getResourceEntryName(id));
ansA.setText(colors[0]);
ansB.setText(colors[1]);
ansC.setText(colors[2]);
  1. How do I localize the NAME of this array?
  2. Is it required to extract the strings as I’ve done it? Or is there a way to localize the entire array?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

How do I localize the NAME of this array?

You don’t, any more than you localize a variable name in your Java/Kotlin code. If you want to display Color to users, have that to-be-displayed value as a separate string resource.

Is it required to extract the strings as I’ve done it?

No.

Or is there a way to localize the entire array?

Have two copies of your Colors <string-array>, each in the proper resource set based on language. Each <string-array> can then have <item> elements that contain the actual strings, not references to string resources. You might do this if you want the array to have a different order of elements (e.g., sorted alphabetically).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading