繁体   English   中英

发布后更改 android 应用程序中的语言

[英]Change language in android application after publication

加载到游戏市场后,无法更改语言,尽管在设备和模拟器上一切正常。 我得出的结论是,谷歌仅在更改系统语言时才会阻止语言并重新加载,这是原因吗? 如果是这样,您如何手动查询或绕过它们?

负责这个的 class 本身看起来像这样:

public class MyContextWrapper extends ContextWrapper {

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

@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context, String language) {
    Configuration config = context.getResources().getConfiguration();
    Locale sysLocale = null;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
        sysLocale = getSystemLocale(config);
    } else {
        sysLocale = getSystemLocaleLegacy(config);
    }
    if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            setSystemLocale(config, locale);
        } else {
            setSystemLocaleLegacy(config, locale);
        }
    }
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    Language.setLang(language);
    return new MyContextWrapper(context);
}

@SuppressWarnings("deprecation")
public static Locale getSystemLocaleLegacy(Configuration config) {
    return config.locale;
}

@TargetApi(Build.VERSION_CODES.N)
public static Locale getSystemLocale(Configuration config) {
    return config.getLocales().get(0);
}

@SuppressWarnings("deprecation")
public static void setSystemLocaleLegacy(Configuration config, Locale locale) {
    config.locale = locale;
}

@TargetApi(Build.VERSION_CODES.N)
public static void setSystemLocale(Configuration config, Locale locale) {
    config.setLocale(locale);
}

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(MyContextWrapper.wrap(newBase, Language.getLang()));
}

如果您提供捆绑包而不是 apk,请将此代码块添加到应用程序build.gradle

android {
 ...
 bundle {
        language {
            enableSplit = false
        }
    }
}

请参阅重新启用或禁用配置 APK 的类型

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM