【问题标题】:Android App Locale Not Working on Play Store ReleaseAndroid 应用程序区域设置不适用于 Play 商店版本
【发布时间】:2021-07-08 18:04:29
【问题描述】:

我只需按一下按钮即可更改应用程序区域设置。它在 AVD 以及使用 API 30 的实际设备上调试和发布构建 APK 上完美运行。

但是,一旦发布,它就不适用于应用程序的 Play 商店版本。语言环境永远不会改变。

请帮忙!谢谢!

这是 SettingsFragment 中的代码:

private void setAppLocale(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = context.getResources().getConfiguration();
    config.setLocale(locale);
    context.createConfigurationContext(config);
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    Intent refresh = new Intent(getActivity().getApplicationContext(), MainActivity.class);
    startActivity(refresh);
    getActivity().finish();
}

一旦按下按钮并且选择被放入共享首选项中,就会调用上述内容。 Activity 被刷新并加载主要 Activity,但区域设置从未改变。

这就是我的 MainActivity 的样子:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    hideSystemUI();

    sharedPref = getPreferences(Context.MODE_PRIVATE);
    selectedLanguage = sharedPref.getString("Test.SL.LanguageName", language);
    selectedTheme = sharedPref.getString("Test.SL.ThemeName", "Light");

    if (selectedTheme.equals("Light")){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    } else if (selectedTheme.equals("Dark")) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }

    if (selectedLanguage.equals("Sinhala")) {
        language = "Sinhala";
        setAppLocale(this, "si");
    } else {
        language = "English";
        setAppLocale(this, "en");
    }

    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    setSupportActionBar(binding.appBarMain.toolbar);
    
       ......

}

public void setAppLocale(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = context.getResources().getConfiguration();
    config.setLocale(locale);
    context.createConfigurationContext(config);
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}

请有任何想法、建议和解决方案!再次感谢!

【问题讨论】:

    标签: java android android-studio locale


    【解决方案1】:

    更新新的 App Bundle 格式

    Google 引入了一种新的 App Bundle 格式,可在将安装在客户端设备上的 apk 文件拆分为更小的尺寸。但是,这意味着我们不能在应用程序中进行动态语言更改。

    为了防止这种拆分,我们需要在 app 文件夹内的 build.gradle 文件中添加额外的行,如下所示。

    android {
        //...
        //... removed for brevity
        bundle {
            language {
                enableSplit = false
            }
        }
    }
    

    Refer to this post

    【讨论】:

    • 这就像一个魅力!非常感谢!我不知道 AAB 会阻止动态语言更改。再次非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 2019-07-20
    • 2020-10-19
    相关资源
    最近更新 更多