//! 类型安全的 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 // 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 }