【问题标题】:How to change the whole application language? [duplicate]如何更改整个应用程序语言? [复制]
【发布时间】:2018-08-31 14:00:08
【问题描述】:

我正在制作一个应用程序,我希望在其中有一个显示语言选择页面的页面。到目前为止,我已经包括了英语、印地语和马拉地语,英语设置为默认值。

我的问题是:

  1. 如何在所选语言中更改整个应用程序语言?

  2. 在我重新打开应用程序时选择语言后,它会提供以前选择的语言吗?

【问题讨论】:

标签: android android-layout android-fragments


【解决方案1】:

将所有文本放入字符串文件中。为每种语言创建单独的字符串文件(Deutsch values-de/strings.xml,French values-fr/strings.xml) 并且您需要更改语言调用以下功能。为英文集"en"为另一集对应键

#Kotlin

val config = resources.configuration
val locale = Locale("en")
Locale.setDefault(locale)
config.locale = locale
resources.updateConfiguration(config, resources.displayMetrics)

#Android Java

Configuration config = getBaseContext().getResources().getConfiguration();
 Locale locale = new Locale("en");
 Locale.setDefault(locale);
 config.locale = locale;
 getBaseContext().getResources().updateConfiguration(config, 
            getBaseContext().getResources().getDisplayMetrics());

【讨论】:

    【解决方案2】:
     String lang= "en";
    
     public void changeLang(String lang) {
            Configuration config = getBaseContext().getResources().getConfiguration();
            if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) {
    
                locale = new Locale(lang);
                Locale.setDefault(locale);
                Configuration conf = new Configuration(config);
                conf.locale = locale;
                getBaseContext().getResources().updateConfiguration(conf, getBaseContext().getResources().getDisplayMetrics());
            }
        }
    

    试试这个方法..这绝对行得通.. 当您选择首选语言时,在此方法中传递您选择的语言代码,这将更改整个应用程序的语言。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      • 2017-09-03
      • 1970-01-01
      • 2017-05-09
      • 2015-05-09
      • 2013-08-08
      • 2021-04-13
      相关资源
      最近更新 更多