用途:为 AdonisJS + Inertia 应用提供国际化方案,统一管理多语言文案、语言检测与用户语言偏好。
主要能力:语言在 config/i18n.ts 中声明(supportedLocales、defaultLocale、loaders)。翻译 JSON 按模块存放于 app/<mod>/resources/lang/<locale>/<mod>.json,键按模块命名空间(如 common.layout.navMain.dashboard),文件内不重复模块前缀,由加载器补上。后端用 ctx.i18n.t('key'),前端用 useTranslation(),返回 { t, changeLanguage, language }。支持 ICU 插值({name})、复数与选择器。VineJS 校验消息通过中间件绑定的 messages provider 本地化,键放在 <mod>/resources/lang/<locale>/validator.json 的 <field>.<rule> 下。
使用方式:新增键需在每个受支持语言文件中同步添加,无自动生成;缺失键运行时回退到默认语言。新增模块翻译需为每种语言创建 JSON,并在 config/i18n.ts 添加 fs loader:loaders.fs({ location: app.makePath('app/<mod>/resources/lang') })。语言检测中间件优先级为 X-User-Language 请求头 → user-locale Cookie → 浏览器 Accept-Language;已登录用户以 User.locale 为准,切换端点同时写 Cookie 与数据库,登录流程会从 user.locale 同步 Cookie。
适用场景:多语言 Web 应用、营销与认证页(登录前仅用 Cookie/Accept-Language)、控制器、action 与邮件类。
限制与注意:键路径深度建议不超过 3;不要硬编码字符串;不要直接读 req.cookies['user-locale'] 而应使用 ctx.i18n.locale;已登录时不要只写 Cookie 而不更新 user.locale;新增语言需更新 supportedLocales、为每个模块添加语言目录并确认所有键存在。