Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i18n: inject strings from trace engine npm package #16272

Merged
merged 5 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -766,6 +766,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 @@ -793,6 +841,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
Original file line number Diff line number Diff line change
Expand Up @@ -6942,8 +6942,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 @@ -11724,8 +11724,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 @@ -16922,8 +16922,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 @@ -24858,8 +24858,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 @@ -8988,8 +8988,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
Loading