Repository navigation
Expand file tree
/
Copy pathlanguage-route.js
More file actions
119 lines (97 loc) · 3.24 KB
/
Copy pathlanguage-route.js
File metadata and controls
119 lines (97 loc) · 3.24 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
(function () {
if (window.__teableDocsLanguageRouteInitialized) {
return;
}
window.__teableDocsLanguageRouteInitialized = true;
var locales = ["en", "zh", "ru", "de", "fr", "es", "it", "ja", "tr", "uk", "ar"];
var localeCookie = "TEABLE_DOCS_LOCALE";
var routerPaths = ["", "/index", "/en/locale-router"];
function isRouterPath(pathname) {
return routerPaths.indexOf(pathname.replace(/\/$/, "")) !== -1;
}
var lastObservedPath = window.location.pathname;
function localeFromPath(pathname) {
var firstSegment = pathname.split("/")[1].toLowerCase();
return locales.indexOf(firstSegment) === -1 ? null : firstSegment;
}
function localeFromTag(tag) {
var normalized = String(tag || "").trim().toLowerCase();
for (var index = 0; index < locales.length; index += 1) {
var locale = locales[index];
if (normalized === locale || normalized.indexOf(locale + "-") === 0) {
return locale;
}
}
return null;
}
function readLocaleCookie() {
var prefix = localeCookie + "=";
var cookies = document.cookie.split(";");
for (var index = 0; index < cookies.length; index += 1) {
var cookie = cookies[index].trim();
if (cookie.indexOf(prefix) === 0) {
try {
return localeFromTag(decodeURIComponent(cookie.slice(prefix.length)));
} catch (_) {
return null;
}
}
}
return null;
}
function writeLocaleCookie(locale) {
document.cookie =
localeCookie + "=" + encodeURIComponent(locale) + "; Path=/; Max-Age=31536000; SameSite=Lax";
}
function preferredBrowserLocale() {
var browserLanguages = navigator.languages && navigator.languages.length
? navigator.languages
: [navigator.language];
for (var index = 0; index < browserLanguages.length; index += 1) {
var locale = localeFromTag(browserLanguages[index]);
if (locale) {
return locale;
}
}
return "en";
}
function rememberLanguageSwitcherChoice(event) {
var link = event.target.closest && event.target.closest("a[href]");
if (!link) {
return;
}
var target = new URL(link.href, window.location.origin);
if (target.origin !== window.location.origin) {
return;
}
var currentLocale = localeFromPath(window.location.pathname);
var targetLocale = localeFromPath(target.pathname);
if (currentLocale && targetLocale && currentLocale !== targetLocale) {
writeLocaleCookie(targetLocale);
}
}
function rememberCurrentPageLocale() {
var currentPath = window.location.pathname;
if (currentPath === lastObservedPath) {
return;
}
lastObservedPath = currentPath;
if (isRouterPath(currentPath)) {
return;
}
var locale = localeFromPath(currentPath);
if (locale) {
writeLocaleCookie(locale);
}
}
document.addEventListener("click", rememberLanguageSwitcherChoice, true);
window.addEventListener("popstate", rememberCurrentPageLocale);
window.setInterval(rememberCurrentPageLocale, 250);
if (!isRouterPath(window.location.pathname)) {
return;
}
var locale = readLocaleCookie() || preferredBrowserLocale();
window.location.replace(
"/" + locale + window.location.search + window.location.hash
);
})();