在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
您不应依赖特殊标志来构建Cloud Functions中使用的节点版本。相反,您可以做的是引入处理格式化语言环境字符串的模块。例如,您可以使用intl模块:
npm install intl
使用此:
const intl = require('intl')
const format = intl.NumberFormat("pt-BR", {maximumFractionDigits: 2})
console.log(format.format(price))
intl似乎不受支持(最新版本为2yo ...)。
我已经实现了如下添加真实的intl:1.在Firebase Cloud Functions上添加full-icu
依赖项:npm install full-icu --save
2. Edit在成功部署后在console.cloud.google.com上运行(不要忘记选择正确的google dev项目)3.将NODE_ICU_DATA
env var与值node_modules/full-icu
相加4.保存并等待1或2分钟以部署功能。
进一步的更新不会删除此环境,因此我能够成功地使用Luxon的Intl api。