feat: complete catalog and exam localization and localize display dates

This commit is contained in:
biss committed 2026-10-31 19:20:50 +08:00
1 parent ed1f861708
commit 93b3916b11
27 files changed
+324 -266

No files matched your search

+29 -1
View File
@@ -1,5 +1,7 @@
import enUS from '../src/i18n/messages/en-US.ts'
import zhCN from '../src/i18n/messages/zh-CN.ts'
import { readFileSync, readdirSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
function flatten(value, prefix = '') {
return Object.entries(value).flatMap(([key, child]) => {
@@ -13,9 +15,35 @@ const candidate = new Set(flatten(enUS))
const missing = [...reference].filter((key) => !candidate.has(key))
const extra = [...candidate].filter((key) => !reference.has(key))
if (missing.length || extra.length) {
// Fully migrated pages must not silently reintroduce hardcoded Chinese UI copy.
const completedViews = ['CoursesView.vue', 'OtherExamsView.vue', 'BaseDataView.vue']
const hardcoded = completedViews.filter((name) => {
const file = fileURLToPath(new URL(`../src/views/${name}`, import.meta.url))
let source = readFileSync(file, 'utf8')
// These are editable persisted business defaults, not display labels.
if (name === 'BaseDataView.vue') source = source.replace("degreeType: '学士'", '').replace("roomType: '普通教室'", '')
return /\p{Script=Han}/u.test(source)
})
const viewsDirectory = new URL('../src/views/', import.meta.url)
const fixedLocaleViews = readdirSync(viewsDirectory).filter((name) => name.endsWith('.vue') &&
/(?:toLocale(?:Date|Time)?String|DateTimeFormat|NumberFormat|Collator)\(['"]zh-CN['"]/.test(readFileSync(new URL(name, viewsDirectory), 'utf8')))
function values(value, prefix = '') {
return Object.entries(value).flatMap(([key, child]) => {
const path = prefix ? `${prefix}.${key}` : key
return child && typeof child === 'object' ? values(child, path) : [[path, child]]
})
}
const english = new Map(values(enUS))
const placeholders = (value) => [...new Set([...String(value).matchAll(/\{(\w+)\}/g)].map((match) => match[1]))].sort().join(',')
const mismatched = values(zhCN).filter(([key, value]) => candidate.has(key) && placeholders(value) !== placeholders(english.get(key))).map(([key]) => key)
if (missing.length || extra.length || hardcoded.length || mismatched.length || fixedLocaleViews.length) {
if (missing.length) console.error(`en-US missing keys:\n${missing.join('\n')}`)
if (extra.length) console.error(`en-US extra keys:\n${extra.join('\n')}`)
if (hardcoded.length) console.error(`Hardcoded Chinese in completed views:\n${hardcoded.join('\n')}`)
if (mismatched.length) console.error(`Interpolation parameters differ:\n${mismatched.join('\n')}`)
if (fixedLocaleViews.length) console.error(`Fixed Chinese display formats:\n${fixedLocaleViews.join('\n')}`)
process.exitCode = 1
} else {
console.log(`i18n catalogs match (${reference.size} keys).`)