-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
executable file
·161 lines (131 loc) · 5.13 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
#!/usr/bin/env node
const process = require('process');
const fs = require('fs');
const path = require('path');
const program = require('commander');
const shell = require('shelljs');
const logger = require('./logger.js');
const xlfHandler = require('./xlf-handler');
const jsonHandler = require('./json-handler');
const arbHandler = require('./arb-handler');
function resolveHandler(fileName) {
switch (path.extname(fileName.toLowerCase())) {
case '.json':
return jsonHandler;
case '.arb':
return arbHandler;
default:
return xlfHandler;
}
}
function parseFileContent(fileName, allItems, allowConflicts) {
const handler = resolveHandler(fileName);
const fileContent = fs.readFileSync(fileName).toString();
const parser = handler.createParser(fileContent);
let count = 0;
for (const item of parser.parse(fileContent)) {
const duplicate = allItems.find(t => t.id === item.id);
if (duplicate) {
if (duplicate.text === item.text) {
logger.warn(`Duplicate ${item.id} found in files ${fileName} and ${duplicate.fileName}`);
continue;
} else {
const errMsg = `Item with ID ${item.id} found in files ${fileName} and ${duplicate.fileName}. Both instances contain different text.`;
if (allowConflicts) {
logger.warn(errMsg);
} else {
throw new Error(errMsg);
}
}
}
allItems.push({ ...item, fileName });
count++;
}
if (count) {
logger.info(`Parsed ${count} translations in file ${fileName}`);
} else {
logger.warn(`Parsed file ${fileName}. No translations found.`);
}
return parser.getLocale();
}
function readInputs(inputPaths, operation) {
for (const fileName of shell.ls(inputPaths)) {
try {
operation(fileName);
} catch (err) {
logger.error('Couldn\'t parse file ' + fileName);
throw err;
}
}
}
function convertTranslationFiles(inputPaths, outputFormat, allowConflicts) {
let counter = 0;
readInputs(
inputPaths,
fileName => {
const transItems = [];
const locale = parseFileContent(fileName, transItems, allowConflicts);
if (transItems.length) {
const baseName = path.basename(fileName, path.extname(fileName));
const newExt = '.' + outputFormat;
const outputPath = path.join(path.dirname(fileName), baseName + newExt);
const convertedContent = resolveHandler(outputPath).save(transItems, locale);
fs.writeFileSync(outputPath, convertedContent);
logger.info(`Converted file ${fileName} into ${outputPath}`);
}
}
);
logger.success(`xlf-merge converted all ${counter} input files`);
}
function mergeTranslationFiles(inputPaths, outputPath, allowConflicts) {
const transItems = [];
let locale = null;
readInputs(inputPaths, fileName => {
let fileLocale = parseFileContent(fileName, transItems, allowConflicts);
if (locale && fileLocale && locale !== fileLocale) {
throw new Error(`Locales among input file don't match. File ${fileName} has locale ${fileLocale}, expected ${locale}.`);
}
locale = fileLocale;
});
logger.info(`All input files parsed. Found ${transItems.length} translated texts.`);
if (transItems.length) {
const handler = resolveHandler(outputPath);
const output = handler.save(transItems, locale);
fs.writeFileSync(outputPath, output);
logger.success('xlf-merge generated output file ' + outputPath);
}
}
program
.name('xlf-merge')
.version('2.0.1')
.addHelpText(
'before',
'Xlf-merge 2.0.1\n' +
'Merges and/or converts translation dictionary files. Supports XLF 1.2, JSON, and ARB.\n' +
'Generates single translation dictionary required by Angular compiler from multiple input files.\n'
)
.usage('[options] <input files or pattern such as *.xlf, *.json ...>')
.option('-o --output <output>', 'Output file name')
.addOption(new program.Option('-c --convert <format>', 'Converts all input files in place').choices(['xlf', 'json', 'arb']))
.option('-q --quiet', 'Quiet mode. Doesn\'t show warnings and info messages.')
.option('-ac --allow-conflicts', 'Does not fail if the same key is present multiple times with different content')
.addHelpText('after', '\nEither --output or --convert option is required')
.parse(process.argv);
const options = program.opts();
if (program.args === 0 || (!options.output && !options.convert)) {
program.help();
}
if (options.quiet) {
logger.quietMode = true;
}
try {
if (options.convert) {
convertTranslationFiles(program.args, options.convert, options.allowConflicts);
}
if (options.output) {
mergeTranslationFiles(program.args, options.output, options.allowConflicts);
}
} catch (err) {
logger.error('xlf-merge failed\n' + err.toString());
process.exitCode = 1;
}