重构: 前端架构审查降技术债·i18n收口(高ROI)
- 新建 src/i18n/i18n-helpers.ts: 类型安全 t + currentLocale(any中转内部1处带注释)
- 12文件改 i18n as any 样板收口: utils/time + stores(knowledge/project/{ideas,projects,tasks,workflow}) + composables(aiShared/useAiConversations/useAiEvents/useAiSend/useAiStream/useToolCard)
- as any 22→10(i18n 13→0对外), 签名统一(key, named?)
- useToolCard文档块同步(防AI被过时注释误导)
This commit is contained in:
32
src/i18n/i18n-helpers.ts
Normal file
32
src/i18n/i18n-helpers.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
//! 类型安全的 i18n 访问助手 — 供 store / composable / util 在非 setup 上下文使用
|
||||
//!
|
||||
//! 背景:vue-i18n 在 composition 模式下,`i18n.global.t` 的泛型会递归展开全部 message
|
||||
//! schema,触发 TS2589(类型实例化过深)。历史上每个 store/composable 都各自写一段
|
||||
//! `(i18n as any).global.t as (...)` 样板绕过,共 13 处,签名还微差(`(k:string)` /
|
||||
//! `(k:string, named?)` / `(key, params?)`)。本模块收口为一处:仅在内部用一次 any 中转,
|
||||
//! 对外暴露统一签名的 `t` 与 `currentLocale`。
|
||||
//!
|
||||
//! 用法:
|
||||
//! import { t, currentLocale } from '@/i18n/i18n-helpers'
|
||||
//! t('common.justNow')
|
||||
//! t('common.minutesAgo', { n: 5 })
|
||||
//! currentLocale() === 'en'
|
||||
|
||||
import i18n from '@/i18n'
|
||||
|
||||
/** 统一的翻译函数签名(键 + 可选命名参数) */
|
||||
type TranslateFn = (key: string, named?: Record<string, unknown>) => string
|
||||
|
||||
// any 中转仅在内部出现一次,对外不可见。规避 TS2589。
|
||||
const _global = (i18n as unknown as { global: { t: TranslateFn; locale: { value: string } } }).global
|
||||
|
||||
/**
|
||||
* 类型安全的翻译函数(绑定到全局 i18n 实例)。
|
||||
* 非组件上下文(store/composable/util 模块顶层)使用;组件内仍优先用 useI18n()。
|
||||
*/
|
||||
export const t: TranslateFn = _global.t.bind(_global)
|
||||
|
||||
/** 当前 locale('zh-CN' | 'en' 等),供时间格式化等按语言分支的逻辑读取 */
|
||||
export function currentLocale(): string {
|
||||
return _global.locale.value
|
||||
}
|
||||
Reference in New Issue
Block a user