代码之家  ›  专栏  ›  技术社区  ›  Muhammed Refaat

Android应用程序捆绑包和应用程序内语言环境更改

  •  10
  • Muhammed Refaat  · 技术社区  · 7 年前

    当我需要在应用程序本身内更改应用程序区域设置(即在应用程序内设置语言更改)时,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);
    

    顺便说一句,这个应用程序是完美的工作时,建立它作为APK。

    2 回复  |  直到 7 年前
        1
  •  62
  •   Pierre    7 年前

    编辑:

    这个 现在支持按需下载其他语言的字符串: https://developer.android.com/guide/app-bundle/playcore#lang_resources

    替代解决方案(不鼓励):

    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
            }
        }
    }
    

    后一种解决方案将增加应用程序的大小。

        2
  •  4
  •   ianhanniballake    7 年前

    这在应用程序包中是不可能的:googleplay只在设备所选语言更改时下载资源。

    如果你想有一个应用内语言选取器,你必须使用APKs。

    推荐文章