-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2a0b59
commit 86fcff1
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |