-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
128 lines (96 loc) · 3.54 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
'use strict';
var through = require('through2');
var path = require('path');
var fs = require('fs');
var PluginError = require("plugin-error");
var File = require("vinyl");
module.exports = function(options) {
if(!options) {
options = {};
}
let outputFileName = options.fileName || "translation.json";
let defaultLanguageName = options.defaultLanguage || "en";
let defaultLanguage = {};
let languages = {};
let cwd = null;
let fileName = null;
let anyFileProcessed = false;
function AddTranslation(languageContent, key, value, context) {
if(!value) value = " ";
var content = languageContent;
let objectPath = key.split('.');
let finalPathPart = objectPath[objectPath.length-1];
for(let i = 0; i<objectPath.length-1; i++) {
let pathPart = objectPath[i];
if(!content[pathPart]) content[pathPart] = {};
content = content[pathPart];
}
if(content[finalPathPart]) {
context.emit('warning', new PluginError('gulp-i18n-compile2', "'Duplicate key '" + key + "' found. Content has been overwritten."));
}
content[finalPathPart] = value;
}
function bufferContents(file, enc, cb) {
if (file.isNull()) {
cb();
return;
}
if (file.isStream()) {
this.emit('error', new PluginError('gulp-i18n-compile2', 'Streaming not supported'));
cb();
return;
}
let translations = JSON.parse(file.contents);
if(!translations) {
this.emit('error', new PluginError('gulp-i18n-compile2', 'Cannot read file ' + path.basename(file.path)));
cb();
return;
}
if(!anyFileProcessed) { // only on first execution
anyFileProcessed = true;
cwd = file.cwd;
}
for(let sourceFile in translations) {
var source = translations[sourceFile];
for(let i18nKey in source.content) {
let extract = source.content[i18nKey];
AddTranslation(defaultLanguage, i18nKey,extract.content, this);
for(let language in extract.translations) {
let translation = extract.translations[language];
let languageContent = languages[language];
if(!languageContent) {
languageContent = {};
languages[language] = languageContent;
}
AddTranslation(languageContent, i18nKey,translation.content, this);
}
}
}
cb();
}
function createFile(language, content) {
return new File(
{
base: cwd,
path: path.resolve(cwd, "./" + language + "/" + outputFileName),
cwd: cwd,
contents: new Buffer(JSON.stringify(content, null, '\t'))
});
}
function endStream(cb) {
if(!anyFileProcessed) { // only on first execution
this.emit('error', new PluginError('gulp-i18n-compile2', 'Pipeline is empty. No output has been created.'));
cb();
return;
}
this.push(createFile(defaultLanguageName,defaultLanguage));
for(let languageName in languages) {
let content = languages[languageName];
// Add default language for missing translations
content = Object.assign(defaultLanguage, content);
this.push(createFile(languageName,content));
}
cb();
}
return through.obj(bufferContents, endStream);
}