Skip to content

Commit 3b35d53

Browse files
committed
pr fixes
1 parent cefe2f2 commit 3b35d53

File tree

2 files changed

+45
-51
lines changed

2 files changed

+45
-51
lines changed

core/lib/i18n/i18n.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ function lookupLocale(locales, possibleLocales) {
173173
function createIcuMessageFn(filename, fileStrings) {
174174
if (filename.startsWith('file://')) filename = url.fileURLToPath(filename);
175175

176+
// In the common case, `filename` is an absolute path that needs to be transformed
177+
// to be relative to LH_ROOT. In other cases, `filename` might be the exact i18n identifier
178+
// already (see: stack-packs.js, or bundled lighthouse).
179+
if (path.isAbsolute(filename)) filename = path.relative(LH_ROOT, filename);
180+
176181
/**
177182
* Combined so fn can access both caller's strings and i18n.UIStrings shared across LH.
178183
* @type {Record<string, string>}
@@ -189,26 +194,11 @@ function createIcuMessageFn(filename, fileStrings) {
189194
const keyname = Object.keys(mergedStrings).find(key => mergedStrings[key] === message);
190195
if (!keyname) throw new Error(`Could not locate: ${message}`);
191196

192-
let filenameToLookup;
193-
if (path.isAbsolute(filename)) {
194-
// `message` can be a UIString defined within the provided `fileStrings`, or it could be
195-
// one of the common strings found in `i18n.UIStrings`.
196-
if (keyname in fileStrings) {
197-
filenameToLookup = filename;
198-
} else if (keyname in UIStrings) {
199-
filenameToLookup = getModulePath(import.meta);
200-
} else {
201-
throw new Error(`Provided UIString is invalid: ${filename}, ${keyname}.`);
202-
}
203-
204-
filenameToLookup = path.relative(LH_ROOT, filenameToLookup);
205-
} else {
206-
// `filename` might have been passed in as the exact i18n identifier
207-
// already (see: stack-packs.js). Also the case for bundled lighthouse.
208-
// Otherwise, the common case requires relativizing the absolute filename with LH_ROOT.
209-
filenameToLookup = filename;
210-
}
211-
197+
// `message` can be a UIString defined within the provided `fileStrings`, or it could be
198+
// one of the common strings found in `i18n.UIStrings`.
199+
const filenameToLookup = keyname in fileStrings ?
200+
filename :
201+
path.relative(LH_ROOT, getModulePath(import.meta));
212202
const unixStyleFilename = filenameToLookup.replace(/\\/g, '/');
213203
const i18nId = `${unixStyleFilename} | ${keyname}`;
214204

shared/test/localization/format-test.js

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -186,37 +186,41 @@ describe('format', () => {
186186
expect(formattedStr).toEqual('en-XZ cuerda!');
187187
});
188188

189-
it('overwrites existing locale strings', async () => {
190-
const filename = `${LH_ROOT}/core/audits/is-on-https.js`;
191-
const {UIStrings} = await import('../../../core/audits/is-on-https.js');
192-
const str_ = i18n.createIcuMessageFn(filename, UIStrings);
193-
194-
// To start with, we get back the intended string..
195-
const origTitle = format.getFormatted(str_(UIStrings.title), 'es-419');
196-
expect(origTitle).toEqual('Usa HTTPS');
197-
const origFailureTitle = format.getFormatted(str_(UIStrings.failureTitle), 'es-419');
198-
expect(origFailureTitle).toEqual('No usa HTTPS');
199-
200-
// Now we declare and register the new string...
201-
const localeData = {
202-
'core/audits/is-on-https.js | title': {
203-
'message': 'new string for es-419 uses https!',
204-
},
205-
};
206-
format.registerLocaleData('es-419', localeData);
207-
208-
// And confirm that's what is returned
209-
const newTitle = format.getFormatted(str_(UIStrings.title), 'es-419');
210-
expect(newTitle).toEqual('new string for es-419 uses https!');
211-
212-
// Meanwhile another string that wasn't set in registerLocaleData just falls back to english
213-
const newFailureTitle = format.getFormatted(str_(UIStrings.failureTitle), 'es-419');
214-
expect(newFailureTitle).toEqual('Does not use HTTPS');
215-
216-
// Restore overwritten strings to avoid messing with other tests
217-
locales['es-419'] = clonedLocales['es-419'];
218-
const title = format.getFormatted(str_(UIStrings.title), 'es-419');
219-
expect(title).toEqual('Usa HTTPS');
189+
[
190+
{label: 'relative path', filename: 'core/audits/is-on-https.js'},
191+
{label: 'absolute path', filename: `${LH_ROOT}/core/audits/is-on-https.js`},
192+
].forEach(({label, filename}) => {
193+
it(`overwrites existing locale strings: ${label}`, async () => {
194+
const {UIStrings} = await import('../../../core/audits/is-on-https.js');
195+
const str_ = i18n.createIcuMessageFn(filename, UIStrings);
196+
197+
// To start with, we get back the intended string..
198+
const origTitle = format.getFormatted(str_(UIStrings.title), 'es-419');
199+
expect(origTitle).toEqual('Usa HTTPS');
200+
const origFailureTitle = format.getFormatted(str_(UIStrings.failureTitle), 'es-419');
201+
expect(origFailureTitle).toEqual('No usa HTTPS');
202+
203+
// Now we declare and register the new string...
204+
const localeData = {
205+
'core/audits/is-on-https.js | title': {
206+
'message': 'new string for es-419 uses https!',
207+
},
208+
};
209+
format.registerLocaleData('es-419', localeData);
210+
211+
// And confirm that's what is returned
212+
const newTitle = format.getFormatted(str_(UIStrings.title), 'es-419');
213+
expect(newTitle).toEqual('new string for es-419 uses https!');
214+
215+
// Meanwhile another string that wasn't set in registerLocaleData just falls back to english
216+
const newFailureTitle = format.getFormatted(str_(UIStrings.failureTitle), 'es-419');
217+
expect(newFailureTitle).toEqual('Does not use HTTPS');
218+
219+
// Restore overwritten strings to avoid messing with other tests
220+
locales['es-419'] = clonedLocales['es-419'];
221+
const title = format.getFormatted(str_(UIStrings.title), 'es-419');
222+
expect(title).toEqual('Usa HTTPS');
223+
});
220224
});
221225
});
222226

0 commit comments

Comments
 (0)