Skip to content

Commit

Permalink
i18n: inject strings from trace engine npm package (#16272)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored Dec 4, 2024
1 parent 23d322f commit 36cac18
Show file tree
Hide file tree
Showing 53 changed files with 2,997 additions and 90 deletions.
26 changes: 26 additions & 0 deletions core/computed/trace-engine-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import * as i18n from '../lib/i18n/i18n.js';
import * as TraceEngine from '../lib/trace-engine.js';
import {makeComputedArtifact} from './computed-artifact.js';
import {CumulativeLayoutShift} from './metrics/cumulative-layout-shift.js';
Expand Down Expand Up @@ -35,9 +36,34 @@ class TraceEngineResult {
), {});
if (!processor.parsedTrace) throw new Error('No data');
if (!processor.insights) throw new Error('No insights');
this.localizeInsights(processor.insights);
return {data: processor.parsedTrace, insights: processor.insights};
}

/**
* @param {import('@paulirish/trace_engine/models/trace/insights/types.js').TraceInsightSets} insightSets
*/
static localizeInsights(insightSets) {
for (const insightSet of insightSets.values()) {
for (const [name, model] of Object.entries(insightSet.model)) {
if (model instanceof Error) {
continue;
}

const key = `node_modules/@paulirish/trace_engine/models/trace/insights/${name}.js`;
const str_ = i18n.createIcuMessageFn(key, {
title: model.title,
description: model.description,
});

// @ts-expect-error coerce to string, should be fine
model.title = str_(model.title);
// @ts-expect-error coerce to string, should be fine
model.description = str_(model.description);
}
}
}

/**
* @param {{trace: LH.Trace}} data
* @param {LH.Artifacts.ComputedContext} context
Expand Down
49 changes: 49 additions & 0 deletions core/scripts/i18n/collect-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,54 @@ function checkKnownFixedCollisions(strings) {
}
}

/**
* @param {Record<any, any>} obj
* @return {Record<any, any>}
*/
function sortObject(obj) {
return Object.keys(obj).sort().reduce(function(result, key) {
// @ts-expect-error
result[key] = obj[key];
return result;
}, {});
}

/**
* Inject translated strings from `node_modules/@paulirish/trace_engine`. This avoids Lighthouse
* re-translating these same strings.
*/
function injectTraceEngineStrings() {
const traceEngineStringsDir = `${LH_ROOT}/node_modules/@paulirish/trace_engine/locales`;
const lhTraceStringsDir = `${LH_ROOT}/shared/localization/locales`;
for (const file of glob.sync(`${lhTraceStringsDir}/*.json`)) {
let name = path.basename(file);
if (name.endsWith('.ctc.json')) {
continue;
}

if (name === 'ar-XB.json') {
name = 'ar.json';
}

if (['en-XA.json'].includes(name)) {
continue;
}

const traceEnginePath = `${traceEngineStringsDir}/${name}`;
if (!fs.existsSync(traceEnginePath)) {
throw new Error(`expected locale file to exist: ${traceEnginePath}`);
}

const traceEngineStrings = JSON.parse(fs.readFileSync(traceEnginePath, 'utf-8'));
const strings = JSON.parse(fs.readFileSync(file, 'utf-8'));
for (const [key, value] of Object.entries(traceEngineStrings)) {
strings[`node_modules/@paulirish/trace_engine/${key.replace('.ts', '.js')}`] = value;
}

fs.writeFileSync(file, JSON.stringify(sortObject(strings), null, 2) + '\n');
}
}

async function main() {
/** @type {Record<string, CtcMessage>} */
const strings = {};
Expand Down Expand Up @@ -799,6 +847,7 @@ async function main() {

// Remove any obsolete strings in existing LHL files.
console.log('Checking for out-of-date LHL messages...');
injectTraceEngineStrings();
pruneObsoleteLhlMessages();

// Report on translation progress.
Expand Down
8 changes: 4 additions & 4 deletions core/test/fixtures/user-flows/reports/sample-flow-result.json
Original file line number Diff line number Diff line change
Expand Up @@ -7001,8 +7001,8 @@
"dropdownSaveGist": "Save as Gist",
"dropdownSaveHTML": "Save as HTML",
"dropdownSaveJSON": "Save as JSON",
"dropdownViewer": "Open in Viewer",
"dropdownViewUnthrottledTrace": "View Unthrottled Trace",
"dropdownViewer": "Open in Viewer",
"errorLabel": "Error!",
"errorMissingAuditInfo": "Report error: no audit information",
"expandView": "Expand view",
Expand Down Expand Up @@ -11806,8 +11806,8 @@
"dropdownSaveGist": "Save as Gist",
"dropdownSaveHTML": "Save as HTML",
"dropdownSaveJSON": "Save as JSON",
"dropdownViewer": "Open in Viewer",
"dropdownViewUnthrottledTrace": "View Unthrottled Trace",
"dropdownViewer": "Open in Viewer",
"errorLabel": "Error!",
"errorMissingAuditInfo": "Report error: no audit information",
"expandView": "Expand view",
Expand Down Expand Up @@ -17004,8 +17004,8 @@
"dropdownSaveGist": "Save as Gist",
"dropdownSaveHTML": "Save as HTML",
"dropdownSaveJSON": "Save as JSON",
"dropdownViewer": "Open in Viewer",
"dropdownViewUnthrottledTrace": "View Unthrottled Trace",
"dropdownViewer": "Open in Viewer",
"errorLabel": "Error!",
"errorMissingAuditInfo": "Report error: no audit information",
"expandView": "Expand view",
Expand Down Expand Up @@ -24999,8 +24999,8 @@
"dropdownSaveGist": "Save as Gist",
"dropdownSaveHTML": "Save as HTML",
"dropdownSaveJSON": "Save as JSON",
"dropdownViewer": "Open in Viewer",
"dropdownViewUnthrottledTrace": "View Unthrottled Trace",
"dropdownViewer": "Open in Viewer",
"errorLabel": "Error!",
"errorMissingAuditInfo": "Report error: no audit information",
"expandView": "Expand view",
Expand Down
2 changes: 1 addition & 1 deletion core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -9041,8 +9041,8 @@
"dropdownSaveGist": "Save as Gist",
"dropdownSaveHTML": "Save as HTML",
"dropdownSaveJSON": "Save as JSON",
"dropdownViewer": "Open in Viewer",
"dropdownViewUnthrottledTrace": "View Unthrottled Trace",
"dropdownViewer": "Open in Viewer",
"errorLabel": "Error!",
"errorMissingAuditInfo": "Report error: no audit information",
"expandView": "Expand view",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
"webtreemap-cdt": "^3.2.1"
},
"dependencies": {
"@paulirish/trace_engine": "0.0.38",
"@paulirish/trace_engine": "0.0.39",
"@sentry/node": "^7.0.0",
"axe-core": "^4.10.2",
"chrome-launcher": "^1.1.2",
Expand Down
60 changes: 60 additions & 0 deletions shared/localization/locales/ar-XB.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 36cac18

Please sign in to comment.