Word parameterized messages at the sender with omarchy-text and translate only exact text in the shell

This commit is contained in:
ZacharyZhang-NY committed 2026-10-06 22:29:57 -04:00
1 parent 619979a716
commit 051952600d
38 files changed
+198 -156

No files matched your search

+25 -3
View File
@@ -10,7 +10,7 @@ const vm = require('vm')
const source = fs.readFileSync(path.join(root, 'shell/Commons/I18nZh.js'), 'utf8').replace('.pragma library', '')
const context = {}
vm.runInNewContext(source + '\nthis.table = table; this.templates = templates', context)
vm.runInNewContext(source + '\nthis.table = table', context)
const table = context.table
const files = []
@@ -40,11 +40,33 @@ for (const file of files) {
}
}
assert(calls > 300, 'the shell routes its text through I18n.tr', `found ${calls} calls`)
const missingTemplates = Array.from(context.templates).filter(t => !(t in table))
assert(missingTemplates.length === 0, 'every message template has a Chinese entry', missingTemplates.join('\n'))
// Senders translate messages that carry a name or number with omarchy-text, or text() in Python.
const senders = []
for (const entry of fs.readdirSync(path.join(root, 'bin'), { withFileTypes: true })) {
if (!entry.isFile()) continue
const name = entry.name
const text = fs.readFileSync(path.join(root, 'bin', name), 'utf8')
for (const match of text.matchAll(/(?:omarchy-text\s+|\btext\(\s*)"((?:[^"\\]|\\.)*)"/g)) {
if (match[1].includes('$')) continue
senders.push([name, JSON.parse('"' + match[1] + '"')])
}
}
assert(senders.length > 40, 'senders translate their parameterized messages', `found ${senders.length}`)
const unsent = senders.filter(([, key]) => !(key in table)).map(([file, key]) => `bin/${file}: ${key}`)
assert(unsent.length === 0, 'every sender message has a Chinese entry', unsent.join('\n'))
assert(missing.length === 0, 'every I18n.tr text has a Chinese entry', missing.join('\n'))
const placeholders = s => (s.match(/%\d/g) || []).sort().join(',')
const mismatched = Object.keys(table).filter(key => placeholders(key) !== placeholders(table[key]))
assert(mismatched.length === 0, 'every Chinese entry keeps its English placeholders', mismatched.join('\n'))
JS
text=$ROOT/bin/omarchy-text
[[ $(OMARCHY_PATH=$ROOT LANG=zh_CN.UTF-8 "$text" "Sent to %1" "phone %2") == "已发送给 phone %2" ]] ||
fail "omarchy-text translates and fills %1 in one pass"
[[ $(OMARCHY_PATH=$ROOT LANG=en_US.UTF-8 "$text" "Battery is down to %1%" 9) == "Battery is down to 9%" ]] ||
fail "omarchy-text keeps English and a literal % outside zh_CN"
[[ $(OMARCHY_PATH=$ROOT LANG=zh_CN.UTF-8 "$text" "Unknown 50% of %1" x) == "Unknown 50% of x" ]] ||
fail "omarchy-text passes an unknown message through"
pass "omarchy-text translates by exact key and fills arguments in one pass"