diff --git a/lighthouse-core/lib/i18n/i18n.js b/lighthouse-core/lib/i18n/i18n.js index d3da92e48acb..a6c30210106b 100644 --- a/lighthouse-core/lib/i18n/i18n.js +++ b/lighthouse-core/lib/i18n/i18n.js @@ -127,9 +127,7 @@ function lookupLocale(locale) { * @param {string} icuMessage * @param {Record} [values] */ -function _preprocessMessageValues(icuMessage, values) { - if (!values) return; - +function _preprocessMessageValues(icuMessage, values = {}) { const clonedValues = JSON.parse(JSON.stringify(values)); const parsed = MessageParser.parse(icuMessage); // Throw an error if a message's value isn't provided @@ -137,7 +135,7 @@ function _preprocessMessageValues(icuMessage, values) { .filter(el => el.type === 'argumentElement') .forEach(el => { if (el.id && (el.id in values) === false) { - throw new Error('ICU Message contains a value reference that wasn\'t provided'); + throw new Error(`ICU Message contains a value reference ("${el.id}") that wasn't provided`); } }); diff --git a/lighthouse-core/test/lib/i18n/i18n-test.js b/lighthouse-core/test/lib/i18n/i18n-test.js index 185c1df5e9a9..2782a2bb1d44 100644 --- a/lighthouse-core/test/lib/i18n/i18n-test.js +++ b/lighthouse-core/test/lib/i18n/i18n-test.js @@ -102,6 +102,7 @@ describe('i18n', () => { helloSecWorld: 'Hello {in, number, seconds} World', helloTimeInMsWorld: 'Hello {timeInMs, number, seconds} World', helloPercentWorld: 'Hello {in, number, extendedPercent} World', + helloWorldMultiReplace: '{hello} {world}', }; const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings); @@ -134,5 +135,16 @@ describe('i18n', () => { const helloPercentStr = str_(UIStrings.helloPercentWorld, {in: 0.43078}); expect(helloPercentStr).toBeDisplayString('Hello 43.08% World'); }); + + it('throws an error when values are needed but not provided', () => { + expect(_ => i18n.getFormatted(str_(UIStrings.helloBytesWorld), 'en-US')) + .toThrow(`ICU Message contains a value reference ("in") that wasn't provided`); + }); + + it('throws an error when a value is missing', () => { + expect(_ => i18n.getFormatted(str_(UIStrings.helloWorldMultiReplace, + {hello: 'hello'}), 'en-US')) + .toThrow(`ICU Message contains a value reference ("world") that wasn't provided`); + }); }); });