handlebars-intl
handlebars-intl copied to clipboard
Why am I getting (US$) and not ($)
I'm using
"email-templates": "^2.5.4",
"handlebars": "^4.0.6",
"handlebars-intl": "^1.1.2",
"handlebars-layouts": "^3.1.4"
This is how I'm formatting
{{formatNumber price style="currency" currency="USD" locales="en-US"}}
And here's my js file
const handlebars = require('handlebars');
const handlebarsIntl = require('handlebars-intl');
handlebarsIntl.registerWith(handlebars);
handlebars.registerHelper(layouts(handlebars));
handlebars.registerPartial('layout', fs.readFileSync(`public/email-template/layout.hbs`, 'utf8'));
var intlData = {
locales: 'en-US'
// I also tried to follow this: https://formatjs.io/handlebars/ (Using Named Number Formats)
};
var newsletter = new EmailTemplate(path, {
data: { intl: intlData }
});
handlebars.registerPartial('data', { intl: intlData });
newsletter.render({ params: { ... }, data: { intl: intlData } }, function (err, result) {
if(err) { return reject(err); }
return resolve(result.html);
});
Everything that I tried, or nothing happens or I got this error
ReferenceError: Could not find Intl object: formats.number.USD at intlGet (E:\www\unclehub\v0.2.2\api\node_modules\handlebars-intl\lib\helpers.js:96:23)
@rochapablo did you have any luck solving this? I'm having the same issue.
@gregholland, nop. I kind give up for now. I'm working in other areas and I'll let this for later.
Honestly I don't think this issue has to do with this lib. Under the hood it uses the browsers intl API which is what is using US$ instead of $. So likely in testing this is the browser using a different locale than en-US. You can see in the polyfill what I mean: https://github.com/andyearnshaw/Intl.js/search?p=19&q=%22US%24%22&type=&utf8=%E2%9C%93
Oh and the formats part of the intl file is mostly for shorthand.. so instead of typing {{formatNumber fooNum style="currency" currency="USD"}} with a format you can just do {{formatNumber fooNum "bar"}} where
var intlData = {
"locales": "en-US",
"formats": {
"number": {
"bar": {
"style": "currency",
"currency": "USD"
}
}
}
};
I honestly can't find good reason to use them.. seems hard to maintain.
I was able to solve the problem using singular 'locale' form:
{{formatNumber price style="currency" currency="USD" locale="en-US"}}
Need to use currencyDisplay: 'narrowSymbol' to display $ instead of US$ . Please refer https://stackoverflow.com/questions/52410407/intl-numberformat-currency-us-currency-symbol