最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Why does changing the in app language work on some devices but not others - Stack Overflow

programmeradmin0浏览0评论

I can change the language locale on my Galaxy S23 Ultra, but when I try on my Galaxy A14 5g, the language remains the same. They are both running Android 14. I don't understand what I am doing wrong. Here is the code I am using:

String languageToLoad = "es";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;

MainActivity.this.getResources().updateConfiguration(config, MainActivity.this.getResources().getDisplayMetrics());

Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

I've tried changing the xml string file name. It is currently values-es. I've tried changing the bundle language enableSplit to false. These did not work what can I do differently?

I can change the language locale on my Galaxy S23 Ultra, but when I try on my Galaxy A14 5g, the language remains the same. They are both running Android 14. I don't understand what I am doing wrong. Here is the code I am using:

String languageToLoad = "es";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;

MainActivity.this.getResources().updateConfiguration(config, MainActivity.this.getResources().getDisplayMetrics());

Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

I've tried changing the xml string file name. It is currently values-es. I've tried changing the bundle language enableSplit to false. These did not work what can I do differently?

Share Improve this question edited Feb 7 at 9:17 Mark Rotteveel 109k226 gold badges155 silver badges219 bronze badges asked Feb 7 at 4:55 Jeffrey LimaJeffrey Lima 1 New contributor Jeffrey Lima is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 0

From SDK 33 (Tiramisu), instead of using updateConfiguration the recommended way to change language is to use setApplicationLocales():

        LocaleListCompat aLocale = LocaleListCompat.forLanguageTags(locale.getLanguage());
        AppCompatDelegate.setApplicationLocales(aLocale);

It is worth to note that: This API should always be called after Activity.onCreate(), apart from any exceptions explicitly mentioned in this documentation.

Reference: setApplicationLocales

发布评论

评论列表(0)

  1. 暂无评论