在Android 6.0.1上以编程方式更改语言环境无效。

3

我有一个问题,无法更改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();

}
5个回答

13

这对我没用。我尝试了所有的解决方案,但在Android 6.0.1上无法工作。

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

请阅读这个https://dev59.com/HFMI5IYBdhLWcg3w0e97#58004553

我使用了以下代码,并且它在所有版本上都可以完美地工作。

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

3
如果您正在使用分发您的应用程序,请确保保证所有语言字符串都打包在用户从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://dev59.com/EFQI5IYBdhLWcg3w-Qz_#54862243

1

2022年1月更新

新方法:

使用 AndroidX 库设置区域设置,对于7.0.0(API 级别 >24)以上版本,该库将自行处理存储 - PerAppLanguages

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();

    }

}

setContentView之前更新本地化设置 - 这对我很有帮助!谢谢 :) - SerjantArbuz


1

由于我成功地编辑了我的代码,因此关闭这个主题,也许有人需要它。

编辑

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();
}

1
对于 Android 版本 >= N,updateConfiguration 也已被弃用。 - Darksymphony

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接