|
@@ -147,6 +147,8 @@
|
|
|
// 检测当前语言
|
|
// 检测当前语言
|
|
|
if (currentHash.includes('/en/')) {
|
|
if (currentHash.includes('/en/')) {
|
|
|
// 从英文切换到中文
|
|
// 从英文切换到中文
|
|
|
|
|
+ localStorage.setItem('preferredLanguage', 'zh');
|
|
|
|
|
+
|
|
|
let newHash = currentHash.replace('#/', '').replace('en/', '');
|
|
let newHash = currentHash.replace('#/', '').replace('en/', '');
|
|
|
|
|
|
|
|
// 提取文件名
|
|
// 提取文件名
|
|
@@ -167,6 +169,8 @@
|
|
|
window.$docsify.pagination.nextText = '下一章节';
|
|
window.$docsify.pagination.nextText = '下一章节';
|
|
|
} else {
|
|
} else {
|
|
|
// 从中文切换到英文
|
|
// 从中文切换到英文
|
|
|
|
|
+ localStorage.setItem('preferredLanguage', 'en');
|
|
|
|
|
+
|
|
|
let path = currentHash.replace('#/', '');
|
|
let path = currentHash.replace('#/', '');
|
|
|
|
|
|
|
|
if (path === '' || path === '/') {
|
|
if (path === '' || path === '/') {
|
|
@@ -195,12 +199,42 @@
|
|
|
window.location.reload();
|
|
window.location.reload();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 页面加载时设置按钮文本
|
|
|
|
|
|
|
+ // 页面加载时设置按钮文本和检查语言偏好
|
|
|
window.addEventListener('load', function() {
|
|
window.addEventListener('load', function() {
|
|
|
const currentHash = window.location.hash;
|
|
const currentHash = window.location.hash;
|
|
|
const langBtn = document.getElementById('langBtn');
|
|
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 = '中文';
|
|
langBtn.textContent = '中文';
|
|
|
} else {
|
|
} else {
|
|
|
langBtn.textContent = 'English';
|
|
langBtn.textContent = 'English';
|