This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
200 lines (163 loc) · 5.37 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'use strict';
var through = require('through2');
var path = require('path');
var fs = require('fs');
var moment = require('moment');
var htmlPlugIn = require('./plugins/html');
var jsonPlugIn = require('./plugins/json');
var stringify = require('json-stable-stringify');
var PluginError = require("plugin-error");
var File = require("vinyl");
var log = require("fancy-log");
var colors = require("ansi-colors");
const obsoleteTranslationsProperty = "obsoleteTranslations";
exports.html = htmlPlugIn;
exports.json = jsonPlugIn;
exports.extract = function(outFile, options) {
outFile = outFile || 'i18nextract.json';
options = options || {};
if(!options.plugIns || options.plugIns.lenght == 0) {
options.plugIns = [ new htmlPlugIn() ];
}
var warnOnDuplicates = (options.warnOnDuplicates !== undefined ? options.warnOnDuplicates : true);
var markUpdates = (options.markUpdates !== undefined ? options.markUpdates : true);
var defaultLanguages = options.defaultLanguages || ["de"];
var keepObsoleteTranslations = (options.keepObsoleteTranslations !== undefined ? options.keepObsoleteTranslations : false);
var fileName;
var cwd;
if (typeof outFile === 'string') {
fileName = outFile;
} else if (typeof outFile.path === 'string') {
fileName = path.basename(outFile.path);
} else {
throw new PluginError('gulp-i18n-extract', 'Missing outFile for gulp-i18n-extract');
}
var existingExtracts = null;
var extract = null;
var obsoleteTranslations = [];
function bufferContents(file, enc, cb) {
if (file.isNull()) {
cb();
return;
}
if (file.isStream()) {
this.emit('error', new PluginError('gulp-i18n-extract', 'Streaming not supported'));
cb();
return;
}
if(!extract) { // only on first execution
cwd = file.cwd;
fileName = path.resolve(cwd, fileName);
if(fs.existsSync(fileName)) {
var previousContent = fs.readFileSync(fileName);
existingExtracts = JSON.parse(previousContent) || {};
}
else {
existingExtracts = {};
}
extract = {};
}
var ext = path.extname(file.path);
var name = path.basename(file.path, ext)
var src = path.relative(cwd, file.path);
var modifiedDate = moment();
obsoleteTranslations = existingExtracts[obsoleteTranslationsProperty] || [];
options.plugIns.filter((plugIn) => plugIn.canHandle(file.path))
.forEach((plugIn) => {
var fileContent = existingExtracts[name] || extract[name];
if(!fileContent) {
fileContent = { "src":src, "content": {} };
}
var newContent = {};
var hasContent = false;
var extractedKeys = {};
try {
//log("Processing",src);
plugIn.parse(file, (key, value) => {
if(warnOnDuplicates && extractedKeys[key]) {
log("Duplicate key",colors.yellow(key), "in", colors.yellow(src), "found.");
return;
}
var extractedKey = fileContent.content[key];
if(extractedKey) {
delete fileContent.content[key];
}
else {
extractedKey = newContent[key];
}
if(!extractedKey) {
extractedKey = { "content" : value, "lastModified": modifiedDate,"translations":{} };
if(markUpdates) extractedKey["needsUpdate"] = true;
}
else {
if(extractedKey.content != value) {
extractedKey.content = value;
extractedKey.lastModified = modifiedDate
if(markUpdates) extractedKey["needsUpdate"] = true;
}
}
defaultLanguages.forEach(lang => {
if(!extractedKey.translations[lang]) {
extractedKey.translations[lang] = { "content" : "", "lastModified": "" }
}
}, this);
extractedKeys[key] = key;
newContent[key] = extractedKey;
hasContent = true;
});
}
catch (e) {
this.emit('error', new PluginError('gulp-i18n-extract', e, {showStack: true}));
}
if(hasContent)
{
if(keepObsoleteTranslations) {
// Foreach remaining key in file
for(var key in fileContent.content) {
// Get obsolete key
var obsolete = fileContent.content[key];
// Get or create obsolete translation object
var obsoleteTranslation = obsoleteTranslations.find(x => x.key === obsolete.content);
if(!obsoleteTranslation) {
obsoleteTranslation = {"key":obsolete.content, "translations":{}};
obsoleteTranslations.push(obsoleteTranslation);
}
// foreach translated language
for(var language in obsolete.translations) {
// Get translated text
var translatedText = obsolete.translations[language].content;
// Get or create language list
var translations = obsoleteTranslation.translations[language];
if(!translations) {
translations = [];
obsoleteTranslation.translations[language] = translations;
}
// Add if not in the list
if(!translations.find( x => x === translatedText)) {
translations.push(translatedText);
}
}
}
}
fileContent.content = newContent;
extract[name] = fileContent;
}
}, this);
cb();
}
function endStream(cb) {
if(keepObsoleteTranslations) {
extract[obsoleteTranslationsProperty] = obsoleteTranslations;
}
var extractFile = new File(
{
base: cwd,
path: fileName,
cwd: cwd,
contents: new Buffer( stringify(extract, { space: '\t' }))
});
this.push(extractFile);
cb();
}
return through.obj(bufferContents, endStream);
};