waybetter i18n check|extract: an app's catalogs against the framework's rules
Configured by waybetter.i18n.json in the app's root; --write regenerates languages.json and the native localization files (macOS, Linux).
This commit is contained in:
parent
c570f308e9
commit
d8a94a2285
3 changed files with 446 additions and 3 deletions
|
|
@ -1,7 +1,9 @@
|
|||
#!/usr/bin/env node
|
||||
// The `waybetter` command: `waybetter store build|sign|verify` (cli/store.mjs) and
|
||||
// `waybetter release windows|macos` (cli/release.mjs).
|
||||
// The `waybetter` command: `waybetter store build|sign|verify` (cli/store.mjs),
|
||||
// `waybetter release windows|macos` (cli/release.mjs) and `waybetter i18n check|extract`
|
||||
// (cli/i18n.mjs).
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { I18N_USAGE, i18nCommand } from '../cli/i18n.mjs';
|
||||
import { STORE_USAGE, storeCommand } from '../cli/store.mjs';
|
||||
import { RELEASE_USAGE, releaseCommand } from '../cli/release.mjs';
|
||||
|
||||
|
|
@ -11,10 +13,12 @@ try {
|
|||
for (const line of await storeCommand(rest)) console.log(line);
|
||||
} else if (area === 'release') {
|
||||
await releaseCommand(rest);
|
||||
} else if (area === 'i18n') {
|
||||
process.exitCode = await i18nCommand(rest);
|
||||
} else if (area === '--version' || area === '-v') {
|
||||
console.log(JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version);
|
||||
} else {
|
||||
console.log(`${STORE_USAGE}\n\n${RELEASE_USAGE}`);
|
||||
console.log(`${STORE_USAGE}\n\n${RELEASE_USAGE}\n\n${I18N_USAGE}`);
|
||||
if (area && area !== '--help' && area !== '-h') process.exitCode = 1;
|
||||
}
|
||||
} catch (error) {
|
||||
|
|
|
|||
320
packages/waybetter/cli/i18n.mjs
Normal file
320
packages/waybetter/cli/i18n.mjs
Normal file
|
|
@ -0,0 +1,320 @@
|
|||
// `waybetter i18n check|extract`: an app's locale catalogs against the framework's rules
|
||||
// (waybetter/i18n/check: file shape, keys English does not have, ICU syntax, placeholder parity,
|
||||
// plural forms, no markup or control characters, length warnings), the files generated from them,
|
||||
// and the keys the app's code looks up.
|
||||
//
|
||||
// waybetter i18n check check every catalog (exit 1 on errors)
|
||||
// waybetter i18n check --write also regenerate languages.json and the native localization files
|
||||
// waybetter i18n check --verbose list every warning (untranslated and long messages)
|
||||
// waybetter i18n check tr de only these locales (plus English)
|
||||
// waybetter i18n extract keys the code looks up that English lacks (exit 1), then
|
||||
// English keys no literal lookup names
|
||||
// waybetter i18n extract --missing only the missing keys
|
||||
//
|
||||
// Config: waybetter.i18n.json in the app's root (or --config <file>); every path is relative to it.
|
||||
//
|
||||
// {
|
||||
// "locales": "locales", // <bcp47>.json, en.meta.json, the generated languages.json
|
||||
// "framework": "node_modules/waybetter/locales", // default: this package's catalogs
|
||||
// "sources": ["src"], // t('key'), tx('key'), coreText('key'), i18n.t('key')
|
||||
// "nativeSources": ["src-tauri/src"], // tr(app, "native.key", ...), trf(...)
|
||||
// "bundledPlugins": "src-tauri/bundled-plugins", // their inline English must match en.json
|
||||
// "runtimeKeys": ["plugin.", "native.app."], // prefixes of keys built at runtime or used by generated files
|
||||
// "native": { "tauriDir": "src-tauri", "desktopFile": "linux/app.desktop" }
|
||||
// }
|
||||
//
|
||||
// With "native", `--write` generates (and the plain check compares): macOS `Info.plist`
|
||||
// (CFBundleLocalizations) and `macos/<lang>.lproj/InfoPlist.strings` with `tauri.macos.conf.json`
|
||||
// listing them, and the Linux .desktop template with Name[xx]/Comment[xx] (`native.app.name`,
|
||||
// `native.app.comment`). The Windows installer's languages ship with this package
|
||||
// (windows/languages/*.nsh, generated from the framework catalogs).
|
||||
import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs';
|
||||
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
|
||||
export const I18N_CONFIG_FILE = 'waybetter.i18n.json';
|
||||
|
||||
export const I18N_USAGE = `Usage:
|
||||
waybetter i18n check [--write] [--verbose] [locale…] [--config <file>]
|
||||
Checks every locale catalog against English and the framework's rules; with --write also
|
||||
regenerates languages.json and the native localization files (macOS, Linux).
|
||||
waybetter i18n extract [--missing] [--config <file>]
|
||||
Keys the code looks up that English lacks, and English keys without a literal lookup.
|
||||
|
||||
Config: ${I18N_CONFIG_FILE} in the app's root (locales, framework, sources, nativeSources,
|
||||
bundledPlugins, runtimeKeys, native).`;
|
||||
|
||||
class I18nError extends Error {}
|
||||
|
||||
const read = file => JSON.parse(readFileSync(file, 'utf8'));
|
||||
const CATALOG = /^[a-z]{2,3}(-[A-Za-z0-9]+)*\.json$/;
|
||||
const catalogFiles = dir => readdirSync(dir).filter(file => CATALOG.test(file) && file !== 'languages.json');
|
||||
const walk = (dir, pattern) => readdirSync(dir).flatMap(name => {
|
||||
const path = join(dir, name);
|
||||
return statSync(path).isDirectory() ? walk(path, pattern) : pattern.test(name) ? [path] : [];
|
||||
});
|
||||
|
||||
/** The catalog checks: the framework's sources inside its repository, else the built package. */
|
||||
async function loadChecks() {
|
||||
const source = fileURLToPath(new URL('../../runtime/src/i18nCheck.ts', import.meta.url));
|
||||
const built = fileURLToPath(new URL('../dist/runtime/i18nCheck.js', import.meta.url));
|
||||
const file = existsSync(source) ? source : built;
|
||||
if (!existsSync(file)) throw new I18nError('the catalog checks are not built (run the package build)');
|
||||
return import(pathToFileURL(file).href);
|
||||
}
|
||||
|
||||
/** Reads and resolves the config (paths absolute). */
|
||||
export function loadI18nConfig(cwd, file) {
|
||||
const path = resolve(cwd, file ?? I18N_CONFIG_FILE);
|
||||
if (!existsSync(path)) throw new I18nError(`${relative(cwd, path) || path} not found (see \`waybetter i18n --help\`)`);
|
||||
let raw;
|
||||
try { raw = read(path); } catch (error) { throw new I18nError(`${path}: ${error.message}`); }
|
||||
const root = dirname(path);
|
||||
const at = value => (isAbsolute(value) ? value : join(root, value));
|
||||
const list = value => (Array.isArray(value) ? value : value ? [value] : []);
|
||||
if (typeof raw.locales !== 'string') throw new I18nError(`${I18N_CONFIG_FILE}: "locales" (the catalog folder) is required`);
|
||||
const native = raw.native && typeof raw.native === 'object' ? raw.native : null;
|
||||
if (native && typeof native.tauriDir !== 'string') throw new I18nError(`${I18N_CONFIG_FILE}: "native.tauriDir" is required`);
|
||||
return {
|
||||
root,
|
||||
locales: at(raw.locales),
|
||||
framework: raw.framework ? at(raw.framework) : fileURLToPath(new URL('../locales', import.meta.url)),
|
||||
sources: list(raw.sources ?? 'src').map(at),
|
||||
nativeSources: list(raw.nativeSources).map(at),
|
||||
bundledPlugins: raw.bundledPlugins ? at(raw.bundledPlugins) : null,
|
||||
runtimeKeys: list(raw.runtimeKeys ?? ['plugin.', 'native.app.']),
|
||||
native: native ? { tauriDir: at(native.tauriDir), desktopFile: native.desktopFile ?? null, macos: native.macos !== false } : null,
|
||||
};
|
||||
}
|
||||
|
||||
// ---- Native localization files ------------------------------------------------------------------
|
||||
|
||||
/** Catalog locale -> Apple localization id (none for private-use tags). */
|
||||
const apple = code => (code.includes('-x-') ? null : code);
|
||||
const plistEscape = text => text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||
const stringsEscape = text => text.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n');
|
||||
const desktopEscape = text => text.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/;/g, '\\;');
|
||||
|
||||
/** `[path, content]` of every generated native file: `catalogs` are the app's, English first. */
|
||||
export function renderNative(config, en, catalogs) {
|
||||
const { tauriDir, desktopFile, macos } = config.native;
|
||||
const from = relative(config.root, config.locales).replace(/\\/g, '/');
|
||||
const out = [];
|
||||
if (macos) {
|
||||
const codes = catalogs.map(catalog => apple(catalog.locale)).filter(Boolean);
|
||||
out.push([join(tauriDir, 'Info.plist'), [
|
||||
'<?xml version="1.0" encoding="UTF-8"?>',
|
||||
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">',
|
||||
`<!-- Generated by \`waybetter i18n check --write\` from ${from}. Merged into the app bundle by Tauri. -->`,
|
||||
'<plist version="1.0">',
|
||||
'<dict>',
|
||||
' <key>CFBundleDevelopmentRegion</key>',
|
||||
' <string>en</string>',
|
||||
' <key>CFBundleAllowMixedLocalizations</key>',
|
||||
' <true/>',
|
||||
' <key>CFBundleLocalizations</key>',
|
||||
' <array>',
|
||||
...codes.map(code => ` <string>${plistEscape(code)}</string>`),
|
||||
' </array>',
|
||||
'</dict>',
|
||||
'</plist>',
|
||||
'',
|
||||
].join('\n')]);
|
||||
const files = {};
|
||||
for (const catalog of catalogs) {
|
||||
const code = apple(catalog.locale);
|
||||
if (!code) continue;
|
||||
const name = catalog.messages['native.app.name'] ?? en.messages['native.app.name'];
|
||||
if (name === undefined) throw new I18nError('en.json needs "native.app.name" (the app\'s name on macOS and Linux)');
|
||||
const path = `macos/${code}.lproj/InfoPlist.strings`;
|
||||
files[`Resources/${code}.lproj/InfoPlist.strings`] = path;
|
||||
out.push([join(tauriDir, path), `/* Generated from ${from}/${catalog.locale}.json (\`waybetter i18n check --write\`). */\n"CFBundleDisplayName" = "${stringsEscape(name)}";\n"CFBundleName" = "${stringsEscape(name)}";\n`]);
|
||||
}
|
||||
out.push([join(tauriDir, 'tauri.macos.conf.json'), `${JSON.stringify({ bundle: { macOS: { files } } }, null, 2)}\n`]);
|
||||
}
|
||||
if (desktopFile) {
|
||||
const desktop = [
|
||||
'[Desktop Entry]',
|
||||
`# Generated by \`waybetter i18n check --write\` from ${from} (a Tauri .desktop template).`,
|
||||
'Categories={{categories}}',
|
||||
'{{#if comment}}',
|
||||
'Comment={{comment}}',
|
||||
'{{/if}}',
|
||||
];
|
||||
for (const catalog of catalogs) {
|
||||
if (catalog.locale === 'en' || !catalog.messages['native.app.comment']) continue;
|
||||
desktop.push(`Comment[${catalog.locale.replace('-', '_')}]=${desktopEscape(catalog.messages['native.app.comment'])}`);
|
||||
}
|
||||
desktop.push('Exec={{exec}}', 'Icon={{icon}}', 'Name={{name}}');
|
||||
for (const catalog of catalogs) {
|
||||
if (catalog.locale === 'en' || !catalog.messages['native.app.name'] || catalog.messages['native.app.name'] === en.messages['native.app.name']) continue;
|
||||
desktop.push(`Name[${catalog.locale.replace('-', '_')}]=${desktopEscape(catalog.messages['native.app.name'])}`);
|
||||
}
|
||||
desktop.push('Terminal=false', 'Type=Application', '{{#if mime_type}}', 'MimeType={{mime_type}}', '{{/if}}', '');
|
||||
out.push([join(tauriDir, desktopFile), desktop.join('\n')]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ---- check ------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Checks the catalogs. Returns `{ errors, warnings, lines }` (`lines`: what a terminal shows);
|
||||
* with `write`, out-of-date generated files are rewritten instead of reported.
|
||||
*/
|
||||
export async function checkCatalogs(config, { write = false, verbose = false, only = [] } = {}) {
|
||||
const { checkCatalog, checkShape, checkSource, coverage } = await loadChecks();
|
||||
const lines = [];
|
||||
let errors = 0;
|
||||
let warnings = 0;
|
||||
const label = path => relative(config.root, path).replace(/\\/g, '/');
|
||||
const report = (issue, where) => {
|
||||
const line = `${issue.level === 'error' ? 'ERROR' : 'warn '} ${where}${issue.key ? ` ${issue.key}` : ''}: ${issue.message}`;
|
||||
if (issue.level === 'error') { errors += 1; lines.push(line); }
|
||||
else { warnings += 1; if (verbose) lines.push(line); }
|
||||
};
|
||||
const metaOf = dir => {
|
||||
if (!existsSync(join(dir, 'en.meta.json'))) return {};
|
||||
const meta = read(join(dir, 'en.meta.json'));
|
||||
delete meta.$schema;
|
||||
return meta;
|
||||
};
|
||||
const APP = config.locales;
|
||||
const KIT = config.framework;
|
||||
|
||||
// ---- Source (English)
|
||||
const en = read(join(APP, 'en.json'));
|
||||
const meta = metaOf(APP);
|
||||
const kitEn = read(join(KIT, 'en.json'));
|
||||
const kitMeta = metaOf(KIT);
|
||||
for (const issue of [...checkShape(en, 'en.json'), ...checkSource(en, meta)]) report(issue, label(join(APP, 'en.json')));
|
||||
for (const issue of [...checkShape(kitEn, 'en.json'), ...checkSource(kitEn, kitMeta)]) report(issue, label(join(KIT, 'en.json')));
|
||||
for (const key of Object.keys(en.messages).filter(name => name in kitEn.messages)) {
|
||||
report({ level: 'error', key, message: 'the framework catalog already defines this key' }, label(join(APP, 'en.json')));
|
||||
}
|
||||
|
||||
// Bundled plugins ship their English inline (so a copied file still works); it must match en.json.
|
||||
if (config.bundledPlugins) {
|
||||
for (const file of readdirSync(config.bundledPlugins).filter(name => name.endsWith('.plugin.js'))) {
|
||||
const ns = file.replace(/\.plugin\.js$/, '');
|
||||
const text = readFileSync(join(config.bundledPlugins, file), 'utf8');
|
||||
const block = /\/\/ i18n:begin\r?\n([\s\S]*?)\/\/ i18n:end/.exec(text)?.[1];
|
||||
const hosted = Object.fromEntries(Object.entries(en.messages).filter(([key]) => key.startsWith(`plugin.${ns}.`)).map(([key, value]) => [key.slice(`plugin.${ns}.`.length), value]));
|
||||
if (!block) {
|
||||
if (Object.keys(hosted).some(key => key !== 'name' && key !== 'description')) report({ level: 'error', message: 'en.json has strings for this plugin but the file has no // i18n:begin … // i18n:end block' }, file);
|
||||
continue;
|
||||
}
|
||||
let inline;
|
||||
try { inline = JSON.parse(/const MESSAGES = ({[\s\S]*});/.exec(block)?.[1] ?? 'null'); } catch (error) { inline = null; report({ level: 'error', message: `MESSAGES is not plain JSON: ${error.message}` }, file); }
|
||||
if (!inline?.en) { report({ level: 'error', message: 'MESSAGES.en is missing' }, file); continue; }
|
||||
for (const [key, value] of Object.entries(inline.en)) {
|
||||
if (hosted[key] !== value) report({ level: 'error', key: `plugin.${ns}.${key}`, message: hosted[key] === undefined ? 'missing from en.json' : 'differs from en.json' }, file);
|
||||
}
|
||||
for (const key of Object.keys(hosted)) {
|
||||
if (key !== 'name' && key !== 'description' && !(key in inline.en)) report({ level: 'error', key: `plugin.${ns}.${key}`, message: 'in en.json but not in the plugin' }, file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Translations
|
||||
const appFiles = catalogFiles(APP).filter(file => file !== 'en.json');
|
||||
const kitFiles = new Set(catalogFiles(KIT));
|
||||
const languages = [{ code: 'en', name: en.name, englishName: en.englishName, dir: 'ltr', status: 'source', reviewers: [], coverage: 1 }];
|
||||
const table = [];
|
||||
for (const file of appFiles) {
|
||||
const catalog = read(join(APP, file));
|
||||
if (!kitFiles.has(file)) report({ level: 'error', message: `no framework catalog ${label(join(KIT, file))}` }, file);
|
||||
const kit = kitFiles.has(file) ? read(join(KIT, file)) : null;
|
||||
const fallback = catalog.fallback ? (catalog.fallback === 'en' ? null : read(join(APP, `${catalog.fallback}.json`))) : null;
|
||||
const cover = coverage(catalog, en.messages, fallback ?? undefined);
|
||||
languages.push({
|
||||
code: catalog.locale, name: catalog.name, englishName: catalog.englishName, dir: catalog.dir ?? 'ltr', status: catalog.status,
|
||||
reviewers: catalog.reviewers ?? [], ...(catalog.fallback ? { fallback: catalog.fallback } : {}), coverage: Math.round((catalog.fallback === 'en' ? 1 : cover) * 1000) / 1000,
|
||||
});
|
||||
if (kit && (kit.status !== catalog.status || kit.name !== catalog.name)) report({ level: 'error', message: 'name/status differ from the framework catalog of the same locale' }, file);
|
||||
if (only.length && !only.includes(catalog.locale)) continue;
|
||||
const shape = checkShape(catalog, file);
|
||||
for (const issue of [...shape, ...(shape.length ? [] : checkCatalog(catalog, en.messages, meta))]) report(issue, label(join(APP, file)));
|
||||
// The framework's words in the same language (the shell frame, the panel, the Store, the UI kit).
|
||||
if (kit) {
|
||||
const kitShape = checkShape(kit, file);
|
||||
for (const issue of [...kitShape, ...(kitShape.length ? [] : checkCatalog(kit, kitEn.messages, kitMeta))]) report(issue, label(join(KIT, file)));
|
||||
}
|
||||
table.push(`${catalog.locale.padEnd(8)} ${catalog.status.padEnd(9)} ${(cover * 100).toFixed(1).padStart(5)}% ${catalog.englishName}`);
|
||||
}
|
||||
languages.sort((a, b) => (a.code === 'en' ? -1 : b.code === 'en' ? 1 : a.englishName.localeCompare(b.englishName, 'en')));
|
||||
|
||||
// ---- Generated files
|
||||
const outputs = [[join(APP, 'languages.json'), `${JSON.stringify(languages, null, 2)}\n`]];
|
||||
if (config.native) outputs.push(...renderNative(config, en, languages.map(language => (language.code === 'en' ? en : read(join(APP, `${language.code}.json`))))));
|
||||
for (const [path, content] of outputs) {
|
||||
let current = null;
|
||||
try { current = readFileSync(path, 'utf8'); } catch { /* missing */ }
|
||||
if (current === content) continue;
|
||||
if (write) { mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, content); lines.push(`wrote ${label(path)}`); }
|
||||
else report({ level: 'error', message: 'out of date: run `waybetter i18n check --write`' }, label(path));
|
||||
}
|
||||
|
||||
lines.push(table.sort().join('\n'));
|
||||
lines.push(`\ni18n check: ${Object.keys(en.messages).length} app + ${Object.keys(kitEn.messages).length} framework messages, ${languages.length} languages; ${errors} error(s), ${warnings} warning(s)${verbose ? '' : ' (--verbose lists them)'}.`);
|
||||
return { errors, warnings, lines, languages };
|
||||
}
|
||||
|
||||
// ---- extract ----------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Compares the keys the code looks up with English. Literal lookups are `t('key')`, `tx('key')`,
|
||||
* `coreText('key')` and `i18n.t('key')` in `sources`, and `tr(app, "native.key", …)` / `trf(…)` in
|
||||
* `nativeSources`. Keys built at runtime (`features.${id}.label`) are invisible here, so "no
|
||||
* literal lookup" is a hint, never a reason to delete: search first.
|
||||
*/
|
||||
export function extractKeys(config, { onlyMissing = false } = {}) {
|
||||
const messages = { ...read(join(config.framework, 'en.json')).messages, ...read(join(config.locales, 'en.json')).messages };
|
||||
const used = new Map();
|
||||
const note = (key, file) => { if (!used.has(key)) used.set(key, relative(config.root, file).replace(/\\/g, '/')); };
|
||||
const call = /\b(?:t|tx|coreText|i18n\.t|i18n\.tx)\(\s*(['"])([a-z][A-Za-z0-9_.-]*)\1/g;
|
||||
const native = /\btrf?\(\s*&?app,\s*"([a-z][A-Za-z0-9_.-]*)"/g;
|
||||
for (const dir of config.sources) {
|
||||
for (const file of walk(dir, /\.(ts|tsx|js|jsx|mjs)$/)) for (const match of readFileSync(file, 'utf8').matchAll(call)) note(match[2], file);
|
||||
}
|
||||
for (const dir of config.nativeSources) {
|
||||
for (const file of walk(dir, /\.rs$/)) for (const match of readFileSync(file, 'utf8').matchAll(native)) note(match[1], file);
|
||||
}
|
||||
const lines = [];
|
||||
const missing = [...used].filter(([key]) => !(key in messages));
|
||||
for (const [key, file] of missing) lines.push(`missing ${key} (${file})`);
|
||||
if (onlyMissing) {
|
||||
lines.push(`${missing.length} missing`);
|
||||
} else {
|
||||
// The framework's own words are looked up by the framework (its tests check them).
|
||||
const app = read(join(config.locales, 'en.json')).messages;
|
||||
const unused = Object.keys(app).filter(key => !used.has(key) && !config.runtimeKeys.some(prefix => key.startsWith(prefix)));
|
||||
for (const key of unused) lines.push(`no literal lookup ${key}`);
|
||||
lines.push(`\n${used.size} keys looked up literally, ${missing.length} missing from en.json, ${unused.length} en.json keys without a literal lookup (many are built at runtime).`);
|
||||
}
|
||||
return { missing: missing.map(([key]) => key), lines };
|
||||
}
|
||||
|
||||
// ---- The command ------------------------------------------------------------------------------------
|
||||
|
||||
/** `waybetter i18n <check|extract> …`; returns the exit code. */
|
||||
export async function i18nCommand(args, { cwd = process.cwd(), log = console.log } = {}) {
|
||||
const [action, ...rest] = args;
|
||||
const flags = new Set(rest.filter(arg => arg.startsWith('--')));
|
||||
const configAt = rest.indexOf('--config');
|
||||
const configFile = configAt >= 0 ? rest[configAt + 1] : undefined;
|
||||
if (configAt >= 0 && !configFile) throw new I18nError('--config needs a file');
|
||||
const positional = rest.filter((arg, index) => !arg.startsWith('--') && (configAt < 0 || index !== configAt + 1));
|
||||
if (action === 'check') {
|
||||
const result = await checkCatalogs(loadI18nConfig(cwd, configFile), { write: flags.has('--write'), verbose: flags.has('--verbose'), only: positional });
|
||||
for (const line of result.lines) log(line);
|
||||
return result.errors ? 1 : 0;
|
||||
}
|
||||
if (action === 'extract') {
|
||||
const result = extractKeys(loadI18nConfig(cwd, configFile), { onlyMissing: flags.has('--missing') });
|
||||
for (const line of result.lines) log(line);
|
||||
return result.missing.length ? 1 : 0;
|
||||
}
|
||||
log(I18N_USAGE);
|
||||
return action && action !== '--help' && action !== '-h' ? 1 : 0;
|
||||
}
|
||||
119
packages/waybetter/tests/i18n-cli.test.mjs
Normal file
119
packages/waybetter/tests/i18n-cli.test.mjs
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
// `waybetter i18n check|extract` on a small app, and the Windows installer's language files.
|
||||
import assert from 'node:assert/strict';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import test from 'node:test';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { checkCatalogs, extractKeys, i18nCommand, loadI18nConfig } from '../cli/i18n.mjs';
|
||||
import { NSIS_KEYS, NSIS_LANGUAGES, nsisText, renderInstallerLanguages, syncInstallerLanguages } from '../scripts/installer-languages.mjs';
|
||||
|
||||
const BIN = fileURLToPath(new URL('../bin/waybetter.mjs', import.meta.url));
|
||||
const LOCALES = fileURLToPath(new URL('../locales', import.meta.url));
|
||||
const json = (file, value) => writeFileSync(file, `${JSON.stringify(value, null, 2)}\n`);
|
||||
const read = file => JSON.parse(readFileSync(file, 'utf8'));
|
||||
|
||||
/** A small app: English and Turkish catalogs, one source file, one native source, a plugin. */
|
||||
function app(extra = {}) {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'wb-i18n-'));
|
||||
for (const folder of ['locales', 'src', 'src-tauri/src', 'src-tauri/bundled-plugins']) mkdirSync(join(dir, folder), { recursive: true });
|
||||
const tr = read(join(LOCALES, 'tr.json'));
|
||||
json(join(dir, 'locales/en.json'), { locale: 'en', name: 'English', englishName: 'English', status: 'source', messages: {
|
||||
'app.hello': 'Hello {name}', 'app.count': '{count, plural, one {# tab} other {# tabs}}', 'app.unused': 'Never looked up',
|
||||
'native.app.name': 'Demo', 'native.app.comment': 'A demo', 'native.dialog.open': 'Open', 'plugin.clean.name': 'Clean', 'plugin.clean.done': 'Done',
|
||||
} });
|
||||
json(join(dir, 'locales/en.meta.json'), { 'app.hello': 'Greeting; {name} is the account.' });
|
||||
json(join(dir, 'locales/tr.json'), { locale: 'tr', name: tr.name, englishName: tr.englishName, status: tr.status, messages: {
|
||||
'app.hello': 'Merhaba {name}', 'native.app.name': 'Demo', 'native.app.comment': 'Bir demo', ...(extra.tr ?? {}),
|
||||
} });
|
||||
writeFileSync(join(dir, 'src/main.ts'), `t('app.hello', { name });\ntx('app.count');\n${extra.source ?? ''}`);
|
||||
writeFileSync(join(dir, 'src-tauri/src/lib.rs'), 'fn open(app: &AppHandle) { tr(app, "native.dialog.open", "Open"); }\n');
|
||||
writeFileSync(join(dir, 'src-tauri/bundled-plugins/clean.plugin.js'), `// i18n:begin\nconst MESSAGES = {"en":{"done":${JSON.stringify(extra.pluginDone ?? 'Done')}}};\n// i18n:end\n`);
|
||||
json(join(dir, 'waybetter.i18n.json'), {
|
||||
locales: 'locales', sources: ['src'], nativeSources: ['src-tauri/src'], bundledPlugins: 'src-tauri/bundled-plugins',
|
||||
native: { tauriDir: 'src-tauri', desktopFile: 'linux/demo.desktop' },
|
||||
});
|
||||
return dir;
|
||||
}
|
||||
|
||||
test('check: a clean app passes once its generated files are written; stale ones are errors', async () => {
|
||||
const dir = app();
|
||||
const config = loadI18nConfig(dir);
|
||||
assert.equal(config.framework, LOCALES, 'the package\'s own catalogs by default');
|
||||
const stale = await checkCatalogs(config);
|
||||
assert.ok(stale.errors > 0 && stale.lines.some(line => line.includes('locales/languages.json: out of date')));
|
||||
assert.equal(await i18nCommand(['check', '--write'], { cwd: dir, log() {} }), 0);
|
||||
const languages = read(join(dir, 'locales/languages.json'));
|
||||
assert.deepEqual(languages.map(language => language.code), ['en', 'tr']);
|
||||
assert.equal(languages[1].coverage, 0.375, '3 of 8 messages');
|
||||
// Native files: macOS localizations and names, the Linux .desktop template.
|
||||
assert.match(readFileSync(join(dir, 'src-tauri/Info.plist'), 'utf8'), /<string>tr<\/string>/);
|
||||
assert.match(readFileSync(join(dir, 'src-tauri/macos/tr.lproj/InfoPlist.strings'), 'utf8'), /"CFBundleName" = "Demo";/);
|
||||
assert.deepEqual(Object.keys(read(join(dir, 'src-tauri/tauri.macos.conf.json')).bundle.macOS.files), ['Resources/en.lproj/InfoPlist.strings', 'Resources/tr.lproj/InfoPlist.strings']);
|
||||
assert.match(readFileSync(join(dir, 'src-tauri/linux/demo.desktop'), 'utf8'), /Comment\[tr\]=Bir demo/);
|
||||
const clean = await checkCatalogs(config);
|
||||
assert.equal(clean.errors, 0, clean.lines.join('\n'));
|
||||
assert.match(clean.lines.at(-1), /8 app \+ \d+ framework messages, 2 languages; 0 error\(s\)/);
|
||||
assert.equal(await i18nCommand(['check', 'tr'], { cwd: dir, log() {} }), 0);
|
||||
});
|
||||
|
||||
test('check: markup, unknown keys, lost placeholders and a plugin whose English drifted are errors', async () => {
|
||||
const dir = app({ tr: { 'app.hello': '<b>Merhaba</b>', 'app.gone': 'x', 'app.count': '{count, plural, one {# sekme} other {# sekme}} {extra}' }, pluginDone: 'Finished' });
|
||||
const lines = [];
|
||||
assert.equal(await i18nCommand(['check', '--write'], { cwd: dir, log: line => lines.push(line) }), 1);
|
||||
const text = lines.join('\n');
|
||||
assert.match(text, /ERROR locales\/tr\.json app\.hello: markup is not allowed/);
|
||||
assert.match(text, /ERROR locales\/tr\.json app\.gone: key is not in the source catalog/);
|
||||
assert.match(text, /ERROR locales\/tr\.json app\.count: unknown placeholder\(s\) \{extra\}/);
|
||||
assert.match(text, /ERROR clean\.plugin\.js plugin\.clean\.done: differs from en\.json/);
|
||||
});
|
||||
|
||||
test('check: an app key the framework already defines is refused', async () => {
|
||||
const dir = app();
|
||||
const en = read(join(dir, 'locales/en.json'));
|
||||
en.messages['kit.ok'] = 'Fine';
|
||||
json(join(dir, 'locales/en.json'), en);
|
||||
const result = await checkCatalogs(loadI18nConfig(dir), { write: true });
|
||||
assert.ok(result.lines.some(line => line.includes('kit.ok: the framework catalog already defines this key')));
|
||||
});
|
||||
|
||||
test('extract: keys the code looks up must exist; keys built at runtime and generated ones are not flagged', () => {
|
||||
const dir = app({ source: "coreText('app.missing');\nt('kit.ok');\n" });
|
||||
const all = extractKeys(loadI18nConfig(dir));
|
||||
assert.deepEqual(all.missing, ['app.missing']);
|
||||
assert.ok(all.lines.includes('missing app.missing (src/main.ts)'));
|
||||
assert.ok(all.lines.includes('no literal lookup app.unused'));
|
||||
assert.ok(!all.lines.some(line => /no literal lookup (plugin\.|native\.app\.|kit\.|wb\.)/.test(line)), 'plugin strings, generated names and the framework\'s words');
|
||||
assert.ok(!all.lines.includes('no literal lookup native.dialog.open'), 'looked up natively');
|
||||
assert.deepEqual(extractKeys(loadI18nConfig(dir), { onlyMissing: true }).lines, ['missing app.missing (src/main.ts)', '1 missing']);
|
||||
});
|
||||
|
||||
test('the bin: usage, a missing config and exit codes', () => {
|
||||
const run = (cwd, ...args) => execFileSync(process.execPath, [BIN, ...args], { cwd, encoding: 'utf8', stdio: 'pipe' });
|
||||
assert.match(run(tmpdir(), 'i18n', '--help'), /waybetter i18n check/);
|
||||
assert.match(run(tmpdir(), '--help'), /waybetter i18n extract/);
|
||||
const empty = mkdtempSync(join(tmpdir(), 'wb-i18n-none-'));
|
||||
try { run(empty, 'i18n', 'check'); assert.fail('ran without a config'); }
|
||||
catch (error) { assert.equal(error.status, 1); assert.match(String(error.stderr), /waybetter\.i18n\.json not found/); }
|
||||
const dir = app();
|
||||
run(dir, 'i18n', 'check', '--write');
|
||||
assert.match(run(dir, 'i18n', 'check'), /0 error\(s\)/);
|
||||
assert.match(run(dir, 'i18n', 'extract', '--missing'), /0 missing/);
|
||||
});
|
||||
|
||||
test('installer languages: generated from the framework catalogs, up to date, one per NSIS language', () => {
|
||||
const { stale, extra } = syncInstallerLanguages();
|
||||
assert.deepEqual(stale, [], 'run `node packages/waybetter/scripts/installer-languages.mjs --write`');
|
||||
assert.deepEqual(extra, []);
|
||||
const files = renderInstallerLanguages();
|
||||
assert.equal(files.length, Object.keys(NSIS_LANGUAGES).length);
|
||||
const en = read(join(LOCALES, 'en.json')).messages;
|
||||
for (const key of NSIS_KEYS) assert.ok(`wb.installer.${key}` in en, key);
|
||||
const [name, thai] = files.find(([file]) => file === 'Thai.nsh');
|
||||
assert.equal(name, 'Thai.nsh');
|
||||
assert.equal(thai.split('\n').filter(line => line.startsWith('LangString ')).length, NSIS_KEYS.length);
|
||||
assert.ok(thai.includes('${LANG_THAI}') && thai.includes('${PRODUCTNAME}'));
|
||||
assert.equal(nsisText('{product} {version} "x"\n$1 {installed} {error} {code}'), '${PRODUCTNAME} ${VERSION} $\\"x$\\"$\\n$$1 $R4 $0 $1');
|
||||
assert.ok(existsSync(fileURLToPath(new URL('../windows/installer.nsi', import.meta.url))), 'next to the installer template');
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue