Skip to content

Commit 2e539f4

Browse files
feat(redirect): extend lang-redirect to support all docs locales
Replace the two-locale (root/en) redirect logic with a full locale alias map covering all ten docs locales. Add LOCALE_ROUTES and LOCALE_ALIASES lookup tables, update stripDocsLocalePrefix and buildLocaleUrl to handle arbitrary locale prefixes, and export the API for module consumers. Co-Authored-By: Hagicode <noreply@hagicode.com> Signed-off-by: newbe36524 <newbe36524@qq.com>
1 parent 93be9fa commit 2e539f4

1 file changed

Lines changed: 51 additions & 18 deletions

File tree

public/lang-redirect.js

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,40 @@
55

66
var DOCS_LANGUAGE_STORAGE_KEY = 'starlight-route';
77
var DEFAULT_DOCS_ENTRY_LOCALE = 'en';
8+
9+
var LOCALE_ROUTES = ['root', 'en', 'zh-Hant', 'ja-JP', 'ko-KR', 'de-DE', 'fr-FR', 'es-ES', 'pt-BR', 'ru-RU'];
10+
var LOCALE_ALIASES = {
11+
'en': 'en', 'en-us': 'en', 'en-gb': 'en', 'en-au': 'en', 'en-ca': 'en',
12+
'root': 'root', 'zh': 'root', 'zh-cn': 'root', 'zh-hans': 'root', 'zh-hans-cn': 'root',
13+
'zh-hant': 'zh-Hant', 'zh-tw': 'zh-Hant', 'zh-hk': 'zh-Hant', 'zh-hant-tw': 'zh-Hant', 'zh-hant-hk': 'zh-Hant',
14+
'ja': 'ja-JP', 'ja-jp': 'ja-JP',
15+
'ko': 'ko-KR', 'ko-kr': 'ko-KR',
16+
'de': 'de-DE', 'de-de': 'de-DE', 'de-at': 'de-DE', 'de-ch': 'de-DE',
17+
'fr': 'fr-FR', 'fr-fr': 'fr-FR', 'fr-ca': 'fr-FR', 'fr-be': 'fr-FR',
18+
'es': 'es-ES', 'es-es': 'es-ES', 'es-mx': 'es-ES', 'es-ar': 'es-ES',
19+
'pt': 'pt-BR', 'pt-br': 'pt-BR', 'pt-pt': 'pt-BR',
20+
'ru': 'ru-RU', 'ru-ru': 'ru-RU',
21+
};
22+
823
function parseDocsLocale(value) {
924
if (!value) {
1025
return null;
1126
}
1227

13-
var normalized = String(value).trim().toLowerCase();
28+
var normalized = String(value).trim().toLowerCase().replace(/_/g, '-');
1429
if (!normalized) {
1530
return null;
1631
}
1732

18-
if (normalized === 'en' || normalized.indexOf('en-') === 0) {
19-
return 'en';
33+
var mapped = LOCALE_ALIASES[normalized];
34+
if (mapped) {
35+
return mapped;
2036
}
2137

22-
if (
23-
normalized === 'root' ||
24-
normalized === 'zh' ||
25-
normalized === 'zh-cn' ||
26-
normalized.indexOf('zh-') === 0
27-
) {
28-
return 'root';
38+
for (var i = 0; i < LOCALE_ROUTES.length; i++) {
39+
if (normalized === LOCALE_ROUTES[i].toLowerCase()) {
40+
return LOCALE_ROUTES[i];
41+
}
2942
}
3043

3144
return null;
@@ -36,15 +49,28 @@
3649
}
3750

3851
function stripDocsLocalePrefix(pathname) {
39-
if (!pathname || pathname === '/en') {
52+
if (!pathname) {
4053
return '/';
4154
}
4255

43-
if (pathname.indexOf('/en/') === 0) {
44-
return pathname.slice(3) || '/';
56+
var normalized = pathname.charAt(0) === '/' ? pathname : '/' + pathname;
57+
58+
for (var i = 0; i < LOCALE_ROUTES.length; i++) {
59+
var routeLocale = LOCALE_ROUTES[i];
60+
if (routeLocale === 'root') {
61+
continue;
62+
}
63+
64+
if (normalized === '/' + routeLocale) {
65+
return '/';
66+
}
67+
68+
if (normalized.indexOf('/' + routeLocale + '/') === 0) {
69+
return normalized.slice(routeLocale.length + 1) || '/';
70+
}
4571
}
4672

47-
return pathname;
73+
return normalized;
4874
}
4975

5076
function isLandingRoutePath(pathname) {
@@ -58,7 +84,11 @@
5884
return normalizedPath === '/' ? '/en/' : '/en' + normalizedPath;
5985
}
6086

61-
return normalizedPath || '/';
87+
if (locale === 'root') {
88+
return normalizedPath || '/';
89+
}
90+
91+
return normalizedPath === '/' ? '/' + locale + '/' : '/' + locale + normalizedPath;
6292
}
6393

6494
function getStoredDocsLocale(storageValue) {
@@ -203,7 +233,7 @@
203233
storedLocale: storedLocale,
204234
isLandingPath: landingPath,
205235
shouldPersist: shouldPersist,
206-
shouldRedirect: targetUrl.toString() !== currentUrl.toString(),
236+
shouldRedirect: targetUrl.toString() !== currentUrl.toString() && (landingPath || requestedLang !== null),
207237
};
208238
}
209239

@@ -268,6 +298,9 @@
268298
},
269299
};
270300

271-
window.__HAGICODE_DOCS_ENTRY__ = api;
272-
api.applyEntryRouting(window);
301+
api.applyEntryRouting();
302+
303+
if (typeof module === 'object' && module && typeof module.exports === 'object') {
304+
module.exports = api;
305+
}
273306
})();

0 commit comments

Comments
 (0)