简体   繁体   English

Firebase Cloud Function 中的本地化错误

[英]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,如下所示:

  1. Add full-icu dependencies on your Firebase Cloud Functions: npm install full-icu --save在您的 Firebase Cloud Functions 上添加full-icu依赖项: npm install full-icu --save
  2. Edit you functions (after a successful deployment) on console.cloud.google.com (don't forget to select the right google dev project)console.cloud.google.com编辑你的功能(在成功部署后)(不要忘记选择正确的谷歌开发项目)
  3. Add NODE_ICU_DATA env var with value node_modules/full-icu添加值为node_modules/full-icu NODE_ICU_DATA env var
  4. Save and wait 1 or 2 min for the functions to be deploy.保存并等待 1 或 2 分钟以部署功能。

Furter 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM