From f8db7f5ee15e3c3a3ee13ff0c5c2e7c17cf55255 Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Wed, 25 Jul 2018 16:04:23 -0700 Subject: [PATCH 1/4] core(i18n): always use english for logs --- lighthouse-core/lib/i18n.js | 46 +++++++++++++++++++++++++++++-------- lighthouse-core/runner.js | 2 +- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/lighthouse-core/lib/i18n.js b/lighthouse-core/lib/i18n.js index a666b8de88bd..9a15c32a5a43 100644 --- a/lighthouse-core/lib/i18n.js +++ b/lighthouse-core/lib/i18n.js @@ -13,6 +13,7 @@ const MessageParser = require('intl-messageformat-parser'); const LOCALES = require('./locales'); const LH_ROOT = path.join(__dirname, '../../'); +const MESSAGE_INSTANCE_ID_REGEX = /(.* \| .*) # (\d+)$/; (() => { // Node usually doesn't come with the locales we want built-in, so load the polyfill if we can. @@ -196,12 +197,41 @@ function createMessageInstanceIdFn(filename, fileStrings) { return getMessageInstanceIdFn; } +/** + * @param {string} icuMessageIdOrRawString + * @param {LH.Locale} [locale] + * @return {string} + */ +function getDisplayValue(icuMessageIdOrRawString, locale) { + return MESSAGE_INSTANCE_ID_REGEX.test(icuMessageIdOrRawString) ? + resolveIcuMessageInstanceId(icuMessageIdOrRawString, locale).formattedString : + icuMessageIdOrRawString; +} + +/** + * @param {string} icuMessageInstanceId + * @param {LH.Locale} [locale] + * @return {{icuMessageInstance: IcuMessageInstance, formattedString: string}} + */ +function resolveIcuMessageInstanceId(icuMessageInstanceId, locale = 'en-US') { + const matches = icuMessageInstanceId.match(MESSAGE_INSTANCE_ID_REGEX); + if (!matches) throw new Error(`${icuMessageInstanceId} is not a valid message instance ID`); + + const [_, icuMessageId, icuMessageInstanceIndex] = matches; + const icuMessageInstances = _icuMessageInstanceMap.get(icuMessageId) || []; + const icuMessageInstance = icuMessageInstances[Number(icuMessageInstanceIndex)]; + + const {formattedString} = _formatIcuMessage(locale, icuMessageId, + icuMessageInstance.icuMessage, icuMessageInstance.values); + + return {icuMessageInstance, formattedString} +} + /** * @param {LH.Result} lhr * @param {LH.Locale} locale */ function replaceIcuMessageInstanceIds(lhr, locale) { - const MESSAGE_INSTANCE_ID_REGEX = /(.* \| .*) # (\d+)$/; /** * @param {*} objectInLHR @@ -216,11 +246,8 @@ function replaceIcuMessageInstanceIds(lhr, locale) { // Check to see if the value in the LHR looks like a string reference. If it is, replace it. if (typeof value === 'string' && MESSAGE_INSTANCE_ID_REGEX.test(value)) { - // @ts-ignore - Guaranteed to match from .test call above - const [_, icuMessageId, icuMessageInstanceIndex] = value.match(MESSAGE_INSTANCE_ID_REGEX); - const messageInstancesInLHR = icuMessagePaths[icuMessageId] || []; - const icuMessageInstances = _icuMessageInstanceMap.get(icuMessageId) || []; - const icuMessageInstance = icuMessageInstances[Number(icuMessageInstanceIndex)]; + const {icuMessageInstance, formattedString} = resolveIcuMessageInstanceId(value, locale); + const messageInstancesInLHR = icuMessagePaths[icuMessageInstance.icuMessageId] || []; const currentPathAsString = _formatPathAsString(currentPathInLHR); messageInstancesInLHR.push( @@ -229,11 +256,8 @@ function replaceIcuMessageInstanceIds(lhr, locale) { currentPathAsString ); - const {formattedString} = _formatIcuMessage(locale, icuMessageId, - icuMessageInstance.icuMessage, icuMessageInstance.values); - objectInLHR[property] = formattedString; - icuMessagePaths[icuMessageId] = messageInstancesInLHR; + icuMessagePaths[icuMessageInstance.icuMessageId] = messageInstancesInLHR; } else { replaceInObject(value, icuMessagePaths, currentPathInLHR); } @@ -251,5 +275,7 @@ module.exports = { getDefaultLocale, getRendererFormattedStrings, createMessageInstanceIdFn, + getDisplayValue, + resolveIcuMessageInstanceId, replaceIcuMessageInstanceIds, }; diff --git a/lighthouse-core/runner.js b/lighthouse-core/runner.js index 249421fe800e..b418b0351f11 100644 --- a/lighthouse-core/runner.js +++ b/lighthouse-core/runner.js @@ -218,7 +218,7 @@ class Runner { */ static async _runAudit(auditDefn, artifacts, settings, runWarnings) { const audit = auditDefn.implementation; - const status = `Evaluating: ${audit.meta.title}`; + const status = `Evaluating: ${i18n.getDisplayValue(audit.meta.title)}`; log.log('status', status); let auditResult; From 211d83dd83d564165ecf43a702599fb5c5fcef4b Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Wed, 25 Jul 2018 16:10:01 -0700 Subject: [PATCH 2/4] lints --- lighthouse-core/lib/i18n.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lighthouse-core/lib/i18n.js b/lighthouse-core/lib/i18n.js index 9a15c32a5a43..c4266ce97b7b 100644 --- a/lighthouse-core/lib/i18n.js +++ b/lighthouse-core/lib/i18n.js @@ -224,7 +224,7 @@ function resolveIcuMessageInstanceId(icuMessageInstanceId, locale = 'en-US') { const {formattedString} = _formatIcuMessage(locale, icuMessageId, icuMessageInstance.icuMessage, icuMessageInstance.values); - return {icuMessageInstance, formattedString} + return {icuMessageInstance, formattedString}; } /** @@ -232,7 +232,6 @@ function resolveIcuMessageInstanceId(icuMessageInstanceId, locale = 'en-US') { * @param {LH.Locale} locale */ function replaceIcuMessageInstanceIds(lhr, locale) { - /** * @param {*} objectInLHR * @param {LH.I18NMessages} icuMessagePaths From 358cda964ea641c8de921b4ff64ffaa21e3cffac Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Wed, 25 Jul 2018 17:44:22 -0700 Subject: [PATCH 3/4] feedback --- lighthouse-core/lib/i18n.js | 17 +++++++++-------- lighthouse-core/runner.js | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lighthouse-core/lib/i18n.js b/lighthouse-core/lib/i18n.js index c4266ce97b7b..b724b4ac185c 100644 --- a/lighthouse-core/lib/i18n.js +++ b/lighthouse-core/lib/i18n.js @@ -202,10 +202,12 @@ function createMessageInstanceIdFn(filename, fileStrings) { * @param {LH.Locale} [locale] * @return {string} */ -function getDisplayValue(icuMessageIdOrRawString, locale) { - return MESSAGE_INSTANCE_ID_REGEX.test(icuMessageIdOrRawString) ? - resolveIcuMessageInstanceId(icuMessageIdOrRawString, locale).formattedString : - icuMessageIdOrRawString; +function getFormatted(icuMessageIdOrRawString, locale) { + if (MESSAGE_INSTANCE_ID_REGEX.test(icuMessageIdOrRawString)) { + return _resolveIcuMessageInstanceId(icuMessageIdOrRawString, locale).formattedString; + } + + return icuMessageIdOrRawString; } /** @@ -213,7 +215,7 @@ function getDisplayValue(icuMessageIdOrRawString, locale) { * @param {LH.Locale} [locale] * @return {{icuMessageInstance: IcuMessageInstance, formattedString: string}} */ -function resolveIcuMessageInstanceId(icuMessageInstanceId, locale = 'en-US') { +function _resolveIcuMessageInstanceId(icuMessageInstanceId, locale = 'en-US') { const matches = icuMessageInstanceId.match(MESSAGE_INSTANCE_ID_REGEX); if (!matches) throw new Error(`${icuMessageInstanceId} is not a valid message instance ID`); @@ -245,7 +247,7 @@ function replaceIcuMessageInstanceIds(lhr, locale) { // Check to see if the value in the LHR looks like a string reference. If it is, replace it. if (typeof value === 'string' && MESSAGE_INSTANCE_ID_REGEX.test(value)) { - const {icuMessageInstance, formattedString} = resolveIcuMessageInstanceId(value, locale); + const {icuMessageInstance, formattedString} = _resolveIcuMessageInstanceId(value, locale); const messageInstancesInLHR = icuMessagePaths[icuMessageInstance.icuMessageId] || []; const currentPathAsString = _formatPathAsString(currentPathInLHR); @@ -274,7 +276,6 @@ module.exports = { getDefaultLocale, getRendererFormattedStrings, createMessageInstanceIdFn, - getDisplayValue, - resolveIcuMessageInstanceId, + getFormatted, replaceIcuMessageInstanceIds, }; diff --git a/lighthouse-core/runner.js b/lighthouse-core/runner.js index b418b0351f11..39ad8e8f91a8 100644 --- a/lighthouse-core/runner.js +++ b/lighthouse-core/runner.js @@ -218,7 +218,7 @@ class Runner { */ static async _runAudit(auditDefn, artifacts, settings, runWarnings) { const audit = auditDefn.implementation; - const status = `Evaluating: ${i18n.getDisplayValue(audit.meta.title)}`; + const status = `Evaluating: ${i18n.getFormatted(audit.meta.title)}`; log.log('status', status); let auditResult; From 0d186e8290b82dab92c4c16d04ad96d1e80bce2b Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Wed, 25 Jul 2018 17:49:19 -0700 Subject: [PATCH 4/4] test utils --- lighthouse-core/test/test-utils.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lighthouse-core/test/test-utils.js b/lighthouse-core/test/test-utils.js index 0199ac12d3db..222d6ed275b4 100644 --- a/lighthouse-core/test/test-utils.js +++ b/lighthouse-core/test/test-utils.js @@ -11,9 +11,7 @@ const i18n = require('../lib/i18n'); expect.extend({ toBeDisplayString(received, expected) { - const obj = {value: received}; - i18n.replaceIcuMessageInstanceIds(obj, 'en-US'); - const actual = obj.value; + const actual = i18n.getFormatted(received, 'en-US'); const pass = expected instanceof RegExp ? expected.test(actual) : actual === expected;