Skip to content

Commit

Permalink
clients(devtools): expose registerLocaleData to worker (#9645)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Nov 6, 2019
1 parent 4a507fa commit da4df8d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
7 changes: 7 additions & 0 deletions clients/devtools-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const lighthouse = require('../lighthouse-core/index.js');
const RawProtocol = require('../lighthouse-core/gather/connections/raw.js');
const log = require('lighthouse-logger');
const {registerLocaleData, lookupLocale} = require('../lighthouse-core/lib/i18n/i18n.js');

/** @typedef {import('../lighthouse-core/gather/connections/connection.js')} Connection */

Expand Down Expand Up @@ -53,6 +54,8 @@ if (typeof module !== 'undefined' && module.exports) {
module.exports = {
runLighthouseInWorker,
listenForStatus,
registerLocaleData,
lookupLocale,
};
}

Expand All @@ -63,4 +66,8 @@ if (typeof self !== 'undefined') {
self.runLighthouseInWorker = runLighthouseInWorker;
// @ts-ignore
self.listenForStatus = listenForStatus;
// @ts-ignore
self.registerLocaleData = registerLocaleData;
// @ts-ignore
self.lookupLocale = lookupLocale;
}
1 change: 0 additions & 1 deletion lighthouse-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,5 @@ lighthouse.traceCategories = require('./gather/driver.js').traceCategories;
lighthouse.Audit = require('./audits/audit.js');
lighthouse.Gatherer = require('./gather/gatherers/gatherer.js');
lighthouse.NetworkRecords = require('./computed/network-records.js');
lighthouse.registerLocaleData = require('./lib/i18n/i18n.js').registerLocaleData;

module.exports = lighthouse;
6 changes: 3 additions & 3 deletions lighthouse-core/lib/i18n/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ const locales = {
'tr': require('./locales/tr.json'),
'uk': require('./locales/uk.json'),
'vi': require('./locales/vi.json'),
'zh': require('./locales/zh.json'), // zh-CN identical, so it falls back into zh
'zh-HK': require('./locales/zh-HK.json'),
'zh-TW': require('./locales/zh-TW.json'),
'zh': require('./locales/zh.json'), // aka ZH-Hans, sometimes seen as zh-CN, zh-Hans-CN, Simplified Chinese
'zh-HK': require('./locales/zh-HK.json'), // aka zh-Hant-HK. Note: yue-Hant-HK is not supported.
'zh-TW': require('./locales/zh-TW.json'), // aka zh-Hant, zh-Hant-TW, Traditional Chinese
};

module.exports = locales;
13 changes: 11 additions & 2 deletions lighthouse-core/test/lib/i18n/i18n-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ describe('i18n', () => {
});

describe('#registerLocaleData', () => {
// Store original locale data so we can restore at the end
const moduleLocales = require('../../../lib/i18n/locales.js');
const clonedLocales = JSON.parse(JSON.stringify(moduleLocales));

it('installs new locale strings', () => {
const localeData = {
'lighthouse-core/test/lib/i18n/i18n-test.js | testString': {
Expand Down Expand Up @@ -160,18 +164,23 @@ describe('i18n', () => {
// Now we declare and register the new string...
const localeData = {
'lighthouse-core/audits/is-on-https.js | title': {
'message': 'es-419 uses https!',
'message': 'new string for es-419 uses https!',
},
};
i18n.registerLocaleData('es-419', localeData);

// And confirm that's what is returned
const newTitle = i18n.getFormatted(str_(UIStrings.title), 'es-419');
expect(newTitle).toEqual('es-419 uses https!');
expect(newTitle).toEqual('new string for es-419 uses https!');

// Meanwhile another string that wasn't set in registerLocaleData just falls back to english
const newFailureTitle = i18n.getFormatted(str_(UIStrings.failureTitle), 'es-419');
expect(newFailureTitle).toEqual('Does not use HTTPS');

// Restore overwritten strings to avoid messing with other tests
moduleLocales['es-419'] = clonedLocales['es-419'];
const title = i18n.getFormatted(str_(UIStrings.title), 'es-419');
expect(title).toEqual('Usa HTTPS');
});
});

Expand Down

0 comments on commit da4df8d

Please sign in to comment.