9dfa06ffee
Docker image / Build (linux/amd64) (push) Has been cancelled
Docker image / Build (linux/arm64) (push) Has been cancelled
Docker image / Merge release multi-arch manifest (push) Has been cancelled
Docker image / Merge debug multi-arch manifest (push) Has been cancelled
Docker image / Build public push gateway (linux/amd64) (push) Has been cancelled
Docker image / Build public push gateway (linux/arm64) (push) Has been cancelled
Docker image / Publish public push gateway image (push) Has been cancelled
Sprig image / Build (linux/amd64) (push) Has been cancelled
Sprig image / Build (linux/arm64) (push) Has been cancelled
Sprig image / Merge multi-arch manifest (push) Has been cancelled
Harbor Buzz Orchestra / Python tests and lint (push) Has been cancelled
CI / Detect Changed Paths (push) Has been cancelled
CI / Rust Lint (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Desktop Core (push) Has been cancelled
CI / Desktop Smoke E2E (1) (push) Has been cancelled
CI / Desktop Smoke E2E (2) (push) Has been cancelled
CI / Desktop Smoke E2E (3) (push) Has been cancelled
CI / Desktop Smoke E2E (4) (push) Has been cancelled
CI / Desktop (push) Has been cancelled
CI / Desktop E2E Relay (push) Has been cancelled
CI / Desktop E2E Integration (1/2) (push) Has been cancelled
CI / Desktop E2E Integration (2/2) (push) Has been cancelled
CI / Desktop E2E Integration (push) Has been cancelled
CI / Backend Integration (relay e2e) (push) Has been cancelled
CI / Relay E2E (push) Has been cancelled
CI / Web (push) Has been cancelled
CI / Mobile (push) Has been cancelled
CI / Security (push) Has been cancelled
CI / Dead Token Reference Guard (push) Has been cancelled
CI / Server Cross-Compile (aarch64-unknown-linux-musl) (push) Has been cancelled
CI / Server Cross-Compile (x86_64-unknown-linux-musl) (push) Has been cancelled
CI / Windows Rust (x86_64-pc-windows-msvc) (push) Has been cancelled
CI / Desktop Build (macOS) (push) Has been cancelled
helm chart / lint + unittest + render matrix (push) Has been cancelled
helm chart / install on kind (gated) (push) Has been cancelled
helm chart / publish chart to GHCR (push) Has been cancelled
Mesh Lifecycle / Relay-Driven Mesh Lifecycle Smoke (push) Has been cancelled
Sprig / Build (aarch64-unknown-linux-musl) (push) Has been cancelled
Sprig / Build (x86_64-unknown-linux-musl) (push) Has been cancelled
Sprig / Publish rolling release (push) Has been cancelled
Sprig / Publish tagged release (push) Has been cancelled
Signed-off-by: cls_宁波本机 <908705107@qq.com>
111 lines
3.6 KiB
JavaScript
111 lines
3.6 KiB
JavaScript
import { readFileSync, readdirSync, statSync } from "node:fs";
|
|
import { dirname, join, relative } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const desktopDir = dirname(dirname(fileURLToPath(import.meta.url)));
|
|
const sourceDir = join(desktopDir, "src");
|
|
const localeFiles = ["en.json", "zh-Hans.json", "zh-Hant.json"];
|
|
const supplementalLocaleFiles = [
|
|
["ui-copy.en.json", "ui-dynamic.en.json"],
|
|
["ui-copy.zh-Hans.json", "ui-dynamic.zh-Hans.json"],
|
|
["ui-copy.zh-Hant.json", "ui-dynamic.zh-Hant.json"],
|
|
];
|
|
|
|
function flatten(value, prefix = "", result = {}) {
|
|
for (const [key, child] of Object.entries(value)) {
|
|
const path = prefix ? `${prefix}.${key}` : key;
|
|
if (child && typeof child === "object" && !Array.isArray(child)) {
|
|
flatten(child, path, result);
|
|
} else {
|
|
result[path] = String(child);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function placeholders(value) {
|
|
return [...value.matchAll(/\{\{\s*([^},\s]+)[^}]*\}\}/g)]
|
|
.map((match) => match[1])
|
|
.sort();
|
|
}
|
|
|
|
const catalogs = localeFiles.map((file, index) => ({
|
|
file,
|
|
entries: flatten(
|
|
Object.assign(
|
|
JSON.parse(readFileSync(join(sourceDir, "locales", file), "utf8")),
|
|
...supplementalLocaleFiles[index].map((supplementalFile) =>
|
|
JSON.parse(
|
|
readFileSync(join(sourceDir, "locales", supplementalFile), "utf8"),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
}));
|
|
const referenceKeys = Object.keys(catalogs[0].entries);
|
|
|
|
for (const catalog of catalogs.slice(1)) {
|
|
const keys = new Set(Object.keys(catalog.entries));
|
|
const missing = referenceKeys.filter((key) => !keys.has(key));
|
|
const extra = [...keys].filter((key) => !(key in catalogs[0].entries));
|
|
if (missing.length || extra.length) {
|
|
throw new Error(
|
|
`${catalog.file} locale mismatch\nmissing: ${missing.join(", ")}\nextra: ${extra.join(", ")}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
for (const key of referenceKeys) {
|
|
const expected = JSON.stringify(placeholders(catalogs[0].entries[key]));
|
|
for (const catalog of catalogs.slice(1)) {
|
|
const actual = JSON.stringify(placeholders(catalog.entries[key]));
|
|
if (actual !== expected) {
|
|
throw new Error(`${catalog.file} placeholder mismatch for ${key}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
const references = new Map();
|
|
function scanDirectory(directory) {
|
|
for (const name of readdirSync(directory)) {
|
|
if (["__snapshots__", "locales", "testing"].includes(name)) continue;
|
|
const path = join(directory, name);
|
|
if (statSync(path).isDirectory()) {
|
|
scanDirectory(path);
|
|
continue;
|
|
}
|
|
if (!/\.(mjs|ts|tsx)$/.test(name) || /\.(spec|test)\./.test(name)) continue;
|
|
const source = readFileSync(path, "utf8");
|
|
for (const match of source.matchAll(/\bt\(\s*["']([^"']+)["']/g)) {
|
|
const key = match[1];
|
|
if (!/^[A-Za-z][\w-]*(?:\.[A-Za-z][\w-]*)+$/.test(key)) continue;
|
|
if (!references.has(key)) references.set(key, relative(desktopDir, path));
|
|
}
|
|
for (const match of source.matchAll(/\bi18nKey\s*=\s*["']([^"']+)["']/g)) {
|
|
const key = match[1];
|
|
if (!/^[A-Za-z][\w-]*(?:\.[A-Za-z][\w-]*)+$/.test(key)) continue;
|
|
if (!references.has(key)) references.set(key, relative(desktopDir, path));
|
|
}
|
|
}
|
|
}
|
|
scanDirectory(sourceDir);
|
|
|
|
const knownKeys = new Set(referenceKeys);
|
|
const missingReferences = [...references].filter(
|
|
([key]) =>
|
|
!knownKeys.has(key) &&
|
|
!knownKeys.has(`${key}_one`) &&
|
|
!knownKeys.has(`${key}_other`),
|
|
);
|
|
if (missingReferences.length) {
|
|
throw new Error(
|
|
`Missing translation keys:\n${missingReferences
|
|
.map(([key, file]) => `${key} (${file})`)
|
|
.join("\n")}`,
|
|
);
|
|
}
|
|
|
|
console.log(
|
|
`i18n check passed: ${referenceKeys.length} keys, ${references.size} static references`,
|
|
);
|