Firebase云功能中的错误本地化



在Firebase Cloud函数中执行时,以下命令返回美国格式而不是本地化的格式。但是,它在浏览器中效果很好。

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

有什么办法在Firebase Cloud功能中正确起作用?

更新:

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

也许它与节点的默认ICU有关(-with-intl = small-iCu(。为了支持国际化,似乎应该为 - intl = full-icu。

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

<️> intl 似乎不再有效/支持(最后一个版本是4yo ...(。

我已经实现了添加真实INTL如下:

  1. 添加full-icu依赖性对您的firebase云功能: npm install full-icu --save
  2. 编辑您的功能(成功部署之后(在Console.cloud.google.com上(别忘了选择正确的Google Dev Project(
  3. 添加 NODE_ICU_DATA env var带有值 node_modules/full-icu
  4. 保存并等待1或2分钟以部署功能。

Furter Update不会删除此ENV,我能够成功地使用Luxon的INTL API。

您不应依靠特殊标志来构建云功能中使用的节点的版本。相反,您可以做的是拉动一个用于处理格式化语言环境字符串的模块。例如,您可以使用INTL模块:

npm install intl

用途:

const intl = require('intl')
const format = intl.NumberFormat("pt-BR", {maximumFractionDigits: 2})
console.log(format.format(price))

最新更新