-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
40 lines (35 loc) · 1.09 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
const FALLBACK_LOCALE = 'en'
const MAPPED_LOCALES = new Map([
[FALLBACK_LOCALE, FALLBACK_LOCALE],
['zh', 'zh-CN'],
['zh-CN', 'zh-CN'],
])
function resolveNavigatorLocale() {
return navigator.language.replace(/^([a-z]{2})(?:-[a-z]+)??-([A-Z]{2})$/, '$1-$2')
}
function resolveEffectiveLocale(navigatorLocale) {
let mapped = MAPPED_LOCALES.get(navigatorLocale)
if (mapped) {
return mapped
} else if (navigatorLocale?.length > 2) {
const lang = navigatorLocale.substring(0, 2)
mapped = MAPPED_LOCALES.get(lang)
if(mapped) return mapped
}
return FALLBACK_LOCALE
}
const locale = resolveEffectiveLocale(resolveNavigatorLocale())
const {origin, pathname, search} = window.location
const target = origin + pathname.substring(0, pathname.lastIndexOf('/')) + `/${locale}/index.html${search}`
// console.debug("[DEBUG] Redirecting to %s ...", target)
window.location.href = target
</script>
</head>
<body>
</body>
</html>