gpt4 book ai didi

java - Android 在某些部分显示文本语言错误

转载 作者:行者123 更新时间:2023-12-01 19:09:09 24 4
gpt4 key购买 nike

在应用程序上,有时会显示错误的文本语言,例如更改应用程序语言时,某个 Activity 会同时显示“zh_tw”和“en”。但有时却没有这个问题。

以下是更改语言代码:

public static void setApplicationLanguage(Context context, Locale locale) {

Resources resources = context.getApplicationContext().getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
config.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
config.setLocales(localeList);
context.getApplicationContext().createConfigurationContext(config);
Locale.setDefault(locale);
}
resources.updateConfiguration(config, dm);

Intent intent = new Intent(context, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
finishAffinity();
context.startActivity(intent);
}

我谷歌搜索找不到任何解决方案或类似的情况。谢谢。

最佳答案

为此,请从扩展 Application 类的类(基类)调用 LocaleeManager 类中的函数在您的应用程序类中,

  @Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleManager.setLocale(base));
}

在您的区域设置管理器中。此代码不应存在与本地 API 更改相关的问题。这对我有用

private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources res = context.getResources();
Configuration config = new Configuration(res.getConfiguration());
config.setLocale(locale);
context = context.createConfigurationContext(config);
return context;
}

关于java - Android 在某些部分显示文本语言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59496847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com