具有应用内区域设置更改的Android App Bundle

Muh*_*aat 22 android locale android-app-bundle

当我需要从应用程序本身更改应用程序区域设置时(即在应用程序内部具有语言更改设置),我遇到AAB问题,问题是AAB只给我设备语言资源,例如:

我的设备中安装了英语和法语,所以AAb只给我英语和法语的资源,

但是从应用程序本身可以选择在英语,法语和印尼语之间切换语言,

在这种情况下,当将语言更改为英语或法语时,一切都运行良好,但是当将其更改为印度尼西亚语时,应用程序只需进入崩溃循环,因为它一直在寻找印尼语,但却无法找到.

这里的问题是,即使我重新启动应用程序,它再次进入崩溃循环,因为应用程序仍在寻找缺少的语言资源,这里唯一的解决方案是清理现金或重新安装哪些是普通用户赢得的解决方案不去.


只是提一下,这是我通过应用程序更改语言环境的方式:

    // get resources
    Resources res = context.getResources();
    // create the corresponding locale
    Locale locale = new Locale(language); // for example "en"
    // Change locale settings in the app.
    android.content.res.Configuration conf = res.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        conf.setLocale(locale);
        conf.setLayoutDirection(locale);
    } else {
        conf.locale = locale;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        context.getApplicationContext().createConfigurationContext(conf);
    }
    res.updateConfiguration(conf, null);
Run Code Online (Sandbox Code Playgroud)

PS该应用程序在将其构建为APK时工作正常.

Pie*_*rre 26

目前,您可以通过在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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

即将推出更好的解决方


Man*_*ddy 8

可以在此处找到按需下载语言的详细信息

\n

https://android-developers.googleblog.com/2019/03/the-latest-android-app-bundle-updates.html

\n

在您的 app\xe2\x80\x99sbuild.gradle文件中:

\n
dependencies {\n    // This dependency is downloaded from the Google\xe2\x80\x99s Maven repository.\n    // So, make sure you also include that repository in your project\'s build.gradle file.\n    implementation \'com.google.android.play:core:1.10.0\'\n\n    // For Kotlin users also add the Kotlin extensions library for Play Core:\n    implementation \'com.google.android.play:core-ktx:1.8.1\'\n    ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n

获取已安装语言的列表

\n
val splitInstallManager = SplitInstallManagerFactory.create(context)\nval langs: Set<String> = splitInstallManager.installedLanguages\n
Run Code Online (Sandbox Code Playgroud)\n

请求其他语言

\n
val installRequestBuilder = SplitInstallRequest.newBuilder()\ninstallRequestBuilder.addLanguage(Locale.forLanguageTag("pl"))\nsplitInstallManager.startInstall(installRequestBuilder.build())\n
Run Code Online (Sandbox Code Playgroud)\n

检查上面的链接以获取完整的详细信息

\n


beg*_*ner 7

经过几个小时后,我终于能够通过新的 PlayCore API 使用点播语言了。

步骤1.)当用户更改语言时,您需要首先检查该语言是否已经可用,如果没有则下载该语言

private void changeLocale(final String languageSelected){
  SplitInstallManager splitInstallManager = SplitInstallManagerFactory.create(PlayAgainstComputer.this);
    final Set<String> installedLangs = splitInstallManager.getInstalledLanguages();
    if(installedLangs.contains(languageSelected)){ // checking if lang already available
        Toast.makeText(PlayAgainstComputer.this,"Done! The language settings will take effect, once you restart the app!").show();
    }
    else{
        SplitInstallRequest request =
                SplitInstallRequest.newBuilder()
                        .addLanguage(Locale.forLanguageTag(languageSelected))
                        .build();
        splitInstallManager.startInstall(request);
        splitInstallManager.registerListener(new SplitInstallStateUpdatedListener() {
        @Override
        public void onStateUpdate(@NonNull SplitInstallSessionState splitInstallSessionState) {
            if(splitInstallSessionState.status() == SplitInstallSessionStatus.INSTALLED){
                Toast.makeText(PlayAgainstComputer.this,"Download complete! The language settings will take effect, once you restart the app!").show();
            }
        }
    });
 }}
Run Code Online (Sandbox Code Playgroud)

步骤2.)用户启动应用程序时必须安装下载的语言。这是在attchBaseContext()方法中完成的

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    SplitCompat.install(this); // It will install all the downloaded langauges into the app
  }
Run Code Online (Sandbox Code Playgroud)

步骤 3.)您需要告诉 Activity 使用所选语言。以下代码应放置在setContentView(R.layout.layout);该活动之前

  String selectedLanguage = getFromPrefernceOrWhereEverYouSavedIt(); // should be 2 letters. like "de", "es"
  Locale locale = new Locale(selectedLanguage); 
  Locale.setDefault(locale);
  Resources resources = getResources();
  Configuration config = new Configuration(resources.getConfiguration());
  config.locale = locale;
  resources.updateConfiguration(config,
        getBaseContext().getResources().getDisplayMetrics());
Run Code Online (Sandbox Code Playgroud)

完毕!

请注意

当用户(选择非默认/下载的语言)更新应用程序时,需要将该语言再次下载到应用程序中,因此请确保在代码中处理该问题。

当我在下载完成后使用activity.recreate();(自动刷新应用程序以获取新语言)时,我遇到了一些问题,这就是为什么我过去常常Toast要求用户手动重新启动应用程序。但你可以尝试其他方法

我还注意到此方法存在其他一些不一致之处(甚至有时会因为 原因而面临内存泄漏 SplitCompat.install(this);),因此请确保根据您的代码对其进行测试和优化。


ian*_*ake 5

这对于应用程序包是不可能的:Google Play 仅在设备选择的语言发生变化时下载资源。

如果您想拥有应用内语言选择器,则必须使用 APK。

  • 如果设备离线并且用户更改区域设置会发生什么?会崩溃吗?该应用程序是否提供默认界面(英文)。下载正确的语言环境将被安排到下一次连接? (4认同)