-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki.html
80 lines (70 loc) · 1.94 KB
/
wiki.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
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
---
title: Wiki
permalink: wiki/
---
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
const target = location.search.substring(1) + location.hash;
const defaultLocale = '{{ site.lang | downcase }}';
const routing = {
{% for site_page in site.pages %}
{% if site_page.path contains 'wiki/' and site_page.path contains '/index.md' %}
"{{ site_page.lang | downcase }}": '{{ site_page.lang }}/' + target,
{% endif %}
{% endfor %}
};
{% if jekyll.environment == 'production' %}
const enableLog = false;
{% else %}
const enableLog = true;
{% endif %}
function log(message) {
if (enableLog) {
console.log(message);
}
}
function getBrowserLanguage() {
if (!navigator) return null;
if (navigator.languages && navigator.languages.length > 0) return navigator.languages[0];
if (navigator.userLanguage) return navigator.userLanguage;
if (navigator.browserLanguage) return navigator.browserLanguage;
return navigator.language;
}
function localeSimilarity(locale1, locale2) {
if (!locale1 || !locale2) return 0;
locale1 = locale1.split('-');
locale2 = locale2.split('-');
if (!locale1 || !locale2) return 0;
let result = 0;
while (locale1.shift() === locale2.shift())
result++;
return result;
}
function targetUrl(locale) {
log('matching locale ' + locale + ' ...');
if (routing[locale])
return routing[locale];
log('no route found for locale ' + locale);
for (availableLocale in routing) {
if (localeSimilarity(locale, availableLocale) > 0) {
log('found similar locale ' + availableLocale);
return routing[availableLocale];
}
}
log('no similar locale found, using default locale ' + defaultLocale);
return routing[defaultLocale];
}
function redirectToRoute(locale) {
route = targetUrl(locale)
log('will redirect to ' + route);
window.location.replace(route);
}
var locale = getBrowserLanguage() || defaultLocale;
log('browser locale: ' + locale);
locale = locale.toLowerCase();
redirectToRoute(locale);
</script>
</body>
</html>