Skip to content

Commit 61ea9ec

Browse files
committed
feat(i18n): auto-redirect based on browser language
Detect browser language preference and redirect Chinese users to /zh/ on first visit. Uses sessionStorage to prevent repeated redirects and respects explicit language preferences stored in localStorage.
1 parent 041986f commit 61ea9ec

1 file changed

Lines changed: 91 additions & 8 deletions

File tree

assets/js/language-toggle.js

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* Language Toggle for SGEMM Optimization Docs
33
* Handles switching between English and Chinese pages
4+
* Auto-redirects based on browser language preference
45
*/
56

67
(function() {
@@ -33,21 +34,80 @@
3334
'zh-kernel-tensor-core': { en: '/docs/kernel-tensor-core/', 'zh-CN': '/zh/docs/kernel-tensor-core/' }
3435
};
3536

37+
// Key for storing redirect state
38+
const REDIRECT_KEY = 'sgemm-lang-redirected';
39+
3640
// Get base URL from Jekyll site
3741
const baseurl = document.querySelector('meta[name="baseurl"]')?.content ||
3842
document.documentElement.getAttribute('data-baseurl') ||
3943
'/sgemm-optimization';
4044

4145
/**
42-
* Find the paired page path for a given page key and target language
46+
* Detect browser language preference
47+
* Returns 'zh-CN' for Chinese browsers, 'en' otherwise
4348
*/
44-
function findPairedPath(pageKey, targetLang) {
45-
const pair = pagePairs[pageKey];
46-
if (pair && pair[targetLang]) {
47-
return baseurl + pair[targetLang];
49+
function detectBrowserLanguage() {
50+
const browserLang = navigator.language || navigator.userLanguage || 'en';
51+
// Match zh-CN, zh-TW, zh-HK, zh-SG, or plain zh
52+
return /^zh/i.test(browserLang) ? 'zh-CN' : 'en';
53+
}
54+
55+
/**
56+
* Check if this session has already handled language redirect
57+
*/
58+
function hasRedirectedThisSession() {
59+
try {
60+
return sessionStorage.getItem(REDIRECT_KEY) === 'true';
61+
} catch (e) {
62+
return false;
63+
}
64+
}
65+
66+
/**
67+
* Mark that redirect has been handled this session
68+
*/
69+
function markRedirectHandled() {
70+
try {
71+
sessionStorage.setItem(REDIRECT_KEY, 'true');
72+
} catch (e) {
73+
// sessionStorage not available
74+
}
75+
}
76+
77+
/**
78+
* Check if user has explicitly set language preference
79+
*/
80+
function hasExplicitPreference() {
81+
try {
82+
return localStorage.getItem('preferred-doc-lang') !== null;
83+
} catch (e) {
84+
return false;
85+
}
86+
}
87+
88+
/**
89+
* Auto-redirect based on browser language (first visit only)
90+
*/
91+
function autoRedirect() {
92+
// Skip if already redirected this session or user has explicit preference
93+
if (hasRedirectedThisSession() || hasExplicitPreference()) {
94+
return;
95+
}
96+
97+
// Mark that we've handled redirect for this session
98+
markRedirectHandled();
99+
100+
const browserLang = detectBrowserLanguage();
101+
const currentPath = window.location.pathname;
102+
103+
// Check if we're on an English page (not /zh/)
104+
const isEnglishPage = !currentPath.includes('/zh/');
105+
const isHomePage = currentPath === baseurl + '/' || currentPath === baseurl;
106+
107+
// Only redirect from English home page to Chinese if browser is Chinese
108+
if (isHomePage && browserLang === 'zh-CN') {
109+
window.location.replace(baseurl + '/zh/');
48110
}
49-
// Fallback to language home
50-
return targetLang === 'zh-CN' ? baseurl + '/zh/' : baseurl + '/';
51111
}
52112

53113
/**
@@ -72,10 +132,22 @@
72132
}
73133
}
74134

135+
/**
136+
* Find the paired page path for a given page key and target language
137+
*/
138+
function findPairedPath(pageKey, targetLang) {
139+
const pair = pagePairs[pageKey];
140+
if (pair && pair[targetLang]) {
141+
return baseurl + pair[targetLang];
142+
}
143+
// Fallback to language home
144+
return targetLang === 'zh-CN' ? baseurl + '/zh/' : baseurl + '/';
145+
}
146+
75147
/**
76148
* Initialize language switcher functionality
77149
*/
78-
function init() {
150+
function initSwitcher() {
79151
const switcher = document.querySelector('.language-switcher');
80152
if (!switcher) return;
81153

@@ -102,6 +174,17 @@
102174
});
103175
}
104176

177+
/**
178+
* Initialize all language features
179+
*/
180+
function init() {
181+
// Run auto-redirect first (only on first visit)
182+
autoRedirect();
183+
184+
// Initialize switcher buttons
185+
initSwitcher();
186+
}
187+
105188
// Initialize when DOM is ready
106189
if (document.readyState === 'loading') {
107190
document.addEventListener('DOMContentLoaded', init);

0 commit comments

Comments
 (0)