Skip to content

Commit

Permalink
Add "localizePrice" helper
Browse files Browse the repository at this point in the history
  • Loading branch information
bookernath committed Nov 23, 2021
1 parent d2a0b59 commit 86fcff1
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
15 changes: 15 additions & 0 deletions helpers/lib/icu-detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const hasFullICU = (() => {
try {
const january = new Date(9e8);
const spanish = new Intl.DateTimeFormat('es', { month: 'long' });
return spanish.format(january) === 'enero';
} catch (err) {
return false;
}
})();

module.exports = {
hasFullICU
};
46 changes: 46 additions & 0 deletions helpers/localizePrice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
const utils = require('handlebars-utils');
// Detect ICU support
const { hasFullICU } = require('./lib/icu-detect.js');

const factory = () => {
return function(price, locale) {
if (!utils.isObject(price) || !utils.isString(price.currency)
|| isNaN(price.value) || !utils.isString(price.formatted)) {
// Return empty string if this does not appear to be a price object
return '';
}

if (!utils.isString(locale) || locale.length < 2) {
// Valid browser language strings are at least two characters
// https://www.metamodpro.com/browser-language-codes
// If provided locale is less than two characters (or not a string),
// return the normal formatted price
return price.formatted;
}

// If the if full ICU is not installed, fall back to normal price
if (!hasFullICU){
return price.formatted;
}

// Try to format the price to the provided locale,
// but if anything goes wrong,
// just return the usual price
// Could happen if the full ICU is not installed,
// or if an invalid locale is provided.
try {
return new Intl.NumberFormat(
locale, { style: 'currency', currency: price.currency}
).format(price.value);
}
catch (err){
return price.formatted;
}
};
};

module.exports = [{
name: 'localizePrice',
factory: factory,
}];
1 change: 1 addition & 0 deletions spec/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('helper registration', () => {
'lang',
'langJson',
'limit',
'localizePrice',
'money',
'nl2br',
'occurrences',
Expand Down
40 changes: 40 additions & 0 deletions spec/helpers/localizePrice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require('full-icu');

const Lab = require('lab'),
lab = exports.lab = Lab.script(),
describe = lab.experiment,
it = lab.it,
specHelpers = require('../spec-helpers'),
testRunner = specHelpers.testRunner

describe('localizePrice helper', function() {
const context = {
"price": {
"tax_label": "GST",
"without_tax": {
"currency": "USD",
"formatted": "$123,456.78",
"value": 123456.78
}
}
};

const runTestCases = testRunner({context});

it('should return return correct prices across a number of locales', function(done) {
runTestCases([
{
input: '{{localizePrice price.without_tax "en-US"}}',
output: '$123,456.78',
},
{
input: '{{localizePrice price.without_tax "ja-JP"}}',
output: '$123,456.78',
},
{
input: '{{localizePrice price.without_tax "de"}}',
output: '123.456,78 $',
},
], done);
});
});

0 comments on commit 86fcff1

Please sign in to comment.