我要构建支持多语言的 Android 应用程序。 我有 res-values 字符串,包括 zh 和 en 目录。
我使用 Localehelper 更改应用程序中所有 Activity 的语言。
当我从 Activity A 开始在 Activity B 更改我的语言时,持久数据会发生变化。然后,我在 Android 设备上按下后退按钮,发现 Activity A 没有更改其语言环境。
你能告诉我在这种情况下如何更改语言环境吗?
Activity B:
val selectCode = mySetCode[index]
LocaleHelper.setLocale( this , selectCode)
finish()
Activity 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)
}
}
语言环境助手.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;
}
}
最佳答案
这是 implemented fairly well在 WordPress 应用程序中。
关键类/方法是 AppSettingsFragment.changeLanguage , LocaleManager.updateResources , WPMainActivity.appLanguageChanged , 和 ApplicationLifecycleMonitor.onConfigurationChanged .
看起来新语言首选项已保存在本地,然后重新创建应用程序,在重新实例化时应用新语言。希望这足以运行。我会刚刚发表评论,但我缺少 4 个代表 :p
关于机器人 Kotlin : locale des not change after back press,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52309082/