-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
83 lines (74 loc) · 2.17 KB
/
util.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
var localesJA = require('./locales/ja.json')
var localesEN = require('./locales/en.json')
var localesZH = require('./locales/zh.json')
var localesZH_TW = require('./locales/zh_tw.json')
var localesKO = require('./locales/ko.json')
exports.getLocales = getLocales
function getLocales (language) {
switch (language) {
case 'ja':
return localesJA
case 'en':
return localesEN
case 'zh':
return localesZH
case 'zh_tw':
return localesZH_TW
case 'ko':
return localesKO
}
return {}
}
exports.translateLocales = translateLocales
function translateLocales (language, keys) {
var originLocales = getLocales(language)
var locales = {}
keys.forEach(function (key) {
locales[key] = originLocales[key] || ''
})
return translate(language, locales)
}
exports.mergeLocales = mergeLocales
function mergeLocales (language, description) {
var originLocales = getLocales(language)
var keys = Object.keys(description)
var locales = {}
keys.forEach(function (key) {
locales[key] = originLocales[key] || description[key] || ''
})
return translate(language, locales)
}
exports.translate = translate
function translate (language, locales) {
return 'i18n.setLocales(\'' + language + '\', ' + JSON.stringify(locales, null, 2) + ');'
}
var NAMESPACE_MARK = '@namespace:'
exports.parseDescription = parseDescription
function parseDescription (content) {
var set = parseDescriptionSet(content)
var namespace = set.namespace
var description = set.description
if (namespace) {
var result = {}
var keys = Object.keys(description)
for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i]
result[namespace + key] = description[key]
}
return result
} else {
return description
}
}
exports.parseDescriptionSet = parseDescriptionSet
function parseDescriptionSet (content) {
var namespace = ''
var lines = content.trim().split('\n')
var first = (lines[0] || '').trim()
if (first.indexOf(NAMESPACE_MARK) === 0) {
namespace = first.slice(NAMESPACE_MARK.length).trim()
lines.shift()
}
var description = JSON.parse(lines.join('\n'))
return {namespace: namespace, description: description}
}