mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-11-16 06:44:24 +08:00
feat(i18n): add Russian UI and harden localization
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { resolve, dirname } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { messageContractTokens } from './message-contract.mjs';
|
||||
|
||||
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
||||
|
||||
@@ -17,20 +18,34 @@ function flatten(obj, prefix = '') {
|
||||
return keys;
|
||||
}
|
||||
|
||||
const zh = JSON.parse(
|
||||
readFileSync(resolve(root, 'messages/zh-CN.json'), 'utf8'),
|
||||
);
|
||||
const en = JSON.parse(readFileSync(resolve(root, 'messages/en.json'), 'utf8'));
|
||||
const zhKeys = flatten(zh);
|
||||
const enKeys = flatten(en);
|
||||
const onlyZh = [...zhKeys].filter((k) => !enKeys.has(k)).sort();
|
||||
const onlyEnFixed = [...enKeys].filter((k) => !zhKeys.has(k)).sort();
|
||||
|
||||
if (onlyZh.length || onlyEnFixed.length) {
|
||||
console.error('i18n key mismatch');
|
||||
if (onlyZh.length) console.error('only in zh-CN:', onlyZh);
|
||||
if (onlyEnFixed.length) console.error('only in en:', onlyEnFixed);
|
||||
process.exit(1);
|
||||
for (const locale of ['zh-CN', 'ru']) {
|
||||
const messages = JSON.parse(
|
||||
readFileSync(resolve(root, `messages/${locale}.json`), 'utf8'),
|
||||
);
|
||||
const keys = flatten(messages);
|
||||
const extra = [...keys].filter((key) => !enKeys.has(key)).sort();
|
||||
const missing = [...enKeys].filter((key) => !keys.has(key)).sort();
|
||||
if (extra.length || missing.length) {
|
||||
console.error(`i18n key mismatch: ${locale}`, { extra, missing });
|
||||
process.exitCode = 1;
|
||||
}
|
||||
for (const key of enKeys) {
|
||||
const get = (source) =>
|
||||
key.split('.').reduce((value, part) => value?.[part], source);
|
||||
const source = get(en);
|
||||
const translated = get(messages);
|
||||
if (
|
||||
typeof source === 'string' &&
|
||||
typeof translated === 'string' &&
|
||||
JSON.stringify(messageContractTokens(source)) !==
|
||||
JSON.stringify(messageContractTokens(translated))
|
||||
) {
|
||||
console.error(`${locale} changes template variables or tags in ${key}`);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`i18n keys OK (${zhKeys.size} keys)`);
|
||||
if (!process.exitCode)
|
||||
console.log(`i18n keys OK (${enKeys.size} keys, 3 locales)`);
|
||||
|
||||
@@ -6,7 +6,8 @@ import ts from 'typescript';
|
||||
const root = process.argv[2]
|
||||
? resolve(process.argv[2])
|
||||
: resolve(dirname(fileURLToPath(import.meta.url)), '../..');
|
||||
const han = /\p{Script=Han}/u;
|
||||
const translatedScript =
|
||||
/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Script=Cyrillic}]/u;
|
||||
const failures = [];
|
||||
// These six identifiers are persisted upload counter keys. Renaming them is a
|
||||
// data migration, not a translation. The UI translates their labels separately.
|
||||
@@ -24,9 +25,11 @@ const compatibilityFiles = new Set([
|
||||
]);
|
||||
|
||||
function report(file, source, offset, text) {
|
||||
if (!han.test(text)) return;
|
||||
if (!translatedScript.test(text)) return;
|
||||
const name = relative(root, file).replaceAll('\\', '/');
|
||||
if (compatibilityFiles.has(name) && legacyCategoryKeys.has(text)) return;
|
||||
// Historical persisted notification recipient alias, not display text.
|
||||
if (name === 'internal/apps/admin/push/logics.go' && text === '系统') return;
|
||||
const line = source.slice(0, offset).split('\n').length;
|
||||
failures.push(`${name}:${line}`);
|
||||
}
|
||||
@@ -165,7 +168,7 @@ for (const file of ['frontend/proxy.ts', 'frontend/next.config.ts'])
|
||||
inspect(resolve(root, file));
|
||||
if (failures.length) {
|
||||
console.error(
|
||||
'Chinese runtime text must be moved into a locale catalog:\n' +
|
||||
'Non-English runtime text must be moved into a locale catalog:\n' +
|
||||
failures.join('\n'),
|
||||
);
|
||||
process.exitCode = 1;
|
||||
|
||||
@@ -39,4 +39,5 @@ function mergeLocale(locale) {
|
||||
|
||||
mergeLocale('zh-CN');
|
||||
mergeLocale('en');
|
||||
mergeLocale('ru');
|
||||
console.log('fragments merged');
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// Names used by renderers are technical tokens, including inside raw examples.
|
||||
export function messageContractTokens(message) {
|
||||
return [
|
||||
...message.matchAll(
|
||||
/\$[\p{L}_][\p{L}\p{N}_]*(?:\.[\p{L}_][\p{L}\p{N}_]*)*/gu,
|
||||
),
|
||||
...message.matchAll(/<\/?[\p{L}][\p{L}\p{N}_-]*(?:\s[^<>]*?)?\s*\/?>/gu),
|
||||
]
|
||||
.map(([token]) =>
|
||||
token.startsWith('<') ? token.match(/^<\/?[^\s/>]+/u)[0] + '>' : token,
|
||||
)
|
||||
.sort();
|
||||
}
|
||||
Reference in New Issue
Block a user