Skip to content

Commit 3a53df3

Browse files
committed
fix: 完善国际化支持,修复剩余未国际化的内容和CSS元素
1 parent fe2432f commit 3a53df3

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

js/i18n/i18n-case-detail.js

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class I18nCaseDetail {
3232
// 更新页面中的按钮标签
3333
this.updateTestCaseButtons();
3434

35+
// 强制替换所有JavaScript中的硬编码状态文本
36+
this.patchJavaScriptTexts();
37+
3538
// 在DOM加载完成后动态处理"测试完成"的显示
3639
setTimeout(() => {
3740
const resultMessage = document.querySelector('.result-message');
@@ -44,9 +47,72 @@ class I18nCaseDetail {
4447
if (stopButton) {
4548
stopButton.textContent = this.i18n.currentLang === 'zh-CN' ? '停止测试' : 'Stop Test';
4649
}
50+
51+
// 替换导航菜单中的文本
52+
this.updateNavigation();
4753
}, 100);
4854
}
4955

56+
// 替换导航菜单中的文本
57+
updateNavigation() {
58+
const homeLink = document.querySelector('a[href="../../index.html"]');
59+
if (homeLink) {
60+
const icon = homeLink.querySelector('i');
61+
homeLink.innerHTML = '';
62+
if (icon) homeLink.appendChild(icon);
63+
homeLink.appendChild(document.createTextNode(' ' + this.i18n.t('nav.home')));
64+
}
65+
66+
const casesLink = document.querySelector('a[href="../../cases/index.html"]');
67+
if (casesLink) {
68+
const icon = casesLink.querySelector('i');
69+
casesLink.innerHTML = '';
70+
if (icon) casesLink.appendChild(icon);
71+
casesLink.appendChild(document.createTextNode(' ' + this.i18n.t('nav.testCases')));
72+
}
73+
}
74+
75+
// 替换JavaScript中的硬编码中文文本
76+
patchJavaScriptTexts() {
77+
// 获取当前语言,保存为常量
78+
const currentLang = this.i18n.currentLang;
79+
80+
// 直接替换脚本中的常见状态文本
81+
const scripts = document.querySelectorAll('script:not([src])');
82+
scripts.forEach(script => {
83+
if (script.textContent.includes('测试运行中') || script.textContent.includes('测试完成')) {
84+
// 创建一个新的脚本元素,替换原始脚本中的状态文本
85+
const newScript = document.createElement('script');
86+
let newContent = script.textContent;
87+
88+
// 替换常见状态文本
89+
newContent = newContent.replace(/['"]\.\.\.['"]|['"]...['"]|['"]Test Running...['"]/,
90+
`(window.currentLang === 'zh-CN' ? '测试运行中...' : 'Test Running...')`);
91+
92+
newContent = newContent.replace(/['"]['"]|['"]Test Complete['"]/,
93+
`(window.currentLang === 'zh-CN' ? '测试完成' : 'Test Complete')`);
94+
95+
// setInterval特有的文本
96+
if (document.querySelector('.test-container h2')?.textContent.includes('setInterval')) {
97+
newContent = newContent.replace(/['"]\.['"]/,
98+
`(window.currentLang === 'zh-CN' ? '测试期间未检测到任何断点暂停。' : 'No breakpoint pauses detected during the test.')`);
99+
100+
newContent = newContent.replace(/['"]\.['"]/,
101+
`(window.currentLang === 'zh-CN' ? '测试期间检测到执行被断点中断。' : 'Execution was interrupted by a breakpoint during the test.')`);
102+
103+
newContent = newContent.replace(/['"]['"]|['"]Stop Test['"]/,
104+
`(window.currentLang === 'zh-CN' ? '停止测试' : 'Stop Test')`);
105+
}
106+
107+
// 在脚本开头添加当前语言变量
108+
newContent = `window.currentLang = "${currentLang}";\n` + newContent;
109+
110+
newScript.textContent = newContent;
111+
script.parentNode.replaceChild(newScript, script);
112+
}
113+
});
114+
}
115+
50116
// 更新测试用例标题
51117
updateCaseTitle(titleElement) {
52118
const titleText = titleElement.textContent.trim();
@@ -172,8 +238,21 @@ class I18nCaseDetail {
172238
const codeBlock = document.querySelector('.code-block');
173239
if (!codeBlock) return;
174240

241+
// 创建完整的样式覆盖,强制覆盖原有的content属性值
175242
const style = document.createElement('style');
176-
style.textContent = `.code-block::before { content: "${this.i18n.t('testCase.codeLabel')}"; }`;
243+
style.textContent = `
244+
.code-block::before {
245+
content: "${this.i18n.t('testCase.codeLabel')}" !important;
246+
position: absolute;
247+
top: -12px;
248+
left: 1rem;
249+
background: var(--primary-color);
250+
color: white;
251+
padding: 0.25rem 0.75rem;
252+
border-radius: 12px;
253+
font-size: 0.875rem;
254+
}
255+
`;
177256

178257
// 检查是否已经添加过样式
179258
const existingStyle = document.querySelector('style[data-i18n-code-label]');

update_comments.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)