-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.js
94 lines (82 loc) · 1.82 KB
/
translator.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
function Translator() {
this.supportLang = [ "en", "da", "fr", "sv", "nb", "nl" ];
this.supportExt = "properties";
this.allLangMap = [];
}
Translator.prototype = {
constructor : Translator,
indexOf : function(key) {
result = -1;
this.allLangMap.foreach(function(m, i) {
if (m.key == key)
result = i;
});
return result;
},
push : function(key, value, lang) {
var index = this.indexOf(key);
if (index >= 0) {
this.allLangMap[index][lang] = value;
} else {
var m = {};
m.key = key;
m[lang] = value;
this.allLangMap.push(m);
}
},
detectLang : function(fileName) {
var result = "en";
this.supportLang.foreach(function(lang) {
if (fileName.includes("_" + lang))
result = lang;
});
return result;
},
parse : function(file, content) {
var self = this;
var lang = this.detectLang(file.name);
var lines = content.split("\n");
lines.foreach(function(line) {
var str = line.trim();
if (!str || str.indexOf("#") === 0)
return;
var res = str.split("=");
self.push(res[0], res[1], lang);
});
},
getLangResult : function(lang) {
var result = "";
this.allLangMap.foreach(function(m) {
if (!isUndef(m[lang])) {
result += m.key + "=" + m[lang] + "\n";
}
});
return result;
},
generateZip : function() {
var zip = new JSZip();
var folder = zip.folder("I18N");
var self = this;
this.supportLang.foreach(function(lang) {
var result = "" + self.getLangResult(lang);
folder.file(lang + ".properties", result);
});
return zip.generate();
},
properties : function() {
var prop = [ "key" ];
this.supportLang.foreach(function(lang) {
prop.push(lang);
});
return prop;
},
data : function() {
return this.allLangMap;
},
clear : function() {
this.allLangMap.clear();
},
getSupportLang : function() {
return this.supportLang;
}
}