51 lines
2.7 KiB
JavaScript
51 lines
2.7 KiB
JavaScript
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]) => {
|
|
const path = prefix ? `${prefix}.${key}` : key
|
|
return child && typeof child === 'object' ? flatten(child, path) : [path]
|
|
})
|
|
}
|
|
|
|
const reference = new Set(flatten(zhCN))
|
|
const candidate = new Set(flatten(enUS))
|
|
const missing = [...reference].filter((key) => !candidate.has(key))
|
|
const extra = [...candidate].filter((key) => !reference.has(key))
|
|
|
|
// Fully migrated pages must not silently reintroduce hardcoded Chinese UI copy.
|
|
const completedViews = ['CoursesView.vue', 'OtherExamsView.vue', 'BaseDataView.vue', 'OfficialDocumentsView.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).`)
|
|
}
|