-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgulpfile.js
171 lines (150 loc) · 5.09 KB
/
gulpfile.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
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
/*************************************
* Requires
*************************************/
var gulp = require("gulp");
var rename = require("gulp-rename");
var modifyFile = require("gulp-modify-file");
var async = require("async");
var cheerio = require("gulp-cheerio");
var pd = require("pretty-data").pd;
var removeEmptyLines = require('gulp-remove-empty-lines');
/*************************************
* Functions
*************************************/
function copyArray(source) {
var copy = [];
for (elem of source) {
copy.push(elem);
}
return copy;
}
function getNodeIndexFromNodeList(nodeList, node) {
for (var i = 0, iLen = nodeList.length; i < iLen; i++) {
if (nodeList[i].attr("id") == node.attr("id")) {
return i;
}
}
return -1;
}
function removeNodeFromList(nodeList, node) {
return nodeList.splice(getNodeIndexFromNodeList(nodeList, node), 1);
}
/*************************************
* Constants
*************************************/
const PATH_I18N_LANGUAGES = "./src/resources/i18n";
const FILE_SOURCE_I18N = "./messages.xlf";
const XLF_MASK = "/*.xlf";
/*************************************
* Variables
*************************************/
var currentNodes = [];
/*************************************
* Tasks
*************************************/
/**
* Create translation files for languages passed as arguments from source translation file
* It must be run after extracting i18n labels from templates "node_modules/.bin/ng-xi18n"
*
* Sample: gulp i18n-init --languages "es, en, de, fr"
*/
gulp.task("i18n-init", function (done) {
console.log("Creating translation files...");
if (5 > process.argv.length) {
throw new Error('Not enough arguments. Sample: gulp i18n-init --languages "es, en, de, fr"');
}
var tasks = [];
var languages = process.argv[4].replace(/\s+/g, "").split(",");
for (var i = 0; i < languages.length; i++) {
tasks.push(function () {
var language = languages[i];
return function (callback) {
gulp.src(FILE_SOURCE_I18N)
.pipe(rename(function (path) {
path.basename = "messages." + language;
console.log("==>", PATH_I18N_LANGUAGES + "/" + path.basename + path.extname);
}))
.pipe(gulp.dest(PATH_I18N_LANGUAGES))
.on("end", callback)
}
}());
}
async.parallel(tasks, done);
});
/**
* Process source translation file to generate a list of its nodes
* It must be run before "i18n-update:merge" task
*/
gulp.task("i18n-update:init", function () {
console.log("Processing source file...");
return gulp.src(FILE_SOURCE_I18N)
.pipe(cheerio({
run: function ($, file) {
$("trans-unit").each(function () {
var node = $(this);
currentNodes.push(node);
});
console.log("==>", file.path);
}
,
parserOptions: {
xmlMode: true
}
}))
});
/**
* Merge source translation file with existing ones
* Remove unused nodes (not in source)
* Keep nodes intersection
* It must be run after "i18n-update:init" task
*/
gulp.task("i18n-update:merge", ["i18n-update:init"], function () {
console.log("Processing translation files...");
return gulp.src(PATH_I18N_LANGUAGES + XLF_MASK)
.pipe(cheerio({
run: function ($, file) {
var transNodes = copyArray(currentNodes);
$("trans-unit").each(function () {
var node = $(this);
if (-1 === getNodeIndexFromNodeList(transNodes, node)) {
node.remove();
} else {
removeNodeFromList(transNodes, node);
}
});
for (elem of transNodes) {
$("body").append(elem);
}
console.log("==>", file.path);
}
,
parserOptions: {
xmlMode: true
}
}))
.pipe(modifyFile(function (content) {
return pd.xml(content)
}))
.pipe(removeEmptyLines())
.pipe(gulp.dest(PATH_I18N_LANGUAGES));
});
/**
* Task pipe to init & merge i18n translation files
*/
gulp.task("i18n-update", ["i18n-update:merge", "i18n-update:init"]);
/**
* Transpile language translation XLIFF files to TS
*/
gulp.task("i18n-xlf2ts", function () {
console.log("Transpiling XLF translation files to TS...");
gulp.src(PATH_I18N_LANGUAGES + XLF_MASK)
.pipe(rename(function (path) {
path.extname = ".ts";
console.log("==>", PATH_I18N_LANGUAGES + "/" + path.basename + path.extname);
}))
.pipe(modifyFile(function (content, path) {
var language = path.split(".")[1].toUpperCase();
return "export const TRANSLATION_" + language + " = `" + content + "`;";
}))
.pipe(gulp.dest(PATH_I18N_LANGUAGES));
});