forked from google/santa-tracker-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
149 lines (131 loc) · 4.43 KB
/
Copy pathindex.js
File metadata and controls
149 lines (131 loc) · 4.43 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
/*
* Copyright 2015 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
/* jshint node: true */
var dir = require('node-dir');
var through = require('through2');
var path = require('path');
var gutil = require('gulp-util');
var format = require('sprintf-js').sprintf;
var REGEX = /<i18n-msg msgid="([^"]*)">([^<]*)<\/i18n-msg>/gm;
module.exports = function replaceMessages(opts) {
var warn = warnFunc(opts.strict);
var msgPromise = getMsgs(opts.path);
var stream = through.obj(function(file, enc, cb) {
// TODO(cbro): only read HTML files.
if (file.isNull()) return stream.push(file);
if (file.isStream()) error('No support for streams');
msgPromise.then(function(messagesByLang) {
var src = file.contents.toString();
var langs = Object.keys(messagesByLang);
// Force en to be last. gulp halts execution because we push a file to the
// stream with the same path.
langs = langs.filter(function(l) { return l != 'en' });
langs.push('en');
for (var i = 0; i < langs.length; i++) {
var lang = langs[i];
var msgs = messagesByLang[lang];
if (!msgs) {
error('No messages for lang: %s', lang);
}
var ext = '_' + lang + '.html';
var replaced = src
.replace(/lang="en"/, 'lang="' + lang + '"')
.replace(/_en\.html/mg, ext)
.replace(REGEX, function replacer(match, msgid, tagBody) {
var msg = msgs[msgid];
if (!msg) {
warn('[%s %6s] Could not find message %s.', file.relative, lang, msgid);
var fallback = messagesByLang.en[msgid];
if (lang == 'fr-CA') {
fallback = messagesByLang.fr[msgid] || fallback
}
return fallback ? fallback.message: 'MESSAGE_NOT_FOUND';
}
if (lang == 'en' && 'PLACEHOLDER_i18n' != tagBody) {
error('i18n-msg body must be "PLACEHOLDER_i18n" for %s in %s', msgid, file.relative);
}
return msg.message
});
if (replaced == src) {
// Don't create a new file if the source didn't change.
stream.push(file);
break;
}
if (!file.path.match(/(index|error|upgrade|_en)\.html$/)) {
error('[%s] Files with i18n-msg should end in _en.html', file.relative);
}
var dir = '/';
// Only root pages should go in /intl/ directories.
if (lang != 'en' && !file.path.match(/_en\.html$/)) {
dir = '/intl/' + lang + '_ALL/';
}
var i18nfile = file.clone();
i18nfile.path = path.dirname(file.path) + dir +
path.basename(file.path).replace(/_en.html$/, ext);
i18nfile.contents = new Buffer(replaced);
stream.push(i18nfile);
}
cb();
});
});
return stream;
};
/**
* Read messages from _messages/*.json into a map.
* Returns a promise-like object.
*/
function getMsgs(msgDir) {
// map: locale -> message ID -> message object (description/message)
var msgs = {};
var done = false;
var callbacks = [];
dir.readFiles(msgDir, function(err, content, filename, next) {
if (err) throw err;
var lang = path.basename(filename, '.json');
msgs[lang] = JSON.parse(content);
next();
},
function(err) {
if (err) throw err;
done = true;
while (callbacks.length) {
callbacks.pop()(msgs);
}
});
return {
then: function(callback) {
if (!done) {
callbacks.push(callback);
} else {
callback(msgs);
}
}
};
}
function warnFunc(strict) {
return function(var_args) {
var message = format.apply(this, arguments);
if (strict) {
throw new gutil.PluginError('i18n_replace', message);
} else {
gutil.log('WARNING[i18n_replace]:', message);
}
}
}
function error(var_args) {
var message = format.apply(this, arguments);
throw new gutil.PluginError('i18n_replace', message);
}