-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (27 loc) · 1.03 KB
/
index.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
module.exports = Nanotranslate
function Nanotranslate (dict) {
// if (!dict.id) throw new Error('Nanotranslate: dict.id required')
if (!dict.values) throw new Error('Nanotranslate: dict.values required')
translate.id = dict.id
function translate (key, data) {
return Nanotranslate.run(dict, key, data)
}
return translate
}
Nanotranslate.run = function runTranslate (dict, key, data) {
var value = dict.values[key]
if (value === undefined) {
throw new TypeError('translate: "' + key + '" not found in dictionary "' + dict.id + '".')
}
if (Array.isArray(value)) {
var count = data != null ? data.count : undefined
if (typeof count !== 'number') {
throw new TypeError('translate: "' + key + '" is pluralized, second argument must be an object with field "count" as a number.')
}
value = value[Math.abs(count)] || value[value.length - 1]
}
for (var templateKey in data) {
value = value.replace(new RegExp('{{\\s*' + templateKey + '\\s*}}', 'g'), data[templateKey])
}
return value
}