node.js - Firebase Cloud Function 中的本地化错误

标签 node.js firebase google-cloud-functions

在 firebase 云函数中执行时,以下命令返回美国格式而不是本地化格式。 但是,它在浏览器中运行良好。

price.toLocaleString("pt-BR", {
  maximumFractionDigits: 2
});

toLocaleString() 有什么方法可以在 firebase 云函数中正常工作吗?

更新:

let price = 1456.21
let formattedPrice = price.toLocaleString("pt-BR", {maximumFractionDigits: 2});

//Value of formattedPrice expected: 1.456,21 (it works in browsers).
//Value of formattedPrice returned in Firebase Cloud Functions: 1,456.21

可能与 Node 的默认 ICU (--with-intl=small-icu) 有关。为了支持国际化,这个值似乎应该是 --with-intl=full-icu 。

https://nodejs.org/api/intl.html#intl_options_for_building_node_js

最佳答案

⚠️ intl 似乎不再有效/不受支持(最新版本是 4yo...)。

我已经实现了添加真正的 intl,如下所示:

  1. 在您的 Firebase 云函数上添加 full-icu 依赖项: npm install full-icu --save
  2. 编辑您在 console.cloud.google.com 上的功能(在成功部署后) (不要忘记选择正确的谷歌开发项目)
  3. 添加 NODE_ICU_DATA 环境变量,值为 node_modules/full-icu
  4. 保存并等待 1 或 2 分钟以部署功能。

以后的更新不会删除这个环境,我能够成功地使用来自 Luxon 的 Intl api。

关于node.js - Firebase Cloud Function 中的本地化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49012433/

相关文章:

node.js - Mocha 测试执行时间太长

javascript - Angular Firebase 通过订阅监听 foreach 变化

javascript - 从 Cloud Functions 和 Firebase 获取当前用户的 ID

mysql - nodejs 数据库迁移耗尽堆空间

node.js - Express:是否可以简化res.sendFile中的路径?

firebase - Flutter & Firestore - 更新永不停止(从一个值切换到另一个值)

Firebase:如果验证了用户电子邮件,则通过云功能进行检查

javascript - 如何使用 Google Cloud Function 高效删除旧的 Firebase Cloud 文档?

node.js - Mocha测试说express结果不是json。 json 验证说它是

ios - React Native [[DEFAULT]] firebaseapp 未初始化react-native-firebase