refactor: 迁移 i18n 系统从 react-i18next 到 chrome.i18n
- 移除 react-i18next、i18next 及相关依赖
- 删除旧的 i18n/ 目录和 useLazyTranslation 工具
- 新增 utils/chromeI18n.ts 类型安全 wrapper(useI18n Hook + getMessage)
- 生成 public/_locales/{zh,en}/messages.json(298 个翻译 key)
- 批量更新 39+ 组件文件的导入和翻译调用
- 转换翻译键格式:namespace:key → namespace_key
- 修复 ErrorBoundary/PageErrorBoundary 从 withTranslation HOC 改为直接调用 getMessage
- 更新 vitest.setup.ts mock 加载实际翻译文本
- 修复 11 个测试文件的断言以匹配中文翻译
- TypeScript、ESLint、547 项测试全部通过
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
+32
-53
@@ -1,66 +1,45 @@
|
||||
import '@testing-library/jest-dom';
|
||||
import { afterEach, beforeEach, vi } from 'vitest';
|
||||
import { readFileSync } from 'fs';
|
||||
import { resolve } from 'path';
|
||||
import React from 'react';
|
||||
|
||||
// 读取中文翻译文件用于 withTranslation mock
|
||||
const zhCommon = JSON.parse(
|
||||
readFileSync(resolve(__dirname, 'i18n/locales/zh/common.json'), 'utf-8'),
|
||||
);
|
||||
// Load actual zh translations for getMessage mock
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const zhMessages: Record<
|
||||
string,
|
||||
{ message: string }
|
||||
> = require('./public/_locales/zh/messages.json');
|
||||
|
||||
// 支持嵌套 key 查找,如 "errorBoundary.title" → zhCommon.errorBoundary.title
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
function nestedLookup(obj: Record<string, any>, key: string): string | undefined {
|
||||
const parts = key.split('.');
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let current: any = obj;
|
||||
for (const part of parts) {
|
||||
if (current == null || typeof current !== 'object') return undefined;
|
||||
current = current[part];
|
||||
}
|
||||
return typeof current === 'string' ? current : undefined;
|
||||
}
|
||||
|
||||
vi.mock('@/utils/useLazyTranslation', () => ({
|
||||
useLazyTranslation: (ns?: string) => ({
|
||||
// 自动承接命名空间前缀过滤,100% 模拟真实多语种直出
|
||||
t: (key: string) => (ns ? `${ns}:${key}` : key),
|
||||
vi.mock('@/utils/chromeI18n', () => ({
|
||||
useI18n: (ns?: string | string[]) => ({
|
||||
t: (key: string) => {
|
||||
let msgId = key;
|
||||
// Handle namespace:key format
|
||||
if (key.includes(':')) {
|
||||
msgId = key.replace(':', '_').replace(/\./g, '_');
|
||||
}
|
||||
// Try direct key first
|
||||
if (zhMessages[msgId]) return zhMessages[msgId].message;
|
||||
// Try namespace prefix (using converted msgId)
|
||||
if (ns) {
|
||||
const namespaces = Array.isArray(ns) ? ns : [ns];
|
||||
for (const n of namespaces) {
|
||||
const candidate = `${n}_${msgId}`;
|
||||
if (zhMessages[candidate]) return zhMessages[candidate].message;
|
||||
}
|
||||
}
|
||||
return msgId;
|
||||
},
|
||||
i18n: {
|
||||
changeLanguage: vi.fn().mockResolvedValue(undefined),
|
||||
language: 'zh-CN',
|
||||
language: 'zh',
|
||||
},
|
||||
isLoaded: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('react-i18next', () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string) => key,
|
||||
i18n: {
|
||||
changeLanguage: vi.fn().mockResolvedValue(undefined),
|
||||
language: 'zh-CN',
|
||||
},
|
||||
}),
|
||||
withTranslation: (ns?: string) => {
|
||||
const translations = ns === 'common' ? zhCommon : {};
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return (Component: React.ComponentType<any>) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const Wrapped = (props: any) =>
|
||||
React.createElement(Component, {
|
||||
...props,
|
||||
t: (key: string) => nestedLookup(translations, key) || key,
|
||||
i18n: { language: 'zh-CN', changeLanguage: vi.fn() },
|
||||
});
|
||||
Wrapped.displayName = `withTranslation(${Component.displayName || Component.name || 'Component'})`;
|
||||
return Wrapped;
|
||||
};
|
||||
},
|
||||
initReactI18next: {
|
||||
type: '3rdParty',
|
||||
init: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
getMessage: (msgId: string) => zhMessages[msgId]?.message ?? msgId,
|
||||
getLanguage: () => 'zh',
|
||||
normalizeLanguage: (lng: string) => (lng.startsWith('zh') ? 'zh' : 'en'),
|
||||
SUPPORTED_LANGUAGES: ['zh', 'en'],
|
||||
preloadNamespaces: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
vi.mock('@/components/CopyButton', () => ({
|
||||
|
||||
Reference in New Issue
Block a user