-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (72 loc) · 2.69 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
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
var check = require('./src/check');
var tool = require('./src/tool');
var TRANSLATION = require('linguacode-translations');
var languageValidation = function(lng) {
if (!TRANSLATION[lng]) {
throw new Error('Language was not specified.');
}
};
var toCode = exports.toCode = function (data, lng) {
languageValidation(lng);
data = data || '';
var re, reStr;
return data
.split('\n')
.map(function (line) {
for (var i = 0; i < TRANSLATION[lng].length; i++) {
var instance = TRANSLATION[lng][i];
var definition = instance.definition;
re = new RegExp('[^@](' + definition + ')|^' + definition, 'ig');
while ((reStr = re.exec(line)) !== null) { //in line
var index = reStr[1] ? reStr.index + 1 : reStr.index;
var value = reStr[1] ? reStr[1] : reStr[0];
var isPartOfCode = check.isPartOfCode(line, index);
var isPartOfCommand = check.isPartOfCommand(line, value, index);
if (isPartOfCode && !isPartOfCommand) {
var toReplace = instance.command.replace(/\\/g, '');
var firstPartEndIndex = index;
var secondPartBeginIndex = index + value.length;
line = tool.partitionReplace(line, toReplace, firstPartEndIndex, secondPartBeginIndex);
re.lastIndex = firstPartEndIndex + toReplace.length;
}
}
}
return line;
})
.join('\n');
};
var toText = exports.toText = function (data, lng) {
languageValidation(lng);
data = data || '';
var re, reStr;
return data
.split('\n')
.map(function (line) {
line = ' ' + line + ' ';
for (var i = 0; i < TRANSLATION[lng].length; i++) {
var instance = TRANSLATION[lng][i];
var command = instance.command;
re = new RegExp(command, 'ig');
while ((reStr = re.exec(line)) !== null) { //in line
var index = reStr.index;
var value = reStr[0];
var isPartOfCode = check.isPartOfCode(line, index);
var isPartOfCommand = check.isPartOfCommand(line, value, index);
if (isPartOfCode && !isPartOfCommand) {
var toReplace = instance.definition.split('|')[0];
var firstPartEndIndex = index;
var secondPartBeginIndex = index + value.length;
line = tool.partitionReplace(line, toReplace, firstPartEndIndex, secondPartBeginIndex);
re.lastIndex = firstPartEndIndex + toReplace.length;
}
}
}
return line.slice(1, -1);
})
.join('\n');
};
exports.translate = function (source, languageInitial, languageDestination) {
var code = toCode(source, languageInitial);
var text = toText(code, languageDestination);
return text;
};