|
3 | 3 | const path = window.location.pathname || '/'; |
4 | 4 |
|
5 | 5 | const ensureTrailingSlash = () => { |
6 | | - // Only check strictly for the language folders we know about |
| 6 | + // Only normalize supported language folders. |
7 | 7 | if ((path.endsWith('/ja') || path.endsWith('/zh')) && !path.endsWith('/')) { |
8 | 8 | const target = `${path}/` + window.location.search + window.location.hash; |
9 | 9 | window.location.replace(target); |
|
21 | 21 | const lower = String(value).toLowerCase(); |
22 | 22 | if (lower.startsWith('ja')) return 'ja'; |
23 | 23 | if (lower.startsWith('zh')) return 'zh'; |
24 | | - return 'en'; |
| 24 | + if (lower.startsWith('en')) return 'en'; |
| 25 | + return null; |
25 | 26 | }; |
26 | 27 |
|
27 | 28 | const getBasePath = () => { |
|
63 | 64 | const redirectIfNeeded = (lang) => { |
64 | 65 | const target = targetFor(lang); |
65 | 66 | 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}`); |
67 | 69 | }; |
68 | 70 |
|
69 | 71 | const safeStorageGet = (key) => { |
|
82 | 84 | } |
83 | 85 | }; |
84 | 86 |
|
| 87 | + // Local storage reflects only explicit user choice (lang switch), not auto-detect. |
85 | 88 | const saved = normalizeLang(safeStorageGet(STORAGE_KEY)); |
86 | 89 |
|
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. |
91 | 90 | 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. |
97 | 92 | } else { |
98 | 93 | // Current location is the 'en' (root) page. |
99 | 94 | // If a valid preference is saved for another language, redirect there. |
|
110 | 105 | let detected = 'en'; |
111 | 106 | for (const lang of languageList) { |
112 | 107 | const normalized = normalizeLang(lang); |
113 | | - if (normalized === 'ja') { |
114 | | - detected = 'ja'; |
| 108 | + if (normalized === 'ja' || normalized === 'zh') { |
| 109 | + detected = normalized; |
115 | 110 | break; |
116 | 111 | } |
117 | | - if (normalized === 'zh') { |
118 | | - detected = 'zh'; |
119 | | - } |
120 | 112 | } |
121 | 113 | if (detected !== 'en') { |
122 | 114 | redirectIfNeeded(detected); |
|
0 commit comments