Files
SHiNE-server/shine-UI/tests/i18n-completeness.test.mjs
T

46 lines
1.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
const uiRoot = fileURLToPath(new URL('../', import.meta.url));
const russian = JSON.parse(readFileSync(join(uiRoot, 'js/i18n/locales/ru.json'), 'utf8'));
const english = JSON.parse(readFileSync(join(uiRoot, 'js/i18n/locales/en.json'), 'utf8'));
function flatten(node, prefix = '') {
return Object.entries(node).flatMap(([name, value]) => {
const key = prefix ? `${prefix}.${name}` : name;
return typeof value === 'string' ? [[key, value]] : flatten(value, key);
});
}
function javascriptFiles(directory) {
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
const full = join(directory, entry.name);
return entry.isDirectory() ? javascriptFiles(full) : (entry.name.endsWith('.js') ? [full] : []);
});
}
test('оба словаря содержат одинаковые ключи и параметры', () => {
const ru = new Map(flatten(russian));
const en = new Map(flatten(english));
assert.deepEqual([...en.keys()].sort(), [...ru.keys()].sort());
for (const [key, value] of ru) {
const parameters = (text) => [...text.matchAll(/\{([a-zA-Z0-9_]+)\}/gu)].map((match) => match[1]).sort();
assert.deepEqual(parameters(en.get(key)), parameters(value), key);
assert.ok(en.get(key).trim(), key);
}
});
test('каждое статическое обращение t/th имеет русский ключ', () => {
const ru = new Map(flatten(russian));
for (const file of javascriptFiles(join(uiRoot, 'js'))) {
if (file.includes('/i18n/')) continue;
const source = readFileSync(file, 'utf8');
for (const match of source.matchAll(/\b(?:t|th)\('([^']+)'/gu)) {
assert.ok(ru.has(match[1]), `${file}: ${match[1]}`);
}
}
});