Android 根据设备语言设置应用语言

Android set app language according the device language

我有一个应用程序,如果用户的设备语言设置为斯洛伐克语,则应用程序语言为斯洛伐克语,否则将为 EN。所以语言在应用程序中没有变化,而是来自安装 SK 或 EN。

 String languageToLoad  = Resources.getSystem().getConfiguration().locale.getLanguage();
 String lng = "sk".equals(languageToLoad) ? "sk" : "en";

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

我注意到,配置中的语言环境已被弃用,资源中的 updateConfiguration(Configuration, DisplayMetrics) 也已被弃用 android N(我认为)。

然而,出于某种原因,它仍然适用于更高的 Android 版本,但 Android studio 表示它已被弃用。

所以我试着把代码分开:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        String languageToLoad  = Resources.getSystem().getConfiguration().getLocales().get(0).getLanguage();
        String lng = "sk".equals(languageToLoad) ? "sk" : "en";
        Locale locale = new Locale(lng);Locale.setDefault(locale);
        Configuration config = new Configuration();config.setLocale(locale);
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

    } else {
  String languageToLoad  = Resources.getSystem().getConfiguration().locale.getLanguage();
  String lng = "sk".equals(languageToLoad) ? "sk" : "en";

  Locale locale = new Locale(lng);Locale.setDefault(locale);
  Configuration config = new Configuration();config.locale = locale;
  getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}

但是第一个条件(>=N)不对,我不知道如何替换当前代码。它仍然说 updateConfiguration 已弃用。

即使我尝试 getBaseContext().createConfigurationContext(config); 它也不会改变语言

当API>=N时,替换为:

getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

通过这个:

context.createConfigurationContext(config);

但是,您必须创建自定义上下文包装器,并确保 activity 已重新启动。查看下一个已接受的答案:

好的,所以我是这样解决的:

在单独的 java 文件中创建了一个 ContextWrapper:

public class ContextWrapper extends android.content.ContextWrapper {

public ContextWrapper(Context base) {
    super(base);
}

public static ContextWrapper wrap(Context context, Locale newLocale) {
    Resources res = context.getResources();
    Configuration configuration = res.getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        configuration.setLocale(newLocale);

        LocaleList localeList = new LocaleList(newLocale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);

        context = context.createConfigurationContext(configuration);

    }  else {
        configuration.locale = newLocale;
        res.updateConfiguration(configuration, res.getDisplayMetrics());
    }

    return new ContextWrapper(context);
}

}

然后在我的 MainActivity 中我这样使用它:

     @Override
       protected void attachBaseContext(Context newBase) {

        String languageToLoad;
        String lng;
        Locale locale;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
             languageToLoad = Resources.getSystem().getConfiguration().getLocales().get(0).getLanguage();
        }
else {  languageToLoad  = Resources.getSystem().getConfiguration().locale.getLanguage();
        }

         lng = "sk".equals(languageToLoad) ? "en" : "sk";
         locale = new Locale(lng);Locale.setDefault(locale);

        Context context = ContextWrapper.wrap(newBase, locale);
        super.attachBaseContext(context);
    }

现在看起来工作正常