Skip to content

Commit

Permalink
STRF-9222 Replace/upgrade deprecated messageformat library
Browse files Browse the repository at this point in the history
  • Loading branch information
jairo-bc committed Jul 12, 2021
1 parent e8918cb commit 1c4608f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 38 deletions.
30 changes: 3 additions & 27 deletions lib/translator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @module paper/lib/translator
*/

const MessageFormat = require('messageformat');
const MessageFormat = require('@messageformat/core');
const Filter = require('./filter');
const LocaleParser = require('./locale-parser');
const Transformer = require('./transformer');
Expand All @@ -23,12 +23,10 @@ const DEFAULT_LOCALE = 'en';
* @param {Object} allTranslations
* @param {Object} logger
* @param {Boolean} omitTransforming
* @param {Boolean} disablePluralKeyChecks
*/
function Translator(acceptLanguage, allTranslations, logger = console, omitTransforming = false, disablePluralKeyChecks = true) {
function Translator(acceptLanguage, allTranslations, logger = console, omitTransforming = false) {
this.logger = logger;
this.omitTransforming = omitTransforming;
this.disablePluralKeyChecks = disablePluralKeyChecks;

const languages = this.omitTransforming ? allTranslations : Transformer.transform(allTranslations, DEFAULT_LOCALE, this.logger);
/**
Expand Down Expand Up @@ -98,7 +96,6 @@ Translator.create = function (acceptLanguage, allTranslations, logger = console,
Translator.compileFormatterFunction = function (language, key) {
const locale = language.locales[key];
const formatter = new MessageFormat(locale);
formatter.disablePluralKeyChecks();

try {
const value = typeof language.translations[key] === "string"
Expand All @@ -122,19 +119,6 @@ Translator.prototype.areFormatterFunctionsGlobal = function() {
return global.plural;
}

Translator.prototype.enableFormatterFunctionsGlobalScope = function() {
const mf = new MessageFormat(this._locale);
global.plural = mf.runtime.plural;
global[this._locale] = mf.pluralFuncs[this._locale];
global.number = mf.runtime.number;
global.select = mf.runtime.select;
};

Translator.prototype.checkFormatterFunctionsAvailability = function() {
if (!this.areFormatterFunctionsGlobal()) {
this.enableFormatterFunctionsGlobalScope();
}
};

/**
* Get translated string
Expand All @@ -154,7 +138,6 @@ Translator.prototype.translate = function (key, parameters) {

try {
if (this.omitTransforming) {
this.checkFormatterFunctionsAvailability();
const fn = new Function(`return ${language.compiledTranslations[key]}`)()
return fn(parameters);
}
Expand Down Expand Up @@ -205,9 +188,6 @@ Translator.prototype.setLanguage = function (languages) {
Translator.prototype._getFormatter = function (locale) {
if (!this._formatters[locale]) {
this._formatters[locale] = new MessageFormat(locale);
if (this.disablePluralKeyChecks) {
this._formatters[locale].disablePluralKeyChecks();
}
}


Expand All @@ -227,11 +207,7 @@ Translator.prototype._compileTemplate = function (key) {
try {
return formatter.compile(language.translations[key]);
} catch (err) {
if (err.name === 'SyntaxError') {
this.logger.error(`Language File Syntax Error: ${err.message} for key "${key}"`, err.expected);

return () => '';
}
this.logger.error(`Language File Syntax Error: ${err.message} for key "${key}"`, err.expected);

throw err;
}
Expand Down
13 changes: 6 additions & 7 deletions lib/translator/locale-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @module paper/lib/translator/locale-parser
*/
const AcceptLanguageParser = require('accept-language-parser');
const MessageFormat = require('messageformat');
const MessageFormat = require('@messageformat/core');

/**
* Get preferred locale
Expand All @@ -15,14 +15,13 @@ const MessageFormat = require('messageformat');
*/
function getPreferredLocale(acceptLanguage, languages, defaultLocale) {
const locale = getLocales(acceptLanguage).find(locale => languages[locale]) || defaultLocale;

try {
new MessageFormat(locale);

return locale;
} catch (err) {
const mf = new MessageFormat(locale);
const options = mf.resolvedOptions();
if (options.locale === MessageFormat.defaultLocale) {
return defaultLocale;
}

return locale;
}

/**
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"homepage": "https://github.com/bigcommerce/paper",
"dependencies": {
"@bigcommerce/stencil-paper-handlebars": "4.4.7",
"@messageformat/core": "^3.0.0",
"accept-language-parser": "~1.4.1",
"messageformat": "~2.3.0"
},
Expand Down
7 changes: 3 additions & 4 deletions spec/lib/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ describe('Translator', () => {
},
}, loggerStub);

translator.translate('items_with_syntax_error', { count: 1 });

expect(() => translator.translate('items_with_syntax_error', { count: 1 })).to.throw(Error);
expect(loggerStub.error.called).to.equal(true);

done();
Expand Down Expand Up @@ -328,7 +327,7 @@ describe('Translator', () => {
done();
})

it('should not throw an error on compiling translation', done => {
it('should not throw an error on compiling translation (zero key is invalid for locale=en', done => {
const flattenedLanguages = {
"en": {
"locale": "en",
Expand All @@ -345,7 +344,7 @@ describe('Translator', () => {
const precompiledTranslations = Translator.precompileTranslations(flattenedLanguages);
translator.setLanguage(precompiledTranslations)
const result = translator.translate('items', {count: 0, term: 'product'});
expect(result).to.equal('0 results found for product');
expect(result).to.equal('{count, plural, zero{No results} one{# result} other{# results}} found for {term}');

done();
})
Expand Down

0 comments on commit 1c4608f

Please sign in to comment.