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
});
Is there any way toLocaleString() works properly in firebase cloud functions ?
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) . 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
⚠️ intl seems no longer active/supported (last version is 4yo...).
I've achieved adding the real intl as follow:
full-icu
dependencies on your Firebase Cloud Functions:
npm install full-icu --save
NODE_ICU_DATA
env var with value node_modules/full-icu
Furter update will not remove this env, and I was able to use Intl apis from Luxon successfully.
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
The use this:
const intl = require('intl')
const format = intl.NumberFormat("pt-BR", {maximumFractionDigits: 2})
console.log(format.format(price))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With