【问题标题】:Using Intl properly in Android React Native app在 Android React Native 应用程序中正确使用 Intl
【发布时间】:2019-11-18 11:50:54
【问题描述】:

我正在尝试使用 Intl 的数字格式化程序,它在 iOS 上以及当调试器附加到 iOS 或 Android 时完美运行,但由于 Android 中的 JSC 过时,仅在没有附加调试器的情况下在 Android 上失败。

经过一番研究,我发现了两种可能的解决方案:

  • 使用Intl polyfill
  • 在 Android 中使用自定义 JSC

在使用 yarn 安装 intlreact-intl 之后,我首先尝试了 Intl polyfill:

//in my app's index.js
if (!global.Intl) {
    global.Intl = require('intl');
}

虽然它仍然写着ReferenceError: Can't find variable: Intl

然后我放弃并尝试包含自定义 JSC(我已确认正确引用了自定义 AAR),但我仍然遇到相同的错误。无论我做什么,我都无法让Intl 在没有附加调试器的情况下在 Android 上运行。

我做错了什么? (我正在使用 React Native 0.59.9)

【问题讨论】:

    标签: android react-native polyfills javascriptcore react-intl


    【解决方案1】:

    小更新,从 React Native v0.60+ 起,您可以定义 lib 的 intl+ 版本。

    在你的app/build.gradle:

    def jscFlavor = 'org.webkit:android-jsc-intl:+'

    并在您的依赖项中实现它:

    dependencies {
        ...
        if (enableHermes) {
            ...
        } else {
            implementation jscFlavor
        }
    }
    

    【讨论】:

    • 谢谢!这是一个救命稻草,也是最简单的解决方案。
    • 我在其他地方看到了这个解决方案,但他们没有提到这个依赖步骤。为什么?
    • @HenriqueBruno 对我来说,依赖步骤已经存在。 jscFlavor已经存在,这只是改变值
    • @Simon 在此之后我遇到了语义错误。
    • 警告这会使您的应用程序大小增加大约 25 MB,这是我的情况,需要找出其他方法。
    【解决方案2】:

    如果禁用 Hermes 不是您的 app/build.gradle 中的选项,并且您不能使用 org.webkit:android-jsc-intl:+,那么 Intl polyfill 可以解决此问题:

    npm install intl
    

    在代码中:

    import 'intl';
    import 'intl/locale-data/jsonp/en'; // or any other locale you need
    

    【讨论】:

    • 这只能在android上做吗?例如if (Platform.android) { require('intl').default }
    【解决方案3】:

    如果您使用的是自定义 JSC,请不要忘记将国际版本导入为 explained in the read me of JSC Android Buildscripts

    对于 React Native 0.59 版,将原始工件 id 替换为 android-jsc-intl

    dependencies {
    +   // Make sure to put android-jsc at the the first
    +   implementation "org.webkit:android-jsc-intl:r241213"
    +
        compile fileTree(dir: "libs", include: ["*.jar"])
        implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
        implementation "com.facebook.react:react-native:+"  // From node_modules
    }
    

    【讨论】:

      【解决方案4】:
      npm i intl
      

      导入之后

      import "intl";
      
      import "intl/locale-data/jsonp/en";
      

      然后使用Intl

      例如:-

      {new Intl.NumberFormat("en-IN", {
                        style: "currency",
                        currency: "INR",
                      }).format(
                        (travelTimeInformation?.duration.value *
                          SURGE_CHARGE_RATE *
                          multiplier) /
                          100
                      )}
      

      【讨论】:

        【解决方案5】:

        我想删除这个问题,但我不能,因为已经有另一个答案了。


        它与 JSC 无关。我的构建脚本没有正确更新 APK,而我认为它是,我一遍又一遍地尝试旧的伪造 APK。否则,显然我做的一切都正常,因为它现在可以使用新的 APK。

        【讨论】:

          猜你喜欢
          • 2022-01-11
          • 2021-12-12
          • 2016-04-17
          • 1970-01-01
          • 1970-01-01
          • 2018-12-31
          • 1970-01-01
          • 2021-11-10
          • 1970-01-01
          相关资源
          最近更新 更多