【问题标题】:Why can the Application language be changed only once? [duplicate]为什么应用程序语言只能更改一次? [复制]
【发布时间】:2021-05-16 18:08:48
【问题描述】:

我正在尝试更改基于 Java 的 Android 中的应用语言。 这是函数:

public void setLocale(String lang) {
    Locale locale = new Locale(lang);
    Configuration config = new Configuration();
    
    Locale.setDefault(locale);
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, null);
    finish();
    startActivity(getIntent());
}

此功能来自另一个问题,例如“如何更改应用程序语言”,但它只能使用一次(例如从英语到希伯来语)。 这不是一个重复的问题
你能帮帮我吗?

【问题讨论】:

  • 这能回答你的问题吗? Change app language programmatically in Android
  • 更改语言并将languageValues设置为布尔类型的true。现在每次活动午餐时检查languageValue。
  • @LeoPelozo 这不是重复的,因为我这样做的方式(如您链接的问题的答案)有效但不是 100% 的应用程序

标签: java android


【解决方案1】:

用这个替换你的函数:

public void setLocale(String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale); // changes the languages
    Resources resources = getBaseContext().getResources();
    Configuration config = resources.getConfiguration();
    config.setLocale(locale);
    resources.updateConfiguration(config, resources.getDisplayMetrics());
    this.recreate(); // re-launches the app
}

【讨论】:

    猜你喜欢
    • 2023-02-09
    • 1970-01-01
    • 2018-08-31
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多