peppa/packages/testkit/test/i18n-kk.test.ts
2026-11-15 23:07:30 +05:00

59 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// PEP-99: Kazakh interface — the kk dictionary covers every key of the English one (the
// complete dictionary of PEP-98) with the same placeholders, and kk renders in Kazakh.
import { test } from "node:test";
import assert from "node:assert/strict";
import { dictionary, fill, intlLocale, localizeText, t, translator } from "@peppa/shared/i18n";
const placeholders = (s: string) => [...s.matchAll(/\{(\w+)\}/g)].map((m) => m[1]).sort().join(",");
test("Полнота: у казахского словаря есть все ключи английского", () => {
const en = dictionary("en");
const kk = dictionary("kk");
const missing = Object.keys(en).filter((k) => kk[k] === undefined);
assert.deepEqual(missing, [], `нет казахского перевода: ${missing.slice(0, 20).join(" | ")}`);
});
test("Граница: лишних ключей и чужих плейсхолдеров в казахском словаре нет", () => {
const en = dictionary("en");
const kk = dictionary("kk");
const extra = Object.keys(kk).filter((k) => en[k] === undefined);
assert.deepEqual(extra, [], `ключи, которых нет в коде: ${extra.slice(0, 20).join(" | ")}`);
const bad = Object.entries(kk).filter(([k, v]) => placeholders(k) !== placeholders(v));
assert.deepEqual(bad, [], "плейсхолдеры перевода не совпадают с ключом");
const empty = Object.entries(kk).filter(([, v]) => !v.trim());
assert.deepEqual(empty, []);
});
test("Казахский интерфейс: перевод, название, локаль дат", () => {
assert.equal(t("kk", "Ошибка: {message}", { message: "x" }), "Қате: x");
assert.equal(t("kk", "Пеппа"), "Peppa FACTORY");
assert.equal(intlLocale("kk"), "kk-KZ");
const tr = translator("kk");
assert.equal(tr.n(3, "{n} вопрос ждёт тебя", "{n} вопроса ждут тебя", "{n} вопросов ждут тебя"), "Сізді күтіп тұрған сұрақтар: 3");
});
test("Бот и уведомления: собранный русский текст любого ключа переводится на казахский", () => {
// The bot and push get finished Russian text and translate it back by templates (localizeText).
const kk = dictionary("kk");
const wrong: string[] = [];
for (const [key, value] of Object.entries(kk)) {
const vars: Record<string, string> = {};
let i = 0;
key.replace(/\{(\w+)\}/g, (_, name: string) => ((vars[name] = `Z${++i}q`), ""));
const got = localizeText("kk", fill(key, vars));
// Two keys of the same shape may share a rendering; Russian must not remain. «эпик «x»» is
// written the same in both languages, so another template's rendering looks untouched.
if (got !== fill(value, vars) && got === fill(key, vars) && key !== "эпик «{title}»") wrong.push(key);
}
assert.deepEqual(wrong, []);
});
test("Готовый русский текст переводится на казахский, неизвестный остаётся как есть", () => {
assert.equal(localizeText("kk", "Ошибка: нет связи"), "Қате: нет связи");
// Kazakh is Cyrillic too: the translated variant must win over the untouched Russian one.
assert.equal(localizeText("kk", "2 ч назад"), "2 сағ бұрын");
assert.equal(localizeText("kk", "✅ Стори PEP-4 готова к ревью · PR: http://x"), "✅ Стори PEP-4 ревьюге дайын · PR: http://x");
assert.equal(localizeText("kk", "Совсем неизвестная фраза"), "Совсем неизвестная фраза");
assert.equal(localizeText("ru", "Ошибка: x"), "Ошибка: x");
});