Skip to content

Commit

Permalink
remove explicit intl-messageformat-parser dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Aug 19, 2019
1 parent be15fb9 commit 4d1b106
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
15 changes: 8 additions & 7 deletions lighthouse-core/lib/i18n/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const path = require('path');
const isDeepEqual = require('lodash.isequal');
const log = require('lighthouse-logger');
const MessageFormat = require('intl-messageformat').default;
const MessageParser = require('intl-messageformat-parser').default;
const lookupClosestLocale = require('lookup-closest-locale');
const LOCALES = require('./locales.js');

Expand Down Expand Up @@ -133,15 +132,16 @@ function lookupLocale(locale) {
}

/**
* Preformat values for the message based on how they're used, like KB or milliseconds.
* @param {string} icuMessage
* @param {MessageFormat} messageFormatter
* @param {Record<string, string | number>} [values]
* @return {Record<string, string | number>}
*/
function _preprocessMessageValues(icuMessage, values = {}) {
function _preformatValues(icuMessage, messageFormatter, values = {}) {
const clonedValues = JSON.parse(JSON.stringify(values));
const parsed = MessageParser.parse(icuMessage);

const elements = _collectAllCustomElementsFromICU(parsed.elements);
const elements = _collectAllCustomElementsFromICU(messageFormatter.getAst().elements);

return _processParsedElements(icuMessage, Array.from(elements.values()), clonedValues);
}
Expand Down Expand Up @@ -269,12 +269,13 @@ function _formatIcuMessage(locale, icuMessageId, uiStringMessage, values) {

// when using accented english, force the use of a different locale for number formatting
const localeForMessageFormat = (locale === 'en-XA' || locale === 'en-XL') ? 'de-DE' : locale;
// pre-process values for the message format like KB and milliseconds
const valuesForMessageFormat = _preprocessMessageValues(localeMessage, values);

const formatter = new MessageFormat(localeMessage, localeForMessageFormat, formats);
const formattedString = formatter.format(valuesForMessageFormat);

// preformat values for the message format like KB and milliseconds
const valuesForMessageFormat = _preformatValues(localeMessage, formatter, values);

const formattedString = formatter.format(valuesForMessageFormat);
return {formattedString, icuMessage: localeMessage};
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@
"inquirer": "^3.3.0",
"intl": "^1.2.5",
"intl-messageformat": "^4.4.0",
"intl-messageformat-parser": "^1.8.1",
"intl-pluralrules": "^1.0.3",
"jpeg-js": "0.1.2",
"js-library-detector": "^5.4.0",
Expand Down

0 comments on commit 4d1b106

Please sign in to comment.