imba-localization 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/localization.imba +44 -34
- package/package.json +1 -1
package/localization.imba
CHANGED
|
@@ -6,10 +6,19 @@ export class Localization
|
|
|
6
6
|
preferred = (window..navigator..language || 'en-US').slice(0, 2)
|
|
7
7
|
default
|
|
8
8
|
ready = false
|
|
9
|
-
|
|
9
|
+
err = {
|
|
10
|
+
cache: {}
|
|
11
|
+
throw: do(code, details)
|
|
12
|
+
return if err.cache[code]
|
|
13
|
+
if onerror isa Function
|
|
14
|
+
onerror(code, details)
|
|
15
|
+
else
|
|
16
|
+
console.log "Localization error:", code, details
|
|
17
|
+
err.cache[code] = true
|
|
18
|
+
}
|
|
10
19
|
|
|
11
|
-
def constructor url,
|
|
12
|
-
default =
|
|
20
|
+
def constructor url, fallback = 'en'
|
|
21
|
+
default = fallback
|
|
13
22
|
window.fetch(url)
|
|
14
23
|
.then(do(response) response.json!)
|
|
15
24
|
.then(do(data) _finalize(data, undefined))
|
|
@@ -19,35 +28,26 @@ export class Localization
|
|
|
19
28
|
get: do(target, p, receiver)
|
|
20
29
|
return Reflect.get(target, p, receiver) if self[p] !== undefined
|
|
21
30
|
if !ready
|
|
22
|
-
|
|
31
|
+
err.throw("Request before localization is ready:", p)
|
|
23
32
|
return
|
|
24
|
-
return if
|
|
33
|
+
return if err.cache[p]
|
|
25
34
|
return target.languages[p] if target.languages[p]
|
|
26
35
|
return target.languages[active][p] if target.languages[active] and target.languages[active][p]
|
|
27
|
-
|
|
28
|
-
onerror('no_localization_key', p) if onerror isa Function
|
|
29
|
-
errors[p] = true
|
|
36
|
+
err.throw('no_localization_key', p)
|
|
30
37
|
return ''
|
|
31
38
|
}
|
|
32
39
|
|
|
33
40
|
def _finalize data, error
|
|
34
|
-
if error
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if
|
|
43
|
-
|
|
44
|
-
else
|
|
45
|
-
console.log('There is no Localization for the default language', default)
|
|
46
|
-
return
|
|
47
|
-
ready = true
|
|
48
|
-
errors = {}
|
|
49
|
-
onready! if onready isa Function
|
|
50
|
-
onchange(active) if onchange isa Function
|
|
41
|
+
if error or !data
|
|
42
|
+
err.throw('no_localization_file',error)
|
|
43
|
+
elif !data[default]
|
|
44
|
+
err.throw('no_default_localization', default)
|
|
45
|
+
else
|
|
46
|
+
languages = data
|
|
47
|
+
ready = true
|
|
48
|
+
err.cache = {}
|
|
49
|
+
onready! if onready isa Function
|
|
50
|
+
onchange(active) if onchange isa Function
|
|
51
51
|
|
|
52
52
|
get active
|
|
53
53
|
const saved = window.localStorage.getItem('imba-localization')
|
|
@@ -60,7 +60,7 @@ export class Localization
|
|
|
60
60
|
if window.localStorage.getItem('imba-localization') != name
|
|
61
61
|
window.localStorage.setItem('imba-localization', name)
|
|
62
62
|
onchange(name) if onchange isa Function
|
|
63
|
-
|
|
63
|
+
err.cache = {}
|
|
64
64
|
|
|
65
65
|
export const path-arrow-down = <path d="M213.66,165.66a8,8,0,0,1-11.32,0L128,91.31,53.66,165.66a8,8,0,0,1-11.32-11.32l80-80a8,8,0,0,1,11.32,0l80,80A8,8,0,0,1,213.66,165.66Z">
|
|
66
66
|
|
|
@@ -83,16 +83,26 @@ tag language-selector
|
|
|
83
83
|
state.active = key
|
|
84
84
|
|
|
85
85
|
def flag language
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
86
|
+
const settings = language.$
|
|
87
|
+
if !settings
|
|
88
|
+
state.err.throw('no_localization_key', '$')
|
|
89
|
+
return undefined
|
|
90
|
+
const flag = settings.flag
|
|
91
|
+
if !flag
|
|
92
|
+
state.err.throw('no_localization_key', '$.flag')
|
|
93
|
+
return undefined
|
|
90
94
|
return icons.replace('##',flag)
|
|
91
95
|
|
|
92
96
|
def name language
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
97
|
+
const settings = language.$
|
|
98
|
+
if !settings
|
|
99
|
+
state.err.throw('no_localization_key', '$')
|
|
100
|
+
return undefined
|
|
101
|
+
const name = settings.name
|
|
102
|
+
if !name
|
|
103
|
+
state.err.throw('no_localization_key', '$.name')
|
|
104
|
+
return undefined
|
|
105
|
+
return name
|
|
96
106
|
|
|
97
107
|
def mouseleave e
|
|
98
108
|
return if passive
|
|
@@ -125,7 +135,7 @@ tag language-selector
|
|
|
125
135
|
<self [pos:rel] @mouseenter=mouseenter @mouseleave=mouseleave @click=click>
|
|
126
136
|
<div.container [pos:rel d:hcc] .active=#dropdown>
|
|
127
137
|
<img.flag src=flag(state[state.active])>
|
|
128
|
-
<div.name> state
|
|
138
|
+
<div.name> name(state[state.active])
|
|
129
139
|
<svg.arrow [ead:$ease] .active=#dropdown viewBox="0 0 256 256">
|
|
130
140
|
<{arrow}>
|
|
131
141
|
|