【问题标题】:Changing locale programmatically not working on Android 6.0.1以编程方式更改语言环境不适用于 Android 6.0.1
【发布时间】:2019-05-20 09:46:36
【问题描述】:

我在将语言更改为 Android 6.0.1 时遇到问题,在新的 Android 版本上更改语言效果很好,但在 6.0.1 上设置默认字符串,无论设备设置的语言如何。模拟器可以工作,但是当我在三星 J5 上安装 apk 时,它可以在 Android 6.0.1 上运行,更改语言不起作用。我的问题有什么解决办法吗?谢谢。

 private void setLocale(String lang) {
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration config = 
    getBaseContext().getResources().getConfiguration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

} else {
    Resources resources = getBaseContext().getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.setLocale(new Locale(lang));
    getBaseContext().getApplicationContext().createConfigurationContext(configuration);
}

// shared pref.
SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
editor.putString("My_Lang", lang);
editor.apply();

}

【问题讨论】:

    标签: java android


    【解决方案1】:

    这对我不起作用。我尝试了所有解决方案,但无法在 Android 6.0.1 上运行

    我正在使用 appCompat androidx.appcompat:appcompat:1.1.0

    请阅读此https://stackoverflow.com/a/58004553/3452078

    我使用了下面的代码,它在所有版本上都能完美运行

    @Override
    public void applyOverrideConfiguration(Configuration overrideConfiguration) {
        if (overrideConfiguration != null) {
            int uiMode = overrideConfiguration.uiMode;
            overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
            overrideConfiguration.uiMode = uiMode;
        }
        super.applyOverrideConfiguration(overrideConfiguration);
    }
    

    【讨论】:

      【解决方案2】:

      如果您使用 aap 分发您的应用,请确保所有语言字符串都捆绑在用户从 Google Play 下载的 apk 中。

      您可以通过添加以下内容来更新您的应用 build.gradle:

      android {
        bundle {
           language {
           // Specifies that the app bundle should not support
           // configuration APKs for language resources. These
           // resources are instead packaged with each base and
           // dynamic feature APK.
           enableSplit = false
           }
        }
      }
      

      此答案基于https://stackoverflow.com/a/54862243/3296947

      【讨论】:

        【解决方案3】:
        【解决方案4】:

        因为我设法编辑了我的代码,所以要结束这个话题,也许有人需要它。

        编辑

        private void setLocale(String lang) {
            Locale locale = new Locale(lang);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            if (Build.VERSION.SDK_INT >= 24) {
                config.setLocale(locale);
                getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
            } else {
                config.locale = locale;
                getBaseContext().getApplicationContext().createConfigurationContext(config);
            }
        
            // shared pref.
            SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
            editor.putString("My_Lang", lang);
            editor.apply();
        }
        

        【讨论】:

        • updateConfiguration 也不推荐用于 android >=N
        【解决方案5】:

        2021 年 3 月

        我使用了下面的代码,它在所有版本上都能完美运行。

        完整代码:

        注意: 对于 6.0.1 版本必须在 setContentView(R.layout.activity_main) 之前更新语言环境,对于 7.0.0 的更高版本(API 级别 >24)需要在项目的所有 Activity 中覆盖 protected void attachBaseContext(Context newBase) .

        MainActivity

         @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
        
                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
                    ContextUtils.updateLocale(MainActivity.this, ContextUtils.getSavedLanguage(MainActivity.this));
                }
        
                setContentView(R.layout.activity_main);
                ......
                .....
        }
        
        @Override
            protected void attachBaseContext(Context newBase) {
                String localeToSwitchTo= ContextUtils.getSavedLanguage(newBase);
                ContextWrapper localeUpdatedContext = ContextUtils.updateLocale(newBase, localeToSwitchTo);
                super.attachBaseContext(localeUpdatedContext);
            }
        

        ContextUtils

        public class ContextUtils  extends ContextWrapper {
        
            public ContextUtils(Context base) {
                super(base);
            }
        
            public static ContextWrapper updateLocale(Context context, String lang) {
        
                Resources resources = context.getResources();
                Configuration configuration = resources.getConfiguration();
        
                Locale localeToSwitchTo = new Locale(lang);
        
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        
                    configuration.setLocale(localeToSwitchTo);
        
                    LocaleList localeList = new LocaleList(localeToSwitchTo);
                    LocaleList.setDefault(localeList);
                    configuration.setLocales(localeList);
                    context = context.createConfigurationContext(configuration);
        
                }else {
        
                    Locale.setDefault(localeToSwitchTo);
                    configuration.locale=localeToSwitchTo;
                    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
                }
        
                return new ContextUtils(context);
            }
        
            public static String getSavedLanguage(Context context) {
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
                        context.getApplicationContext());
                return preferences.getString("SETTINGS_LANG", "en");
            }
        
            public static void setSavedLanguage(Context context, String lang){
        
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
                        context.getApplicationContext());
        
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("SETTINGS_LANG", lang);
                editor.apply();
        
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-19
          • 2013-02-26
          • 2011-06-26
          相关资源
          最近更新 更多