Repository: GoogleChrome/lighthouse
Pull request #5727 (2018-07-25 23:05:04)

core(i18n): always use english for logs
fixes what @paulirish found in #5726 


Patch:
diff --git a/lighthouse-core/lib/i18n.js b/lighthouse-core/lib/i18n.js
index a666b8de8..b724b4ac1 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,13 +197,43 @@ function createMessageInstanceIdFn(filename, fileStrings) {
   return getMessageInstanceIdFn;
 }
 
+/**
+ * @param {string} icuMessageIdOrRawString
+ * @param {LH.Locale} [locale]
+ * @return {string}
+ */
+function getFormatted(icuMessageIdOrRawString, locale) {
+  if (MESSAGE_INSTANCE_ID_REGEX.test(icuMessageIdOrRawString)) {
+    return _resolveIcuMessageInstanceId(icuMessageIdOrRawString, locale).formattedString;
+  }
+
+  return 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
    * @param {LH.I18NMessages} icuMessagePaths
@@ -216,11 +247,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 +257,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 +276,6 @@ module.exports = {
   getDefaultLocale,
   getRendererFormattedStrings,
   createMessageInstanceIdFn,
+  getFormatted,
   replaceIcuMessageInstanceIds,
 };
diff --git a/lighthouse-core/runner.js b/lighthouse-core/runner.js
index 249421fe8..39ad8e8f9 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.getFormatted(audit.meta.title)}`;
 
     log.log('status', status);
     let auditResult;


Test patch:
diff --git a/lighthouse-core/test/test-utils.js b/lighthouse-core/test/test-utils.js
index 0199ac12d..222d6ed27 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;