forked from LeftValues/leftvalues.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.js
144 lines (130 loc) · 3.72 KB
/
i18n.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// SECTIONS MODIFIED FROM LV ARE COMMENTED (yymmdd.hhmm)
// LANGUAGE SETTINGS
function getCookie (name) {
const value = '; ' + document.cookie
const parts = value.split('; ' + name + '=')
if (parts.length === 2) return parts.pop().split(';').shift()
}
function setCookie (cname, cvalue, exhours) {
const d = new Date()
d.setTime(Date.now() + (exhours * 60 * 60 * 1000))
const expires = 'expires=' + d.toUTCString()
document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/'
}
function setBodyClassUserLang (userLang) {
document.getElementsByTagName('body')[0].classList.add(`__i18n-${userLang}`)
}
const langElements = document.querySelectorAll('[data-i18n]')
const userLang = getCookie('lang') || navigator.language || navigator.userLanguage
setBodyClassUserLang(userLang)
const langPicker = document.getElementById('langPicker')
//MODIFIED FROM LV (210820.1412)
var REPLACEMENTS = {
colors: {
con: '#890000', //conservative
lf: '#88232B', //liberal f
rf: '#560000', //radical f
sf: '#782F52', //socialist f
cu: '#963B33', //cultural
wc: '#804E00' //women of color
}
}
const replace = (string, values = null) => {
if (values) {
Object.getOwnPropertyNames(values).forEach((val, idx, array) => {
REPLACEMENTS[val] = values[val]
})
}
const re = /{{([\w\.]+)}}/
let m
do {
m = re.exec(string)
if (m) {
const x = m[1].split('.')
switch (x.length) {
case 1:
string = string.replace(m[0], REPLACEMENTS[x[0]])
break
case 2:
string = string.replace(m[0], REPLACEMENTS[x[0]][x[1]])
break
case 3:
string = string.replace(m[0], REPLACEMENTS[x[0]][x[1]][x[2]])
break
default:
console.error('nested replacements only allowed up to 3 levels')
break
}
}
} while (m)
return string
}
const i18n = {
getString (name, parameters = null) {
if (userLang in i18n.l10n && name in i18n.l10n[userLang]) {
if (parameters) {
return replace(i18n.l10n[userLang][name], parameters)
}
return replace(i18n.l10n[userLang][name])
}
console.log(`using fallback for ${name}`)
if (parameters) {
return replace(i18n.l10n.en[name], parameters)
}
return replace(i18n.l10n.en[name])
},
lang: userLang,
l10n: {}
}
const addLanguage = (code, properties) => {
i18n.l10n[code] = properties
// Language picker
if (langPicker) {
const option = document.createElement('option')
option.text = i18n.l10n[code].language_name
option.value = code
if (option in langPicker.options) {
} else {
langPicker.add(option)
}
if (option.value === userLang) {
langPicker.value = userLang
}
}
}
const getl10n = async (code) => {
const url = `/lang/lang_${code}.json`
await window.fetch(url)
.then(res => res.json())
.then(out => addLanguage(code, out))
.catch(err => { throw err })
}
// MODIFIED FROM LV (210820.1413)
async function loadTranslation () { // eslint-disable-line no-unused-vars
return Promise.all([
getl10n('en'),
getl10n('ja')
]).then(() => {
console.log(i18n)
langElements.forEach((element) => {
const value = element.getAttribute('data-i18n')
element.innerHTML = i18n.getString(value)
})
if (langPicker) {
langPicker.onchange = function () {
const language = langPicker.options[langPicker.selectedIndex].value
setCookie('lang', language, 5)
window.location.reload()
}
}
})
}
function delayPromise (duration) { // eslint-disable-line no-unused-vars
return function (...args) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(...args)
}, duration)
})
}
}