Răsfoiți Sursa

fix Chinese&English

jjyaoao 7 luni în urmă
părinte
comite
473d8ffebd
1 a modificat fișierele cu 36 adăugiri și 2 ștergeri
  1. 36 2
      docs/index.html

+ 36 - 2
docs/index.html

@@ -147,6 +147,8 @@
             // 检测当前语言
             if (currentHash.includes('/en/')) {
                 // 从英文切换到中文
+                localStorage.setItem('preferredLanguage', 'zh');
+
                 let newHash = currentHash.replace('#/', '').replace('en/', '');
 
                 // 提取文件名
@@ -167,6 +169,8 @@
                 window.$docsify.pagination.nextText = '下一章节';
             } else {
                 // 从中文切换到英文
+                localStorage.setItem('preferredLanguage', 'en');
+
                 let path = currentHash.replace('#/', '');
 
                 if (path === '' || path === '/') {
@@ -195,12 +199,42 @@
             window.location.reload();
         }
 
-        // 页面加载时设置按钮文本
+        // 页面加载时设置按钮文本和检查语言偏好
         window.addEventListener('load', function() {
             const currentHash = window.location.hash;
             const langBtn = document.getElementById('langBtn');
+            const preferredLang = localStorage.getItem('preferredLanguage');
 
-            if (currentHash.includes('/en/')) {
+            // 如果用户有语言偏好,且当前 URL 不匹配偏好,则自动切换
+            if (preferredLang === 'en' && !currentHash.includes('/en/')) {
+                // 用户偏好英文,但当前是中文页面
+                let path = currentHash.replace('#/', '');
+
+                if (path === '' || path === '/') {
+                    window.location.hash = '#/en/README_EN.md';
+                } else {
+                    const parts = path.split('/');
+                    const filename = parts[parts.length - 1];
+
+                    if (chapterMapping.zh2en[filename]) {
+                        parts[parts.length - 1] = chapterMapping.zh2en[filename];
+                        window.location.hash = '#/en/' + parts.join('/');
+                    }
+                }
+            } else if (preferredLang === 'zh' && currentHash.includes('/en/')) {
+                // 用户偏好中文,但当前是英文页面
+                let newHash = currentHash.replace('#/', '').replace('en/', '');
+                const parts = newHash.split('/');
+                const filename = parts[parts.length - 1];
+
+                if (chapterMapping.en2zh[filename]) {
+                    parts[parts.length - 1] = chapterMapping.en2zh[filename];
+                    window.location.hash = '#/' + parts.join('/');
+                }
+            }
+
+            // 设置按钮文本
+            if (currentHash.includes('/en/') || preferredLang === 'en') {
                 langBtn.textContent = '中文';
             } else {
                 langBtn.textContent = 'English';