mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-19 22:50:55 +08:00
fix(web): guard t() against prototype keys and check the dictionary in CI
t() looked a key up with dict[key] ?? key, so t("constructor") returned
Object and t("toString", {}) threw. Backend values are used as keys, so
this was reachable. Use an own-property check for the dictionary and the
vars, and commit the key-check script as npm run test:i18n in the web
job so tr.json cannot drift.
This commit is contained in:
@@ -149,6 +149,7 @@ jobs:
|
||||
- run: npm run test:polling
|
||||
- run: npm run test:replay
|
||||
- run: npm run test:upload
|
||||
- run: npm run test:i18n
|
||||
- run: npm run build:static
|
||||
|
||||
python:
|
||||
|
||||
+2
-1
@@ -15,7 +15,8 @@
|
||||
"generate:presets": "ts-node -P tsconfig.scripts.json src/scripts/generate-theme-presets.ts",
|
||||
"test:polling": "node --test scripts/polling.test.mjs",
|
||||
"test:replay": "node --test scripts/replay.test.mjs",
|
||||
"test:upload": "node --test scripts/upload.test.mjs"
|
||||
"test:upload": "node --test scripts/upload.test.mjs",
|
||||
"test:i18n": "node scripts/check-i18n.mjs"
|
||||
},
|
||||
"lint-staged": {
|
||||
"*.{js,ts,jsx,tsx}": [
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import { readFileSync, readdirSync, statSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
// Every literal t("...") / t('...') key must exist in the locale dictionary
|
||||
// with the same {placeholders}. Dynamic keys (variables, template literals
|
||||
// with ${}) are skipped: there is nothing to compare them against.
|
||||
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const dict = JSON.parse(readFileSync(join(root, "src/i18n/locales/tr.json"), "utf8"));
|
||||
|
||||
const files = [];
|
||||
function walk(dir) {
|
||||
for (const name of readdirSync(dir)) {
|
||||
const p = join(dir, name);
|
||||
const st = statSync(p);
|
||||
if (st.isDirectory()) walk(p);
|
||||
else if (/\.(ts|tsx)$/.test(name)) files.push(p);
|
||||
}
|
||||
}
|
||||
walk(join(root, "src"));
|
||||
|
||||
const calls = new Map(); // key -> first file that uses it
|
||||
const literalCall = /(?:^|[^\w$.])t\(\s*(?:"((?:[^"\\]|\\.)*)"|'((?:[^'\\]|\\.)*)')/gm;
|
||||
for (const file of files) {
|
||||
const src = readFileSync(file, "utf8");
|
||||
for (const m of src.matchAll(literalCall)) {
|
||||
const key =
|
||||
m[1] !== undefined ? JSON.parse(`"${m[1]}"`) : m[2].replace(/\\(['\\])/g, "$1");
|
||||
if (!calls.has(key)) calls.set(key, file.slice(root.length + 1));
|
||||
}
|
||||
}
|
||||
|
||||
function placeholders(s) {
|
||||
return [...s.matchAll(/\{(\w+)\}/g)]
|
||||
.map((m) => m[1])
|
||||
.sort()
|
||||
.join(",");
|
||||
}
|
||||
|
||||
const missing = [];
|
||||
const mismatch = [];
|
||||
for (const [key, file] of calls) {
|
||||
if (!Object.prototype.hasOwnProperty.call(dict, key)) {
|
||||
missing.push(`${file}: ${JSON.stringify(key)}`);
|
||||
continue;
|
||||
}
|
||||
if (placeholders(dict[key]) !== placeholders(key)) {
|
||||
mismatch.push(`${JSON.stringify(key)}: source {${placeholders(key)}} dict {${placeholders(dict[key])}}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (missing.length || mismatch.length) {
|
||||
if (missing.length) {
|
||||
console.error(`Missing tr.json entries (${missing.length}):\n${missing.join("\n")}`);
|
||||
}
|
||||
if (mismatch.length) {
|
||||
console.error(`Placeholder mismatches (${mismatch.length}):\n${mismatch.join("\n")}`);
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(
|
||||
`i18n: ${calls.size} literal keys checked against ${Object.keys(dict).length} tr.json entries`,
|
||||
);
|
||||
@@ -59,9 +59,11 @@ const dict = DICTIONARIES[locale];
|
||||
export type TVars = Record<string, string | number>;
|
||||
|
||||
export function t(key: string, vars?: TVars): string {
|
||||
const s = dict[key] ?? key;
|
||||
const s = Object.prototype.hasOwnProperty.call(dict, key) ? dict[key] : key;
|
||||
if (!vars) return s;
|
||||
return s.replace(/\{(\w+)\}/g, (m, name: string) => (name in vars ? String(vars[name]) : m));
|
||||
return s.replace(/\{(\w+)\}/g, (m, name: string) =>
|
||||
Object.prototype.hasOwnProperty.call(vars, name) ? String(vars[name]) : m,
|
||||
);
|
||||
}
|
||||
|
||||
export function setLocale(next: Locale) {
|
||||
|
||||
Reference in New Issue
Block a user