Skip to content

Commit d7c381e

Browse files
committed
i18n: add registerLocaleData() method (#9638)
1 parent d93d32e commit d7c381e

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

lighthouse-core/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@ lighthouse.traceCategories = require('./gather/driver.js').traceCategories;
6666
lighthouse.Audit = require('./audits/audit.js');
6767
lighthouse.Gatherer = require('./gather/gatherers/gatherer.js');
6868
lighthouse.NetworkRecords = require('./computed/network-records.js');
69+
lighthouse.registerLocaleData = require('./lib/i18n/i18n.js').registerLocaleData;
6970

7071
module.exports = lighthouse;

lighthouse-core/lib/i18n/i18n.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,19 @@ function replaceIcuMessageInstanceIds(inputObject, locale) {
473473
return icuMessagePaths;
474474
}
475475

476+
/** @typedef {import('./locales').LhlMessages} LhlMessages */
477+
478+
/**
479+
* Populate the i18n string lookup dict with locale data
480+
* Used when the host environment selects the locale and serves lighthouse the intended locale file
481+
* @see https://docs.google.com/document/d/1jnt3BqKB-4q3AE94UWFA0Gqspx8Sd_jivlB7gQMlmfk/edit
482+
* @param {LH.Locale} locale
483+
* @param {LhlMessages} lhlMessages
484+
*/
485+
function registerLocaleData(locale, lhlMessages) {
486+
LOCALES[locale] = lhlMessages;
487+
}
488+
476489
module.exports = {
477490
_formatPathAsString,
478491
_ICUMsgNotFoundMsg,
@@ -485,4 +498,5 @@ module.exports = {
485498
replaceIcuMessageInstanceIds,
486499
isIcuMessage,
487500
collectAllCustomElementsFromICU,
501+
registerLocaleData,
488502
};

lighthouse-core/test/lib/i18n/i18n-test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ describe('i18n', () => {
7474
});
7575

7676
describe('#getFormatted', () => {
77+
it('returns the formatted string', () => {
78+
const UIStrings = {testMessage: 'happy test'};
79+
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);
80+
const formattedStr = i18n.getFormatted(str_(UIStrings.testMessage), 'en');
81+
expect(formattedStr).toEqual('happy test');
82+
});
83+
84+
85+
it('returns the formatted string with replacements', () => {
86+
const UIStrings = {testMessage: 'replacement test ({errorCode})'};
87+
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);
88+
const formattedStr = i18n.getFormatted(str_(UIStrings.testMessage, {errorCode: 'BOO'}), 'en');
89+
expect(formattedStr).toEqual('replacement test (BOO)');
90+
});
91+
7792
it('throws an error for invalid locales', () => {
7893
// Populate a string to try to localize to a bad locale.
7994
const UIStrings = {testMessage: 'testy test'};
@@ -98,6 +113,50 @@ describe('i18n', () => {
98113
});
99114
});
100115

116+
describe('#registerLocaleData', () => {
117+
it('installs new locale strings', () => {
118+
const localeData = {
119+
'lighthouse-core/test/lib/i18n/i18n-test.js | testString': {
120+
'message': 'en-XZ cuerda!',
121+
},
122+
};
123+
i18n.registerLocaleData('en-XZ', localeData);
124+
125+
const UIStrings = {testString: 'en-US string!'};
126+
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);
127+
const formattedStr = i18n.getFormatted(str_(UIStrings.testString), 'en-XZ');
128+
expect(formattedStr).toEqual('en-XZ cuerda!');
129+
});
130+
131+
it('overwrites existing locale strings', () => {
132+
const filename = 'lighthouse-core/audits/is-on-https.js';
133+
const UIStrings = require('../../../../lighthouse-core/audits/is-on-https.js').UIStrings;
134+
const str_ = i18n.createMessageInstanceIdFn(filename, UIStrings);
135+
136+
// To start with, we get back the intended string..
137+
const origTitle = i18n.getFormatted(str_(UIStrings.title), 'es-419');
138+
expect(origTitle).toEqual('Usa HTTPS');
139+
const origFailureTitle = i18n.getFormatted(str_(UIStrings.failureTitle), 'es-419');
140+
expect(origFailureTitle).toEqual('No usa HTTPS');
141+
142+
// Now we declare and register the new string...
143+
const localeData = {
144+
'lighthouse-core/audits/is-on-https.js | title': {
145+
'message': 'es-419 uses https!',
146+
},
147+
};
148+
i18n.registerLocaleData('es-419', localeData);
149+
150+
// And confirm that's what is returned
151+
const newTitle = i18n.getFormatted(str_(UIStrings.title), 'es-419');
152+
expect(newTitle).toEqual('es-419 uses https!');
153+
154+
// Meanwhile another string that wasn't set in registerLocaleData just falls back to english
155+
const newFailureTitle = i18n.getFormatted(str_(UIStrings.failureTitle), 'es-419');
156+
expect(newFailureTitle).toEqual('Does not use HTTPS');
157+
});
158+
});
159+
101160
describe('Message values are properly formatted', () => {
102161
// Message strings won't be in locale files, so will fall back to values given here.
103162
const UIStrings = {

0 commit comments

Comments
 (0)