Files
doona/tools/languages.mjs
T
Zakk c6afcef4d8 feat(i18n): allow partial languages that fall back to English
A language entry now says whether it is complete. check:i18n requires every key only of complete languages and prints the coverage of the others; --missing <id> lists what a language lacks with the English text. A partial language loads the English catalogue with its own and shows English for a missing key. zh-TW, zh-CN and en are complete and load exactly as before.
2026-09-28 13:41:03 +10:00

27 lines
1.7 KiB
JavaScript

// The language list for the Node tools, read from src/i18n/languages.ts with the TypeScript compiler, never evaluated,
// so the tools and the app share one list.
import {readFileSync} from 'node:fs';
import ts from 'typescript';
const file = 'src/i18n/languages.ts';
const source = ts.createSourceFile(file, readFileSync(new URL(`../${file}`, import.meta.url), 'utf8'), ts.ScriptTarget.Latest, true);
const literal = node => {
while (ts.isAsExpression(node) || ts.isSatisfiesExpression(node)) node = node.expression;
if (ts.isStringLiteral(node)) return node.text;
if (node.kind === ts.SyntaxKind.NullKeyword) return null;
if (node.kind === ts.SyntaxKind.TrueKeyword || node.kind === ts.SyntaxKind.FalseKeyword) return node.kind === ts.SyntaxKind.TrueKeyword;
if (ts.isArrayLiteralExpression(node)) return node.elements.map(literal);
if (ts.isObjectLiteralExpression(node)) return Object.fromEntries(node.properties.map(property => [property.name.text, literal(property.initializer)]));
throw new Error(`${file}: ${node.getText(source)} is not a plain literal`);
};
const constants = {};
for (const statement of source.statements)
if (ts.isVariableStatement(statement))
for (const declaration of statement.declarationList.declarations)
if (ts.isIdentifier(declaration.name) && declaration.initializer) constants[declaration.name.text] = declaration.initializer;
/** @type {Array<{id: string, name: string, locale: string, docs: string, fonts: string | null, complete: boolean}>} */
export const languages = literal(constants.languages);
export const REFERENCE_LANG = literal(constants.REFERENCE_LANG);
export const langs = languages.map(language => language.id);