Quellcode durchsuchen

fix Chinese&English

jjyaoao vor 7 Monaten
Ursprung
Commit
c54290ac5a
1 geänderte Dateien mit 24 neuen und 9 gelöschten Zeilen
  1. 24 9
      docs/index.html

+ 24 - 9
docs/index.html

@@ -41,13 +41,23 @@
     <div id="app"></div>
     <div id="app"></div>
     <script src="//cdn.jsdelivr.net/npm/mermaid@8.0.0-rc.8/dist/mermaid.min.js"></script>
     <script src="//cdn.jsdelivr.net/npm/mermaid@8.0.0-rc.8/dist/mermaid.min.js"></script>
     <script>
     <script>
+        // 检查语言偏好并设置侧边栏
+        const preferredLang = localStorage.getItem('preferredLanguage');
+        const currentHash = window.location.hash;
+
+        // 判断当前应该使用哪个语言
+        let isEnglish = false;
+        if (preferredLang === 'en' || currentHash.includes('/en/')) {
+            isEnglish = true;
+        }
+
         window.$docsify = {
         window.$docsify = {
             name: 'Hello-Agents',
             name: 'Hello-Agents',
             repo: 'https://github.com/datawhalechina/Hello-Agents',
             repo: 'https://github.com/datawhalechina/Hello-Agents',
             loadSidebar: true,
             loadSidebar: true,
             auto2top: true,
             auto2top: true,
             subMaxLevel: 3,
             subMaxLevel: 3,
-            relativePath: false,  // 启用相对路径支持
+            relativePath: false,
             alias: {
             alias: {
                 // 英文路径映射
                 // 英文路径映射
                 '/en/README.md': '/README_EN.md',
                 '/en/README.md': '/README_EN.md',
@@ -55,18 +65,18 @@
                 '/en/_sidebar.md': '/_sidebar_en.md',
                 '/en/_sidebar.md': '/_sidebar_en.md',
                 '/en/chapter(\\d+)/Chapter(.*)': '/chapter$1/Chapter$2',
                 '/en/chapter(\\d+)/Chapter(.*)': '/chapter$1/Chapter$2',
 
 
-                // 默认中文路径保持不变
-                '/_sidebar.md': '/_sidebar.md'
+                // 根据语言偏好动态设置默认侧边栏
+                '/_sidebar.md': isEnglish ? '/_sidebar_en.md' : '/_sidebar.md'
             },
             },
             pagination: {
             pagination: {
-                previousText: '上一章节',
-                nextText: '下一章节',
+                previousText: isEnglish ? 'Previous' : '上一章节',
+                nextText: isEnglish ? 'Next' : '下一章节',
             },
             },
             count: {
             count: {
                 countable: true,
                 countable: true,
                 fontsize: '0.9em',
                 fontsize: '0.9em',
                 color: 'rgb(90,90,90)',
                 color: 'rgb(90,90,90)',
-                language: 'chinese'
+                language: isEnglish ? 'english' : 'chinese'
             },
             },
             // 多语言配置
             // 多语言配置
             fallbackLanguages: ['en'],
             fallbackLanguages: ['en'],
@@ -211,16 +221,19 @@
                 let path = currentHash.replace('#/', '');
                 let path = currentHash.replace('#/', '');
 
 
                 if (path === '' || path === '/') {
                 if (path === '' || path === '/') {
-                    window.location.hash = '#/en/README_EN.md';
+                    window.location.replace('#/en/README_EN.md');
+                    window.location.reload();
                 } else {
                 } else {
                     const parts = path.split('/');
                     const parts = path.split('/');
                     const filename = parts[parts.length - 1];
                     const filename = parts[parts.length - 1];
 
 
                     if (chapterMapping.zh2en[filename]) {
                     if (chapterMapping.zh2en[filename]) {
                         parts[parts.length - 1] = chapterMapping.zh2en[filename];
                         parts[parts.length - 1] = chapterMapping.zh2en[filename];
-                        window.location.hash = '#/en/' + parts.join('/');
+                        window.location.replace('#/en/' + parts.join('/'));
+                        window.location.reload();
                     }
                     }
                 }
                 }
+                return; // 停止执行,等待重新加载
             } else if (preferredLang === 'zh' && currentHash.includes('/en/')) {
             } else if (preferredLang === 'zh' && currentHash.includes('/en/')) {
                 // 用户偏好中文,但当前是英文页面
                 // 用户偏好中文,但当前是英文页面
                 let newHash = currentHash.replace('#/', '').replace('en/', '');
                 let newHash = currentHash.replace('#/', '').replace('en/', '');
@@ -229,8 +242,10 @@
 
 
                 if (chapterMapping.en2zh[filename]) {
                 if (chapterMapping.en2zh[filename]) {
                     parts[parts.length - 1] = chapterMapping.en2zh[filename];
                     parts[parts.length - 1] = chapterMapping.en2zh[filename];
-                    window.location.hash = '#/' + parts.join('/');
+                    window.location.replace('#/' + parts.join('/'));
+                    window.location.reload();
                 }
                 }
+                return; // 停止执行,等待重新加载
             }
             }
 
 
             // 设置按钮文本
             // 设置按钮文本