[英]Wrong localisation in Firebase Cloud Function
When executed in firebase cloud functions, the following command returns the American format instead of the localised one.在 firebase 云函数中执行时,以下命令返回美国格式而不是本地化格式。 However, it works well in browsers.但是,它在浏览器中运行良好。
price.toLocaleString("pt-BR", {
maximumFractionDigits: 2
});
Is there any way toLocaleString() works properly in firebase cloud functions?有什么方法可以让 toLocaleString() 在 firebase 云函数中正常工作吗?
Update:更新:
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
Maybe it something related to the default ICU of Node (--with-intl=small-icu).也许它与 Node 的默认 ICU (--with-intl=small-icu) 有关。 To support internationalization, it seems the value should be --with-intl=full-icu.为了支持国际化,这个值似乎应该是--with-intl=full-icu。
https://nodejs.org/api/intl.html#intl_options_for_building_node_js https://nodejs.org/api/intl.html#intl_options_for_building_node_js
⚠️ intl seems no longer active/supported (last version is 4yo...). ⚠️国际似乎不再活跃/支持(最新版本是 4yo...)。
I've achieved adding the real intl as follow:我已经实现了添加真正的 intl,如下所示:
full-icu
dependencies on your Firebase Cloud Functions: npm install full-icu --save
在您的 Firebase Cloud Functions 上添加full-icu
依赖项: npm install full-icu --save
NODE_ICU_DATA
env var with value node_modules/full-icu
添加值为node_modules/full-icu
NODE_ICU_DATA
env varFurter update will not remove this env, and I was able to use Intl apis from Luxon successfully.进一步的更新不会删除这个环境,我能够成功地使用来自 Luxon 的 Intl api。
You shouldn't depend on special flags for building the version of node used in Cloud Functions.您不应该依赖特殊标志来构建 Cloud Functions 中使用的节点版本。 What you can do instead is pull in a module that deals with formatting locale strings.您可以做的是引入一个处理格式化语言环境字符串的模块。 For example, you can use the intl module:例如,您可以使用intl模块:
npm install intl
The use this:使用这个:
const intl = require('intl')
const format = intl.NumberFormat("pt-BR", {maximumFractionDigits: 2})
console.log(format.format(price))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.