-
Notifications
You must be signed in to change notification settings - Fork 40
(WIP) Add "localizePrice" helper #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bookernath
wants to merge
1
commit into
bigcommerce:master
Choose a base branch
from
bookernath:localizedPrice
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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( | ||
bookernath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
locale, { style: 'currency', currency: price.currency} | ||
).format(price.value); | ||
} | ||
catch (err){ | ||
return price.formatted; | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = [{ | ||
name: 'localizePrice', | ||
factory: factory, | ||
}]; |
This file contains hidden or 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 hidden or 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,38 @@ | ||
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); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the locale value (coming from Stencil context) takes into consideration of the locale support by the theme? i.e.: The browser could forward an
accept-language
header that the theme could not fulfil. In that case, the price would be localized according to the locale value but not the rest of the page. But I guess it's not really a big issue (as it's understandable by the shopper)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it wouldn't take that into account; it's a straight representation of our parsed result of the browser's
Accept-Language
header.That being said, I don't think that's a bad thing - localizing price display is a different task than localizing all human-readable language, and perhaps we can consider those tasks separately?