-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (96 loc) · 2.17 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env node
const fs = require('fs');
const flatten = require('flat');
const unflatten = require('flat').unflatten;
const js2xliff = require('xliff/js2xliff');
const xliff2js = require('xliff/xliff2js');
const createjs = require('xliff/createjs');
const targetOfjs = require('xliff/targetOfjs');
saveToFile = (path, data) => {
fs.writeFile(path, data, (err) => {
if (err) {
console.error(err);
return false;
};
console.log("File has been created");
return true;
});
}
readFromFile = (path) => {
try {
return fs.readFileSync(path, 'utf8');
}
catch (err) {
return;
}
}
saveJsonToFile = (path, json) => {
saveToFile(path, JSON.stringify(unflatten(json), null, 2));
}
generateXliffFromJSON = (src, xliffPath, srcLang, trgLang) => {
let jsonData, xliffData;
const sourceJSON = require(src);
const targetJSON = Object.assign({}, sourceJSON, unflatten(getTargetJSONFromXliff(xliffPath)));
createjs(
srcLang,
trgLang,
flatten(sourceJSON),
flatten(targetJSON),
`translateFrom${srcLang}To${trgLang}`,
(err, res) => {
if (res) {
jsonData = res;
}
else if (err) {
console.log(err);
return false;
}
}
);
js2xliff(jsonData, (err, res) => {
if (res) {
xliffData = res;
}
else if (err) {
console.log(err);
return false;
}
});
return saveToFile(xliffPath, xliffData);
}
generateTargetJSONFromXliff = (xliffPath, tarJSON) => {
const targetJSON = getTargetJSONFromXliff(xliffPath);
saveJsonToFile(tarJSON, targetJSON);
}
getTargetJSONFromXliff = (xliffPath) => {
let jsonData, targetJSON;
const srcXliff = readFromFile(xliffPath);
if (srcXliff === undefined) {
return {};
}
xliff2js(srcXliff, (err, res) => {
if (res) {
jsonData = res;
}
else if (err) {
console.log(err);
return {};
}
});
targetOfjs(jsonData, (err, res) => {
if (res) {
targetJSON = res;
}
else if (err) {
console.log(err);
return {};
}
});
return targetJSON;
}
const j2x = generateXliffFromJSON;
const x2j = generateTargetJSONFromXliff;
module.exports = {
j2x,
x2j
};