mirror of
https://github.com/Zakkaus/doona.git
synced 2026-12-06 16:01:00 +00:00
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.
27 lines
1.7 KiB
JavaScript
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);
|