Files
testing-tool/.github/I18N.md
T

1.2 KiB
Raw Blame History

i18n 开发指南

概述

项目使用 Chrome 扩展标准的 chrome.i18n API 进行本地化,通过 src/utils/chromeI18n.ts 提供类型安全的 React Hook 包装。

配置

  • 翻译文件: public/_locales/{zh_CN,en}/messages.jsonChrome 扩展标准格式)
  • 默认语言: zh_CN(在 wxt.config.tsmanifest.default_locale 中配置)

使用方式

import { useI18n } from '@/utils/chromeI18n';

翻译键格式

  • 直接 key: t('dashboard_title') → 查找 dashboard_title
  • 命名空间格式(兼容旧用法): t('common:buttons.search') → 查找 common_buttons_search
  • 带命名空间参数: useI18n(['common', 'features']),会自动尝试 common_keyfeatures_key

占位符支持

t('router_notFoundDescription', { entryPointType: 'popup' });

Hook 返回值

{ t, i18n: { language, changeLanguage }, isLoaded }

回退策略

当翻译 key 未命中时,返回 key 本身(开发模式下在控制台记录 warning)

限制

chrome.i18n 无法动态切换语言,语言跟随浏览器设置,切换后需刷新页面