1

Well, to begin with, I have set different resource files for each language as Google says. I have a separate Class called LocaleHelper.class to help me display the String values for each Language programmatically.

The translation works perfectly when displaying a String resource. But, when I try to display a String Array, then comes the issue. The items in String Array always displays the English words for every language

To illustrate, I have a String Array for the Spinner as below

<string-array name="SpinnerList_Number">
    <item>@string/Number_One</item>
    <item>@string/Number_Two</item>
    <item>@string/Number_Three</item>
</string-array>

where,

for English:

<string name="Number_One">One</string>

for German:

<string name="Number_One">Eins</string>

Update:

In my Activity/Fragment, I use the following code to display the text in required locale

Context context = LocaleHelper.setLocale(this, AppLanguage);
context.getResources().getString(R.string.Number_One)

To display a String Array (say in a Spinner), I use R.array.SpinnerList_Numbers. How can I use context.getResources() to display the String Array in Java code?

The following is the Code for LocaleHelper.class file

public class LocaleHelper {

    private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

    public static Context onAttach(Context context) {
        String lang = getPersistedData(context, Locale.getDefault().getLanguage());
        return setLocale(context, lang);
    }

    public static Context onAttach(Context context, String defaultLanguage) {
        String lang = getPersistedData(context, defaultLanguage);
        return setLocale(context, lang);
    }

    public static String getLanguage(Context context) {
        return getPersistedData(context, Locale.getDefault().getLanguage());
    }

    public static Context setLocale(Context context, String language) {
        persist(context, language);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context, language);
        }

        return updateResourcesLegacy(context, language);
    }

    private static String getPersistedData(Context context, String defaultLanguage) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
    }

    private static void persist(Context context, String language) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putString(SELECTED_LANGUAGE, language);
        editor.apply();
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        configuration.setLayoutDirection(locale);

        return context.createConfigurationContext(configuration);
    }

    @SuppressWarnings("deprecation")
    private static Context updateResourcesLegacy(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            configuration.setLayoutDirection(locale);
        }

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());

        return context;
    }
}

Update: Solution

As mentioned by @MiteshVanaliya, here is the code for the correct solution while using the LocaleHelper.class

ArrayAdapter<CharSequence> adapterSpinner = new ArrayAdapter<CharSequence>(
        context,
        android.R.layout.simple_spinner_item,
        context.resources().getStringArray(R.array.SpinnerList_Number));
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapterSpinner);
2

2 Answers 2

3

Create array.xml in all language file like this:

res/values/array.xml
res/values-fr/array.xml
res/values-es/array.xml

in array.xml the file contains string array list.

    Context context = LocaleHelper.setLocale(this, "fr");
    String value = context.getResources().getString(R.string.Number_One);
    String[] arrays = context.getResources().getStringArray(R.array.SpinnerList_Number);
//arrays return french value.

Will work. Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

This is what I have been following already. As mentioned, the String values are displayed according to Locale settings, but that is not the case when it comes to String Array
Thanks for guiding me. Already tried using getResources().getStringArray(). I was using ArrayAdapter.createFromResource(). Was showing error. Now found the correct solution. Thanks again
0
  • Dont use context.resources.configuration in android always init new configuration and then setLocale

val configuration = Configuration() configuration.setLocale(locale)

Comments

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.