Skip to content

Commit

Permalink
Merge pull request #243 from jairo-bc/STRF-9227
Browse files Browse the repository at this point in the history
STRF-9227 Address Polyglot exceptions
  • Loading branch information
junedkazi authored Jun 17, 2021
2 parents 53b4cba + 1278aa9 commit e0e0848
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/translator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,16 @@ Translator.compileFormatterFunction = function (language, key) {
formatter.disablePluralKeyChecks();

try {
return formatter.compile(language.translations[key]).toString();
const value = typeof language.translations[key] === "string"
? language.translations[key]
: language.translations[key].toString();
return formatter.compile(value).toString();
} catch (err) {
if (err.name === 'SyntaxError') {
this.logger.error(`Syntax Error during Formatter function precompilation: ${err.message} for key "${key}"`, err.expected);
console.error(`Error occured during Formatter function precompilation: ${err.message} for key "${key}"`);

return () => '';
}

throw err;
const value = language.translations[key] ? language.translations[key] : key;
const fn = new Function(`return "${value}"`);
return fn.toString();
}
}

Expand Down
25 changes: 25 additions & 0 deletions spec/lib/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,31 @@ describe('Translator', () => {

done();
})

it('should log error and return key', done => {
const flattenedValue = "Calificado {rating, plural, one {# Star} otro {# Stars}} O Mas";
const flattenedLanguages = {
"es": {
"locale": "es",
"locales": {
"items": "es",
},
"translations": {
"items": flattenedValue,
}
}
};
const locale = 'es';
const translator = Translator.create(locale, flattenedLanguages, console, true);
const precompiledTranslations = Translator.precompileTranslations(flattenedLanguages);
translator.setLanguage(precompiledTranslations)
const result = translator.translate('items', {rating: 10});

expect(result).to.equal(flattenedValue);

done();

});
})

});

0 comments on commit e0e0848

Please sign in to comment.