user3314337
user3314337

Reputation: 341

Changing language programmatically in Android

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);

Upvotes: 1

Views: 1544

Answers (2)

Dishant Kawatra
Dishant Kawatra

Reputation: 648

  • Dont use context.resources.configuration in android always init new configuration and then setLocale

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

Upvotes: 0

Mitesh Vanaliya
Mitesh Vanaliya

Reputation: 2591

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.

Upvotes: 3

Related Questions