Android 语言环境在运行时没有(完全)改变

Android locale not (completely) changing at runtime

我正在构建一个 Android 应用程序,其中需要在运行时更改区域设置。 我重新创建了整个 activity 和一些绑定到我的 viewmodel 的字符串更改,但 (strings.xml) 的其他字符串仍未翻译。 我在 activity 中使用了以下 class 来翻译:

    public class Language {
        private static String PREFS_LANGUAGE = "LANGUAGE";

        public static void setLanguage(Context context, String language, Activity activity) {
            // Save selected language
            persist(language);

            // Update language
            Locale locale = new Locale(language);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = Locale.UK;
            context.getResources().updateConfiguration(config, MainApplication.getContext().getResources().getDisplayMetrics());
            activity.onConfigurationChanged(config);
        }

        private static SharedPreferences getSharedPrefs(Context context) {
            return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
        }

        private static void persist(String language) {
            SharedPreferences.Editor editor = getSharedPrefs(MainApplication.getContext()).edit();
            editor.putString(PREFS_LANGUAGE, language);
            editor.apply();
        }
    }

在我的 activity 中:

       fun setLanguage(language: String){
            Language.setLanguage(MainApplication.getContext(),language, this)
        }

         override fun onConfigurationChanged(newConfig: Configuration) {
            super.onConfigurationChanged(newConfig)
             recreate()
        }

我只是根据 Java.

从我的旧来源复制粘贴了这个

在您的应用程序 class 和 Mainactvity 中覆盖它。

  @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocaleHelper.onAttach(base));
        Log.e(TAG, "attachBaseContext: ");
    }

How to use: Whenever you click something, invoke like this.

这里,

language is hi (Hindi) and the region is India (IN)

例如:

LocaleHelper.setLocale(this, "hi", "IN");
recreate(); //now restart.

本地帮手class

 public class LocaleHelper {
    private static final String TAG = "DAFT_PUNK_LH : ";


    private static final String SELECTED_LANGUAGE = "en";
    private static final String SELECTED_LANGUAGE_COUNTRY = "US";


    public static Context onAttach(Context context) {
        Log.d(TAG, "onAttach:");
        String lang = getPersistedData(context, Locale.getDefault().getLanguage());
        String langCountry = getPersistedCountryData(context, Locale.getDefault().getCountry());
        return setLocale(context, lang, langCountry);
    }


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

    public static String getLanguageCountry(Context context) {
        return getPersistedCountryData(context, Locale.getDefault().getCountry());
    }

    public static Context setLocale(Context context, String language, String langCountry) {
        Log.d(TAG, "setLocale:  ");
        persist(context, language, langCountry);

        return updateResources(context, language, langCountry);


    }

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

    private static String getPersistedCountryData(Context context, String defaultLangCountry) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(SELECTED_LANGUAGE_COUNTRY, defaultLangCountry);
    }

    private static void persist(Context context, String language, String langCountry) {
        Log.d(TAG, "persist:  ");
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();

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


    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context, String language, String langCountry) {
        Log.d(TAG, "updateResources:  ");
        Locale locale = new Locale(language, langCountry);
        Locale.setDefault(locale);

        Configuration configuration = context.getResources().getConfiguration();
        configuration.setLocale(locale);
        configuration.setLayoutDirection(locale);

        return context.createConfigurationContext(configuration);
    }
}

notTdar 的回答帮助我找到了解决方案,但我也在我的视图模型中发现了一个错误:

变化: getContext().getResources().getString(R.string.str)

致: Mainapplication.getContext().getResources().getString(R.string.str)