Skip to content

Commit ee2a08e

Browse files
committed
Update lang-redirect.js
1 parent 2888a19 commit ee2a08e

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

app/lang-redirect.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const path = window.location.pathname || '/';
44

55
const ensureTrailingSlash = () => {
6-
// Only check strictly for the language folders we know about
6+
// Only normalize supported language folders.
77
if ((path.endsWith('/ja') || path.endsWith('/zh')) && !path.endsWith('/')) {
88
const target = `${path}/` + window.location.search + window.location.hash;
99
window.location.replace(target);
@@ -21,7 +21,8 @@
2121
const lower = String(value).toLowerCase();
2222
if (lower.startsWith('ja')) return 'ja';
2323
if (lower.startsWith('zh')) return 'zh';
24-
return 'en';
24+
if (lower.startsWith('en')) return 'en';
25+
return null;
2526
};
2627

2728
const getBasePath = () => {
@@ -63,7 +64,8 @@
6364
const redirectIfNeeded = (lang) => {
6465
const target = targetFor(lang);
6566
if (normalizePath(path) === normalizePath(target)) return;
66-
window.location.replace(target);
67+
const suffix = window.location.search + window.location.hash;
68+
window.location.replace(`${target}${suffix}`);
6769
};
6870

6971
const safeStorageGet = (key) => {
@@ -82,18 +84,11 @@
8284
}
8385
};
8486

87+
// Local storage reflects only explicit user choice (lang switch), not auto-detect.
8588
const saved = normalizeLang(safeStorageGet(STORAGE_KEY));
8689

87-
// If the user visits a specific language page (non-root/en),
88-
// trust that as the new preference and update storage accordingly.
89-
// 'en' is treated as a default fallback, so storage is not auto-updated just for landing on it,
90-
// unless explicitly switching or the logic below decides otherwise.
9190
if (currentLang !== 'en') {
92-
if (saved !== currentLang) {
93-
safeStorageSet(STORAGE_KEY, currentLang);
94-
}
95-
// Do NOT redirect if limits are already on a specific language page (ja/zh).
96-
// Being here implies the intent to view this specific language.
91+
// Respect direct visits to language pages; do not redirect or store a preference.
9792
} else {
9893
// Current location is the 'en' (root) page.
9994
// If a valid preference is saved for another language, redirect there.
@@ -110,13 +105,10 @@
110105
let detected = 'en';
111106
for (const lang of languageList) {
112107
const normalized = normalizeLang(lang);
113-
if (normalized === 'ja') {
114-
detected = 'ja';
108+
if (normalized === 'ja' || normalized === 'zh') {
109+
detected = normalized;
115110
break;
116111
}
117-
if (normalized === 'zh') {
118-
detected = 'zh';
119-
}
120112
}
121113
if (detected !== 'en') {
122114
redirectIfNeeded(detected);

0 commit comments

Comments
 (0)