- 校验 Ajax::lang() 不再使用 Expires/一个月长缓存,且带 ETag + must-revalidate - 校验 application/tags.php 已注册 FcAssets 行为、行为文件存在 避免今后有人改回长缓存,重现「改了语言包后台仍显示英文 key」
161 行
6.9 KiB
JavaScript
161 行
6.9 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* 后台字段中文化校验(FastAdmin PC)
|
||
*
|
||
* 校验目标:
|
||
* 1) 列表/表单里所有 `__('Key')` 都能在对应语言包里找到**中文**映射
|
||
* —— 缺失即会在后台显示英文 key(这正是「字段没中文化」的直接原因)
|
||
* 2) 语言包的值确实包含中文(排除照抄英文/注释原文的情况)
|
||
* 3) 已知枚举字段都加了 searchList + formatter(取值不会是裸数字/英文枚举)
|
||
*
|
||
* 用法:node tools/check-admin-i18n.mjs
|
||
*/
|
||
import fs from 'node:fs'
|
||
import path from 'node:path'
|
||
import url from 'node:url'
|
||
|
||
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
|
||
const ROOT = path.resolve(__dirname, '..')
|
||
const JS_DIR = path.join(ROOT, 'public/assets/js/backend/fc')
|
||
const LANG_DIR = path.join(ROOT, 'application/admin/lang/zh-cn/fc')
|
||
const VIEW_DIR = path.join(ROOT, 'application/admin/view/fc')
|
||
|
||
let errors = 0
|
||
let warns = 0
|
||
const bad = (m) => { console.log(` ❌ ${m}`); errors++ }
|
||
const warn = (m) => { console.log(` ⚠️ ${m}`); warns++ }
|
||
const ok = (m) => console.log(` ✅ ${m}`)
|
||
|
||
/** 提取源码里所有 __('Key') / __("Key") 的 key */
|
||
function extractKeys(src) {
|
||
const keys = new Set()
|
||
const re = /__\(\s*['"]([^'"]+)['"]\s*\)/g
|
||
let m
|
||
while ((m = re.exec(src))) keys.add(m[1])
|
||
return keys
|
||
}
|
||
|
||
/** 解析语言包 return [ 'Key' => '值', ... ] */
|
||
function parseLang(file) {
|
||
const map = new Map()
|
||
if (!fs.existsSync(file)) return map
|
||
const src = fs.readFileSync(file, 'utf8')
|
||
const re = /'([^']+)'\s*=>\s*'((?:[^'\\]|\\.)*)'/g
|
||
let m
|
||
while ((m = re.exec(src))) map.set(m[1], m[2].replace(/\\'/g, "'"))
|
||
return map
|
||
}
|
||
|
||
const hasCJK = (s) => /[\u4e00-\u9fa5]/.test(s)
|
||
|
||
/** 已知枚举字段 → 期望在 js 中同时出现 searchList(取值中文化) */
|
||
const ENUM_FIELDS = {
|
||
audit_status: true, status: true, is_paid: true, is_top: true, top: true,
|
||
is_member: true, isread: true, enabled: true, user_type: true, pay_type: true,
|
||
level: true, realname_status: true, shop_status: true, target_type: true, type: true,
|
||
channel: true, order_type: true,
|
||
}
|
||
|
||
const modules = fs.readdirSync(JS_DIR).filter((f) => f.endsWith('.js')).map((f) => f.replace(/\.js$/, '')).sort()
|
||
if (!modules.length) {
|
||
console.log('未找到后台模块 JS,路径:' + JS_DIR)
|
||
process.exit(1)
|
||
}
|
||
|
||
// FastAdmin 全局语言包:内置 key(Add/Edit/Delete/OK/Operate/Refresh/Recycle bin…)
|
||
// 都在这里有中文映射,因此缺失判定必须「全局 + 模块」都不存在才算错误。
|
||
const GLOBAL_LANG = parseLang(path.join(ROOT, 'application/admin/lang/zh-cn.php'))
|
||
// 这些值本身不是中文但完全正确,不计入「不含中文」告警
|
||
const CJK_EXEMPT = new Set(['ID', 'IP', 'URL', 'API', 'VIP', 'LOGO', 'Logo', 'OpenID', 'ok'])
|
||
|
||
console.log(`\n后台字段中文化校验(${modules.length} 个模块,全局语言包 ${GLOBAL_LANG.size} 条)\n`)
|
||
|
||
for (const name of modules) {
|
||
console.log(`[${name}]`)
|
||
const lang = parseLang(path.join(LANG_DIR, `${name}.php`))
|
||
if (!lang.size) bad(`${name}.php 语言包为空或不存在`)
|
||
|
||
// 1) 收集该模块 js + 视图里用到的所有 __() key
|
||
const files = [path.join(JS_DIR, `${name}.js`)]
|
||
const viewDir = path.join(VIEW_DIR, name)
|
||
if (fs.existsSync(viewDir)) {
|
||
for (const v of fs.readdirSync(viewDir)) {
|
||
if (v.endsWith('.html')) files.push(path.join(viewDir, v))
|
||
}
|
||
}
|
||
const keys = new Set()
|
||
for (const f of files) {
|
||
for (const k of extractKeys(fs.readFileSync(f, 'utf8'))) keys.add(k)
|
||
}
|
||
|
||
// 2) 缺映射 → 会显示英文
|
||
// 注意:key 本身含中文时(如 __('待审核')),FastAdmin 的 __() 查不到映射会原样
|
||
// 返回 key,显示仍然是中文,因此**不算缺**。只统计「英文 key 且无映射」的情况。
|
||
const missing = [...keys]
|
||
.filter((k) => !hasCJK(k) && !lang.has(k) && !GLOBAL_LANG.has(k))
|
||
.sort()
|
||
if (missing.length) {
|
||
bad(`缺少中文映射 ${missing.length} 个(后台将显示英文):${missing.slice(0, 12).join(', ')}${missing.length > 12 ? ' …' : ''}`)
|
||
} else {
|
||
ok(`字段标题全部有中文映射(${keys.size} 个 key)`)
|
||
}
|
||
|
||
// 3) 模块自身语言包的值必须含中文
|
||
const nonCJK = [...lang.entries()]
|
||
.filter(([, v]) => !hasCJK(v) && !CJK_EXEMPT.has(v))
|
||
.map(([k, v]) => `${k}=${v}`)
|
||
if (nonCJK.length) warn(`这些标签不含中文,请确认:${nonCJK.slice(0, 8).join(', ')}`)
|
||
|
||
// 4) 枚举字段应有 searchList(取值中文化)
|
||
const js = fs.readFileSync(path.join(JS_DIR, `${name}.js`), 'utf8')
|
||
const fieldRe = /field:\s*'([a-zA-Z_]+)'/g
|
||
const usedEnums = new Set()
|
||
let fm
|
||
while ((fm = fieldRe.exec(js))) {
|
||
if (ENUM_FIELDS[fm[1]]) usedEnums.add(fm[1])
|
||
}
|
||
const noSearch = [...usedEnums].filter((f) => {
|
||
// 该字段所在的列对象里是否出现 searchList
|
||
const seg = js.split(new RegExp(`field:\\s*'${f}'`))[1] || ''
|
||
const block = seg.slice(0, 400)
|
||
return !/searchList/.test(block)
|
||
})
|
||
if (noSearch.length) {
|
||
warn(`枚举字段缺 searchList(取值可能显示为原值):${noSearch.join(', ')}`)
|
||
} else if (usedEnums.size) {
|
||
ok(`枚举字段取值已中文化(${[...usedEnums].join(', ')})`)
|
||
}
|
||
}
|
||
|
||
// 5) 语言包缓存策略回归(详见 docs/06 第五章「改完为什么不生效」)
|
||
// 2026-10 线上事故:接口内容已是中文,但浏览器缓存了整包一个月,后台标题仍是英文 key
|
||
const ajaxFile = path.join(ROOT, 'application/admin/controller/Ajax.php')
|
||
const tagsFile = path.join(ROOT, 'application/tags.php')
|
||
const assetsFile = path.join(ROOT, 'application/common/behavior/FcAssets.php')
|
||
console.log('\n[语言包缓存策略]')
|
||
if (!fs.existsSync(assetsFile)) {
|
||
bad('缺少 application/common/behavior/FcAssets.php(站点版本号无法随语言包/JS 自动刷新)')
|
||
} else if (!/FcAssets/.test(fs.readFileSync(tagsFile, 'utf8'))) {
|
||
bad('application/tags.php 未注册 FcAssets 行为(app_init)')
|
||
} else {
|
||
ok('FcAssets 行为已注册(改语言包/JS 后资源 URL 自动变化)')
|
||
}
|
||
const ajaxSrc = fs.readFileSync(ajaxFile, 'utf8')
|
||
const langStart = ajaxSrc.indexOf('public function lang()')
|
||
const langEnd = ajaxSrc.indexOf('public function upload()')
|
||
const langFn = langStart >= 0 && langEnd > langStart ? ajaxSrc.slice(langStart, langEnd) : ''
|
||
if (!langFn) {
|
||
bad('未找到 Ajax::lang() 方法')
|
||
} else if (/'Expires'|"Expires"|30 \* 60 \* 60 \* 24/.test(langFn)) {
|
||
bad('Ajax::lang() 改回了长缓存(Expires),会再次出现「改了语言包后台仍显示英文」')
|
||
} else if (!/ETag/.test(langFn) || !/must-revalidate/.test(langFn)) {
|
||
bad('Ajax::lang() 缺少 ETag / must-revalidate,语言包可能被浏览器长期缓存')
|
||
} else {
|
||
ok('Ajax::lang() 按内容生成 ETag 且强制校验')
|
||
}
|
||
|
||
console.log('\n------------------------------------------')
|
||
console.log(`中文化校验结束:错误 ${errors},警告 ${warns}`)
|
||
console.log('------------------------------------------\n')
|
||
if (errors) process.exitCode = 1
|