android kotlin:反压后locale不会改变

问题描述 投票:2回答:1

我打算构建支持多种语言的Android应用程序。我有res-values字符串,zh和en目录。

我使用Localehelper来更改应用程序中所有活动的语言。

当我从活动A开始在活动B中更改语言时,持久数据会发生变化。然后,我按下Android设备上的后退按钮,找出活动A不会更改其区域设置。

您能否告诉我如何在这种情况下更改区域设置?

活动B:

  val selectCode =  mySetCode[index]
            LocaleHelper.setLocale( this , selectCode)
            finish()

活动A:

 override fun onResume() {
        super.onResume()
        initView()

        val preferences = PreferenceManager.getDefaultSharedPreferences(this)
        val language = preferences.getString("Locale.Helper.Selected.Language" , null)
        if(language != null){
            LocaleHelper.onAttach( this , language)
        }

    }

locale helper.Java

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

        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;
    }
}
android kotlin locale android-preferences
1个回答
1
投票

这是WordPress应用程序中的implemented fairly well

关键类/方法是AppSettingsFragment.changeLanguageLocaleManager.updateResourcesWPMainActivity.appLanguageChangedApplicationLifecycleMonitor.onConfigurationChanged

看起来新的语言首选项在本地保存,然后重新创建应用程序,在重新实例化时应用新语言。希望这足以运行。我会评论,但我缺乏4代表:p

© www.soinside.com 2019 - 2024. All rights reserved.