c59097b0ac
- i18n loader at apps/readest.koplugin/i18n.lua: isolated callable,
falls back to KOReader's gettext when a string is untranslated
- Translation catalog at locales/<i18next-code>/translation.po for 31
languages, mirroring apps/readest-app/public/locales/
- scripts/extract-i18n.js (scan _("...") and _([[...]]); preserve
existing, drop obsolete, add new) and scripts/apply-translations.js
(bulk import from /tmp/koplugin-translations/<lang>.json)
- Mirror apps/readest-app SyncInfoDialog: rename showMetaHashInfo to
showSyncInfo, dialog title "Sync Info", new Last Synced row computed
as max(last_synced_at_config, last_synced_at_notes) from doc_settings
- syncconfig.lua / syncannotations.lua mark per-book sync timestamps
on push/pull success
- Rename "Meta Hash" -> "Book Fingerprint" in koplugin and
apps/readest-app SyncInfoDialog.tsx; translations propagated to
all readest-app locales
- "book config" -> "reading progress" wording across user-facing
strings (matches QiuYukang fork terminology)
- Replace "Log out as " / "Login failed: " concat prefixes with
T(_("...%1..."), arg) placeholder pattern (RTL / verb-final friendly)
- pnpm lint:lua: luajit -b syntax check across koplugin .lua files;
soft-skips when luajit is missing locally; CI installs luajit and
runs the check unconditionally
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
293 lines
9.3 KiB
JavaScript
293 lines
9.3 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Extract translatable strings from readest.koplugin Lua sources and update
|
|
* locales/<lang>/translation.po for every language listed in the readest-app
|
|
* i18next-scanner config.
|
|
*
|
|
* Mirrors the role of `pnpm i18n:extract` (i18next-scanner) on the JS side:
|
|
* new msgids are appended with empty msgstr, obsolete msgids are dropped, and
|
|
* existing translations are preserved.
|
|
*
|
|
* Run with: node scripts/extract-i18n.js
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
const PLUGIN_DIR = path.resolve(__dirname, '..');
|
|
const SCANNER_CONFIG = path.resolve(PLUGIN_DIR, '../readest-app/i18next-scanner.config.cjs');
|
|
const LOCALES_DIR = path.join(PLUGIN_DIR, 'locales');
|
|
const PO_FILENAME = 'translation.po';
|
|
|
|
// Per-language metadata for .po headers. Plural-Forms follow CLDR conventions.
|
|
const LANG_META = {
|
|
de: { label: 'German', plural: 'nplurals=2; plural=(n != 1);' },
|
|
ja: { label: 'Japanese', plural: 'nplurals=1; plural=0;' },
|
|
es: { label: 'Spanish', plural: 'nplurals=2; plural=(n != 1);' },
|
|
fa: { label: 'Persian', plural: 'nplurals=2; plural=(n > 1);' },
|
|
fr: { label: 'French', plural: 'nplurals=2; plural=(n > 1);' },
|
|
it: { label: 'Italian', plural: 'nplurals=2; plural=(n != 1);' },
|
|
el: { label: 'Greek', plural: 'nplurals=2; plural=(n != 1);' },
|
|
ko: { label: 'Korean', plural: 'nplurals=1; plural=0;' },
|
|
uk: {
|
|
label: 'Ukrainian',
|
|
plural:
|
|
'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);',
|
|
},
|
|
nl: { label: 'Dutch', plural: 'nplurals=2; plural=(n != 1);' },
|
|
sl: {
|
|
label: 'Slovenian',
|
|
plural: 'nplurals=4; plural=(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3);',
|
|
},
|
|
sv: { label: 'Swedish', plural: 'nplurals=2; plural=(n != 1);' },
|
|
pl: {
|
|
label: 'Polish',
|
|
plural:
|
|
'nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);',
|
|
},
|
|
pt: { label: 'Portuguese', plural: 'nplurals=2; plural=(n != 1);' },
|
|
ru: {
|
|
label: 'Russian',
|
|
plural:
|
|
'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);',
|
|
},
|
|
tr: { label: 'Turkish', plural: 'nplurals=2; plural=(n > 1);' },
|
|
hi: { label: 'Hindi', plural: 'nplurals=2; plural=(n != 1);' },
|
|
id: { label: 'Indonesian', plural: 'nplurals=1; plural=0;' },
|
|
vi: { label: 'Vietnamese', plural: 'nplurals=1; plural=0;' },
|
|
ms: { label: 'Malay', plural: 'nplurals=1; plural=0;' },
|
|
he: { label: 'Hebrew', plural: 'nplurals=2; plural=(n != 1);' },
|
|
ar: {
|
|
label: 'Arabic',
|
|
plural:
|
|
'nplurals=6; plural=(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5);',
|
|
},
|
|
th: { label: 'Thai', plural: 'nplurals=1; plural=0;' },
|
|
bo: { label: 'Tibetan', plural: 'nplurals=1; plural=0;' },
|
|
bn: { label: 'Bengali', plural: 'nplurals=2; plural=(n != 1);' },
|
|
ta: { label: 'Tamil', plural: 'nplurals=2; plural=(n != 1);' },
|
|
si: { label: 'Sinhala', plural: 'nplurals=2; plural=(n != 1);' },
|
|
'zh-CN': { label: 'Chinese (Simplified)', plural: 'nplurals=1; plural=0;' },
|
|
'zh-TW': { label: 'Chinese (Traditional)', plural: 'nplurals=1; plural=0;' },
|
|
ro: {
|
|
label: 'Romanian',
|
|
plural: 'nplurals=3; plural=(n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2);',
|
|
},
|
|
hu: { label: 'Hungarian', plural: 'nplurals=2; plural=(n != 1);' },
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Lua source extraction
|
|
// ---------------------------------------------------------------------------
|
|
|
|
const LUA_ESCAPES = {
|
|
a: '\x07',
|
|
b: '\b',
|
|
f: '\f',
|
|
n: '\n',
|
|
r: '\r',
|
|
t: '\t',
|
|
v: '\v',
|
|
'\\': '\\',
|
|
'"': '"',
|
|
"'": "'",
|
|
};
|
|
|
|
function luaUnescape(s) {
|
|
let out = '';
|
|
for (let i = 0; i < s.length; i++) {
|
|
const c = s[i];
|
|
if (c === '\\' && i + 1 < s.length) {
|
|
const next = s[i + 1];
|
|
if (LUA_ESCAPES[next] !== undefined) {
|
|
out += LUA_ESCAPES[next];
|
|
i++;
|
|
continue;
|
|
}
|
|
}
|
|
out += c;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// _("..."), allowing escaped quotes inside.
|
|
const STRING_CALL_RE = /_\(\s*"((?:[^"\\]|\\.)*)"\s*[\),]/g;
|
|
// _([[ ... ]]) for long-bracket strings (no escapes - Lua treats them literally).
|
|
const BRACKET_CALL_RE = /_\(\s*\[\[([\s\S]*?)\]\]\s*\)/g;
|
|
|
|
function extractMsgids() {
|
|
const files = fs
|
|
.readdirSync(PLUGIN_DIR)
|
|
.filter((f) => f.endsWith('.lua'))
|
|
.sort();
|
|
const seen = new Map(); // preserves first-seen order
|
|
for (const file of files) {
|
|
const text = fs.readFileSync(path.join(PLUGIN_DIR, file), 'utf-8');
|
|
for (const m of text.matchAll(STRING_CALL_RE)) {
|
|
const id = luaUnescape(m[1]);
|
|
if (id && !seen.has(id)) seen.set(id, true);
|
|
}
|
|
for (const m of text.matchAll(BRACKET_CALL_RE)) {
|
|
// Long brackets ignore one immediately-following newline.
|
|
let id = m[1];
|
|
if (id.startsWith('\n')) id = id.slice(1);
|
|
else if (id.startsWith('\r\n')) id = id.slice(2);
|
|
if (id && !seen.has(id)) seen.set(id, true);
|
|
}
|
|
}
|
|
return [...seen.keys()];
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// .po parser - returns msgid -> msgstr (single-form entries only).
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function unescapePo(s) {
|
|
return s
|
|
.replace(/\\\\/g, ' |