-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathng-translation-gen.js
322 lines (294 loc) · 10.5 KB
/
ng-translation-gen.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
'use strict';
/* jshint -W083 */
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const Handlebars = require('handlebars');
const crypto = require('crypto');
// Used to find the get translations arguments.
// E.g: {0}
const ARGS_REG_EXP = /\{\w+\}/g;
// Both regexp are used to get the method name from the translation key.
// E.g: error.Invalid -> errorInvalid
const UPPER_CASE_REG_EXP = /[A-Z]+/g;
const METHOD_REG_EXP = /[\. | \_ | \-]\w{1}/g;
// Used get the generated TS file name from the TS class name
// E.g: AccessMesagges -> access-messages.ts
const FILE_REG_EXP = /[A-Z]{1}/g;
const RESERVED = [
'abstract', 'arguments', 'await', 'boolean',
'break', 'byte', 'case', 'catch',
'char', 'class', 'const', 'continue',
'debugger', 'default', 'delete', 'do',
'double', 'else', 'enum', 'eval',
'export', 'extends', 'false', 'final',
'finally', 'float', 'for', 'function',
'goto', 'if', 'implements', 'import',
'in', 'instanceof', 'int', 'interface',
'let', 'long', 'native', 'new',
'null', 'package', 'private', 'protected',
'public', 'return', 'short', 'static',
'super', 'switch', 'synchronized', 'this',
'throw', 'throws', 'transient', 'true',
'try', 'typeof', 'var', 'void',
'volatile', 'while', 'with', 'yield'
]
/**
* Main generation function
*/
function ngTranslationGen(options) {
const keys = Object.keys(options.mapping || {});
if (keys.length === 0) {
console.warn("No mapping specified");
return;
}
// Read each template
const templates = {};
const dir = path.join(__dirname, "templates");
for (const file of fs.readdirSync(dir)) {
if (file.endsWith('.handlebars')) {
const basename = file.substring(0, file.length - '.handlebars'.length);
const fullname = path.join(dir, file);
const content = String(fs.readFileSync(fullname));
const template = Handlebars.compile(content)
templates[basename] = template;
Handlebars.registerPartial(basename, template);
}
}
// empty or create the output dir if not exists
fse.emptyDirSync(path.normalize(options.output));
const defaultLocale = options.defaultLocale || 'en';
const locales = (options.locales || []).length === 0 ? ['en'] : options.locales;
const localesModel = getLocalesModel(locales);
const additionalLocalesModel = localesModel.filter(l => l.locale !== defaultLocale);
const argumentType = options.argumentType || 'string';
for (const key of keys) {
let baseName = key;
if (baseName.toLowerCase().endsWith('.json')) {
baseName = baseName.substring(0, baseName.length - 5);
}
const filename = baseName + '.json';
try {
// Read the file content
const fullFilename = path.join(options.input, filename);
const content = fs.readFileSync(fullFilename);
let translations = JSON.parse(content, "utf-8");
// Create the model
let className = options.mapping[key];
const model = getTemplateModel(translations, className, null, baseName, argumentType);
// Set additional properties for the root model
model.defaultLocale = defaultLocale;
model.defaultFilename = filename;
model.defaultHash = contentHash(fullFilename);
model.locales = locales;
// Make a copy of each additional locale model
model.additionalLocales = additionalLocalesModel.map(l => ({ ...l }));
// Add the hashes for each additional locale
model.additionalLocales.forEach(l => {
const localeFilename = baseName + options.separator + l.locale + '.json';
const localeFile = path.join(options.input, localeFilename);
l.filename = localeFilename;
l.hash = contentHash(localeFile);
});
model.separator = options.separator;
model.metaClassName = className + 'Meta';
model.filename = getTSFilename(className);
model.metaFilename = getTSFilename(model.metaClassName);
// Write the interface
let outFile = path.join(options.output, model.filename + ".ts");
fs.writeFileSync(outFile, templates.messages(model), "utf-8");
console.info('Wrote ' + outFile);
// Write the metadata
outFile = path.join(options.output, model.metaFilename + ".ts");
fs.writeFileSync(outFile, templates.meta(model), "utf-8");
console.info('Wrote ' + outFile);
} catch (error) {
const e1 = new Error(`Generation aborted, error processing file: ${filename} (${error}).`);
e1.stack = error.stack;
throw e1;
}
}
// finally, copy all artifacts required by the generated code to the output folder
fs.copyFileSync(path.join(__dirname, 'static', "translations.ts"), path.join(options.output, "translations.ts"));
}
function contentHash(filename) {
let content;
try {
content = fs.readFileSync(filename, {
encoding: 'utf-8'
});
} catch (e) {
return '';
}
return crypto.createHash('sha1').update(content, 'utf-8').digest('hex');
}
/**
* Return the model used to render the template.
*/
function getTemplateModel(translations, className, path, baseName, argumentType) {
let model = {
className: className,
baseName: baseName,
simple: [],
named: [],
positional: [],
nested: [],
hasNested: false,
path: path,
root: path == null || path === ''
};
const allKeys = Object.keys(translations);
// First process all keys that have a translation directly
const directKeys = allKeys.filter(k => typeof translations[k] === 'string');
if (directKeys.length > 0) {
for (const key of directKeys) {
let positionalKeys = [];
let namedKeys = [];
let positionalArgs = {};
let namedArgs = {};
let mapping = undefined;
const value = translations[key];
// each match is of the form {argName} or {a_number}
value.replace(ARGS_REG_EXP, (match) => {
// remove leading '{' and trailing '}' chars
let paramName = match.substring(1, match.length - 1);
// if it is of the form {a_number} then the resulting param name will be
// prefixed with 'arg'
let positional = match.match(/\{\d+\}/);
const paramKey = paramName;
if (positional) {
paramName = "$" + paramName;
} else {
paramName = getValidIdentifier(paramName);
}
let param = {
positional,
key: paramKey,
name: paramName,
type: argumentType
};
if (positional) {
if (!positionalKeys.includes(paramName)) {
positionalKeys.push(paramName)
positionalArgs[paramName] = param;
}
} else {
if (!namedKeys.includes(paramName)) {
namedKeys.push(paramName);
namedArgs[paramName] = param;
}
}
});
// If there's a single named arg, we handle it as positional
const namedAsPositional = namedKeys.length === 1 && positionalKeys.length === 0;
if (namedAsPositional) {
let paramKey = namedKeys[0];
let arg = namedArgs[paramKey];
arg.positional = true;
positionalKeys.push(paramKey);
positionalArgs[paramKey] = arg;
namedKeys = [];
namedArgs = {};
}
// If there's a mix of positional and named keys, handle all positional arguments as named
if (namedKeys.length > 0 && positionalArgs.length > 0) {
positionalArgs.forEach(arg => {
const paramKey = `arg${arg}`;
namedKeys.push(paramKey);
namedArgs[paramKey] = arg;
arg.positional = false;
})
positionalKeys = [];
positionalArgs = {};
}
// Compute each argument
let args = [];
if (namedKeys.length > 0) {
namedKeys.forEach(k => {
const mappedTo = namedArgs[k].key;
if (k !== mappedTo) {
if (!mapping) {
mapping = {};
}
mapping[k] = mappedTo;
};
});
args.push({
name: '$',
type: `{${namedKeys.map(n => n + ': ' + argumentType).join(', ')}}`
});
} else if (positionalKeys.length > 0) {
if (namedAsPositional) {
const first = positionalKeys[0];
mapping = positionalArgs[first].key;
}
positionalKeys.forEach(k => {
const arg = positionalArgs[k];
args.push({
name: arg.name,
type: argumentType
});
});
}
// Add the direct property model
const arr = args.length === 0 ? model.simple : positionalKeys.length > 0 ? model.positional : model.named;
arr.push({
name: getValidIdentifier(key, false),
key,
defaultValue: value.replace(/\n/g, '\\n').replace(/\*\//g, "* /"),
args: args,
mapping: mapping ? JSON.stringify(mapping) : undefined
});
}
}
// Then process all nested models
const nestedKeys = allKeys.filter(k => typeof translations[k] === 'object');
if (nestedKeys.length > 0) {
model.hasNested = true;
for (const key of nestedKeys) {
const value = translations[key];
const nestedPath = (path ? path + '.' + key : key);
let nestedClass = getValidIdentifier(key, false);
nestedClass = nestedClass.charAt(0).toUpperCase() + nestedClass.substring(1);
const nested = getTemplateModel(value, className + '$' + nestedClass, nestedPath, null, argumentType);
nested.property = key;
nested.path = '\'' + nestedPath + '\'';
model.nested.push(nested);
}
}
model.withArgs = [...(model.named || []), ...(model.positional || [])];
return model;
}
/**
* Returns the models for each locale
*/
function getLocalesModel(locales) {
return locales.map(l => ({ locale: l }));
}
/**
* Return the dashed-case version for the given camel-case class name plus the
* '.ts' suffix
*/
function getTSFilename(className) {
let result = className.replace(FILE_REG_EXP, (match) => "-" + match.toLowerCase());
if (result.startsWith("-")) {
result = result.substring(1);
}
return result;
}
/**
* Return a valid TS identifier from a given name.
* @param name E.g. a trasnslation key of the form word1.word2.
* @returns a camel-cased name of the form word1Word2.
*/
function getValidIdentifier(name, checkReserved = true) {
// more than one char in upper case is lower cased
name = name.replace(UPPER_CASE_REG_EXP, (match) => match.length == 1 ? match : match.toLowerCase());
// each char after a '.', '_' or '-' is upper cased
name = name.replace(METHOD_REG_EXP, (match) => match.substring(1).toUpperCase());
if (checkReserved && RESERVED.includes(name)) {
name = `'${name}'`;
}
return name;
}
module.exports = ngTranslationGen;