Skip to content

Commit

Permalink
STRF-7438 fix stencil language translation in Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-williamkwon committed Oct 18, 2019
1 parent 6269754 commit 7365a43
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 3.0.0-rc.26 (2019-10-16)
- Fix Stencil language translation in Safari[#186](https://github.com/bigcommerce/paper/pull/186)

## 3.0.0-rc.25 (2019-10-15)
- Bump paper-handlebars version to 4.2.3 [#183](https://github.com/bigcommerce/paper/pull/183)
- Refactor logging. You can now pass an optional console-like logger object which will be used for internal logging as well as Handlebars logging. [#183](https://github.com/bigcommerce/paper/pull/183)
Expand Down
5 changes: 5 additions & 0 deletions lib/translator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ function Translator(acceptLanguage, allTranslations, logger = console) {
* @returns {Translator}
*/
Translator.create = function (acceptLanguage, allTranslations, logger = console) {
// Safari only sends the most preferred language's code, this is to have fallbacks in case we don't have that language
// As an example we may not have fr-FR so add fr to the header
if (acceptLanguage.split(' ').length === 1 && acceptLanguage.split('-').length === 2) {
acceptLanguage = `${acceptLanguage.split('-')[0]}-${acceptLanguage.split('-')[1].toUpperCase()}, ${acceptLanguage.split('-')[0]};q=0.9`;
}
return new Translator(acceptLanguage, allTranslations, logger);
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bigcommerce/stencil-paper",
"version": "3.0.0-rc.25",
"version": "3.0.0-rc.26",
"description": "A Stencil plugin to load template files and render pages using backend renderer plugins.",
"main": "index.js",
"author": "Bigcommerce",
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ describe('Translator', () => {
done();
});

it('should return translated strings in default language if it cannot find a translation file for a specified language', done => {
const translator = Translator.create('fr-FR', translations);

expect(translator.translate('bye')).to.equal('au revoir');
expect(translator.translate('hello', { name: 'Joe' })).to.equal('Bonjour Joe');
expect(translator.translate('level1.level2')).to.equal('nous sommes dans le deuxième niveau');

done();
});

it('should return translated strings in English if cannot locate the preferred translation file', done => {
const translator = Translator.create('es', translations);

Expand Down

0 comments on commit 7365a43

Please sign in to comment.