首页 > 解决方案 > 如果我以编程方式提供,应用程序不会更改语言

问题描述

我的应用程序中有 2 个 strings.xml 文件,1 个用于“en”,另一个用于“es”。

我已经添加了所有必要的代码,它工作正常。

如果设备语言设置为“英语”,它会使用英语 strings.xml 文件启动应用程序,如果是西班牙语,它会加载西班牙语字符串。

但现在我也为用户提供了设置语言的选项。

现在,当我将“en”或“es”传递给 Locale 助手类时,它根本不会影响应用程序。即使重新启动应用程序也不会更新字符串。

当用户更新语言时,我执行以下代码。

LocaleHelper.setLocale(getApplicationContext(), "es");
LanguagePreference.getInstance().setUserLanguage(getApplicationContext(), "es");

这是我的 LocaleHelper 类 ,我认为这里没有问题,否则默认设备语言也将不起作用。

public class LocaleHelper {

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

    public static Context onAttach(Context context) {
        String lang = getPersistedData(context, LanguagePreference.getInstance().getUserLanguage(GlobalApplication.getAppContext()));
        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, LanguagePreference.getInstance().getUserLanguage(GlobalApplication.getAppContext()));
    }

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


        Log.d("LocaleSet", 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);

        Configuration configuration = context.getResources().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.JELLY_BEAN_MR1) {
            configuration.setLayoutDirection(locale);
        }

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

        return context;
    }
}

标签: androidandroid-layoutandroid-fragments

解决方案


尝试这个:

String tag = Resources.getSystem().getConfiguration().locale.toLanguageTag();

and after check the 'tag' 
if(tag.contains("en"))
//your code
else if(tag.contains("es"))
//your code

OBS.:使用 try/catch


推荐阅读