Firebase Cloud功能中的本地化错误 [英] Wrong localisation in Firebase Cloud Function

查看:56
本文介绍了Firebase Cloud功能中的本地化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在firebase云功能中执行时,以下命令将返回美国格式,而不是本地化格式. 但是,它在浏览器中效果很好.

When executed in firebase cloud functions, the following command returns the American format instead of the localised one. However, it works well in browsers.

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

toLocaleString()在Firebase云函数中是否可以正常工作?

Is there any way toLocaleString() works properly in firebase cloud functions ?

更新:

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.

Maybe it something related to the default ICU of Node (--with-intl=small-icu) . To support internationalization, it seems the value should be --with-intl=full-icu .

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

推荐答案

您不应依赖特殊标志来构建Cloud Functions中使用的节点版本.相反,您可以做的是引入处理格式化语言环境字符串的模块.例如,您可以使用 intl 模块:

You shouldn't depend on special flags for building the version of node used in 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:

npm install intl

使用此:

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

这篇关于Firebase Cloud功能中的本地化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆