-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxliffCreator.js
More file actions
182 lines (147 loc) · 5.21 KB
/
xliffCreator.js
File metadata and controls
182 lines (147 loc) · 5.21 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
var cheerio = require('cheerio');
var xliffCreator = {
parsers: {}
};
// PARSING
// Splits sentences based upon punctutation
var senSplitter = new RegExp(".{0,}?(?:\\.|!|\\?)(?:(?=\\ [A-Z0-9])|$)", "g");
xliffCreator.parsers.splitSentences = function(text) {
return text.trim().match(senSplitter).map(function(sen) {
return sen.trim();
});
};
xliffCreator.parsers.splitLines = function(text) {
return text.trim().split('\n').map(function(sen) {
return sen.trim();
});
};
// END PARSING
// Creating XLIFF Documents from monolingual strings
// - these functions handle the usecase where user has a rawtext file of strings that they want to translate
// The skeleton for an Xliff2 document
// note that the file id is hard-coded here
var xliff2Skeleton = '<?xml version="1.0"?>' +
'<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0">' +
'<file id="f1">' +
'</file>' +
'</xliff>';
var xliff2UnitTemplate = '<unit>' +
'<segment>' +
'<source/>' +
'<target></target>' +
'</segment>' +
'</unit>';
// The skeleton for an Xliff1 document
// note that the file id is hard-coded here
var xliff1Skeleton = '<?xml version="1.0"?>' +
'<xliff version="1.2">' +
'<file id="f1">' +
'<body></body>' +
'</file>' +
'</xliff>';
var xliff1TransUnitTemplate = '<trans-unit >' +
'<source/>' +
'<target/>' +
'</trans-unit>';
xliffCreator.createXlf2FromSourceText = function(sourceLang, targetLang, sourceSens) {
// create a new XLIFF Skeleton
var $xliff = cheerio.load(xliff2Skeleton, {
xmlMode : true,
decodeEntities : true,
normalizeWhitespace: true
});
// set srcLang and trgLang attrs
$xliff('xliff').attr('srcLang', sourceLang);
$xliff('xliff').attr('trgLang', targetLang);
for (var senIdx in sourceSens) {
var $newUnit = cheerio.load(xliff2UnitTemplate, {
xmlMode : true,
decodeEntities : true,
normalizeWhitespace: true
});
// <unit> elements must have an id
$newUnit('unit').attr('id', 'u' + senIdx);
// set the source text of this element
$newUnit('source').text(sourceSens[senIdx]);
$xliff('file').append($newUnit.xml());
}
return $xliff.xml();
}
// Create an XLIFF 1.2 file from source text
xliffCreator.createXlf1FromSourceText = function(sourceLang, targetLang, sourceSens) {
// create a new XLIFF Skeleton
var $xliff = cheerio.load(xliff1Skeleton, {
xmlMode : true,
decodeEntities : true,
normalizeWhitespace: true
});
// set srcLang and trgLang attrs
$xliff('file').attr('source-language', sourceLang);
$xliff('file').attr('target-language', targetLang);
for (var senIdx in sourceSens) {
var $newUnit = cheerio.load(xliff1TransUnitTemplate, {
xmlMode : true,
decodeEntities : true,
normalizeWhitespace: true
});
// <unit> elements must have an id
$newUnit('trans-unit').attr('id', 'tu' + senIdx);
// set the source text of this element
$newUnit('source').text(sourceSens[senIdx]);
$xliff('body').append($newUnit.xml());
}
return $xliff.xml();
}
// Creating XLIFFs from two lists -- source + target sentences
xliffCreator.createXlf2FromSourceAndTargetText = function(sourceLang, targetLang, sourceSens, targetSens) {
// create a new XLIFF Skeleton
var $xliff = cheerio.load(xliff2Skeleton, {
xmlMode : true,
decodeEntities : true,
normalizeWhitespace: true
});
// set srcLang and trgLang attrs
$xliff('xliff').attr('srcLang', sourceLang);
$xliff('xliff').attr('trgLang', targetLang);
for (var senIdx in sourceSens) {
var $newUnit = cheerio.load(xliff2UnitTemplate, {
xmlMode : true,
decodeEntities : true,
normalizeWhitespace: true
});
// <unit> elements must have an id
$newUnit('unit').attr('id', 'u' + senIdx);
// set the source and target text of this element
$newUnit('source').text(sourceSens[senIdx]);
$newUnit('target').text(targetSens[senIdx]);
$xliff('file').append($newUnit.xml());
}
return $xliff.xml();
}
// Create an XLIFF 1.2 file from source text
xliffCreator.createXlf1FromSourceAndTargetText = function(sourceLang, targetLang, sourceSens, targetSens) {
// create a new XLIFF Skeleton
var $xliff = cheerio.load(xliff1Skeleton, {
xmlMode : true,
decodeEntities : true,
normalizeWhitespace: true
});
// set srcLang and trgLang attrs
$xliff('file').attr('source-language', sourceLang);
$xliff('file').attr('target-language', targetLang);
for (var senIdx in sourceSens) {
var $newUnit = cheerio.load(xliff1TransUnitTemplate, {
xmlMode : true,
decodeEntities : true,
normalizeWhitespace: true
});
// <unit> elements must have an id
$newUnit('trans-unit').attr('id', 'tu' + senIdx);
// set the source and target text of this element
$newUnit('source').text(sourceSens[senIdx]);
$newUnit('target').text(targetSens[senIdx]);
$xliff('body').append($newUnit.xml());
}
return $xliff.xml();
}
module.exports = xliffCreator;