LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 const fs = require("fs"); | 22 const fs = require("fs"); |
23 const path = require("path"); | 23 const path = require("path"); |
24 const csv = require("csv"); | 24 const csv = require("csv"); |
25 const {promisify} = require("util"); | 25 const {promisify} = require("util"); |
26 const execFile = promisify(require("child_process").execFile); | 26 const execFile = promisify(require("child_process").execFile); |
27 const csvParser = promisify(csv.parse); | 27 const csvParser = promisify(csv.parse); |
28 const readFile = promisify(fs.readFile); | 28 const readFile = promisify(fs.readFile); |
29 const writeFile = promisify(fs.writeFile); | 29 const writeFile = promisify(fs.writeFile); |
30 const glob = promisify(require("glob").glob); | 30 const glob = promisify(require("glob").glob); |
31 const readJsonPromised = promisify(readJson); | |
32 | 31 |
33 const localesDir = "locale"; | 32 const localesDir = "locale"; |
34 const defaultLocale = "en_US"; | 33 const defaultLocale = "en_US"; |
35 | 34 |
36 const headers = ["Filename", "StringID", "Description", "Placeholders", | 35 const headers = ["Filename", "StringID", "Description", "Placeholders", |
37 defaultLocale]; | 36 defaultLocale]; |
38 let outputFileName = "translations.csv"; | 37 let outputFileName = "translations.csv"; |
39 | 38 |
40 /** | 39 /** |
41 * Export existing translation - files into CSV file | 40 * Export existing translation - files into CSV file |
42 */ | 41 */ |
43 function exportTranslations() | 42 function exportTranslations() |
44 { | 43 { |
45 glob(`${localesDir}/**/*.json`).then((filePaths) => | 44 glob(`${localesDir}/**/*.json`).then((filePaths) => |
46 { | 45 { |
47 // Reading all existing translations files | 46 // Reading all existing translations files |
48 return Promise.all(filePaths.map((filePath) => readJsonPromised(filePath))); | 47 return Promise.all(filePaths.map((filePath) => readJson(filePath))); |
49 }).then(csvFromJsonFileObjects); | 48 }).then(csvFromJsonFileObjects); |
50 } | 49 } |
51 | 50 |
52 /** | 51 /** |
53 * Creating Matrix which reflects output CSV file | 52 * Creating Matrix which reflects output CSV file |
54 * @param {Object[]} fileObjects - array of file objects created by readJson | 53 * @param {Object[]} fileObjects - array of file objects created by readJson |
55 */ | 54 */ |
56 function csvFromJsonFileObjects(fileObjects) | 55 function csvFromJsonFileObjects(fileObjects) |
57 { | 56 { |
58 const locales = []; | 57 const locales = []; |
59 const fileNames = []; | 58 const fileNames = []; |
60 // Create Object tree from the Objects array, for easier search | 59 // Create Object tree from the Objects array, for easier search |
61 // ex.: {dektop-options.json: {en_US: {...}, {de: {...}, {ru: {...}}} | 60 // ex.: {dektop-options.json: {en_US: {...}, {de: {...}, {ru: {...}}} |
62 const dataTreeObj = Object.create(null); | 61 const dataTreeObj = Object.create(null); |
63 for (const fileObject of fileObjects) | 62 for (const fileObject of fileObjects) |
64 { | 63 { |
65 const {fileName, locale, strings} = fileObject; | 64 const {fileName, locale, strings} = fileObject; |
66 | 65 |
67 if (locale != defaultLocale && !locales.includes(locale)) | 66 if (locale != defaultLocale && !locales.includes(locale)) |
68 locales.push(locale); | 67 locales.push(locale); |
69 | 68 |
70 if (!filesFilter.length || filesFilter.includes(fileName)) | 69 if ((!filesFilter.length || filesFilter.includes(fileName)) && |
| 70 !fileNames.includes(fileName)) |
71 fileNames.push(fileName); | 71 fileNames.push(fileName); |
72 | 72 |
73 if (!(fileName in dataTreeObj)) | 73 if (!(fileName in dataTreeObj)) |
74 dataTreeObj[fileName] = Object.create(null); | 74 dataTreeObj[fileName] = Object.create(null); |
75 | 75 |
76 dataTreeObj[fileName][locale] = strings; | 76 dataTreeObj[fileName][locale] = strings; |
77 } | 77 } |
78 // Create two dimensional strings array that reflects CSV structure | 78 // Create two dimensional strings array that reflects CSV structure |
79 const csvArray = [headers.concat(locales)]; | 79 const csvArray = [headers.concat(locales)]; |
80 for (const fileName of fileNames) | 80 for (const fileName of fileNames) |
81 { | 81 { |
82 const strings = dataTreeObj[fileName][defaultLocale]; | 82 const strings = dataTreeObj[fileName][defaultLocale]; |
83 for (const stringID of Object.keys(strings)) | 83 for (const stringID of Object.keys(strings)) |
84 { | 84 { |
85 const fileObj = dataTreeObj[fileName]; | 85 const fileObj = dataTreeObj[fileName]; |
86 const {description, message, placeholders} = strings[stringID]; | 86 const {description, message, placeholders} = strings[stringID]; |
87 const row = [fileName, stringID, description || "", | 87 const row = [fileName, stringID, description || "", |
88 JSON.stringify(placeholders), message]; | 88 JSON.stringify(placeholders), message]; |
89 | 89 |
90 for (const locale of locales) | 90 for (const locale of locales) |
91 { | 91 { |
92 const localeFileObj = fileObj[locale]; | 92 const localeFileObj = fileObj[locale]; |
93 const isTranslated = !!(localeFileObj && localeFileObj[stringID]); | 93 const isTranslated = !!(localeFileObj && localeFileObj[stringID]); |
94 row.push(isTranslated ? localeFileObj[stringID].message : ""); | 94 row.push(isTranslated ? localeFileObj[stringID].message : ""); |
95 } | 95 } |
96 csvArray.push(row); | 96 csvArray.push(row); |
97 } | 97 } |
98 } | 98 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 { | 168 { |
169 const filePath = path.join(localesDir, locale, fileName); | 169 const filePath = path.join(localesDir, locale, fileName); |
170 const sortedJson = sortJson(dataTreeObj[fileName][locale]); | 170 const sortedJson = sortJson(dataTreeObj[fileName][locale]); |
171 let fileString = JSON.stringify(sortedJson, null, 2); | 171 let fileString = JSON.stringify(sortedJson, null, 2); |
172 | 172 |
173 // Newline at end of file to match Coding Style | 173 // Newline at end of file to match Coding Style |
174 if (locale == defaultLocale) | 174 if (locale == defaultLocale) |
175 fileString += "\n"; | 175 fileString += "\n"; |
176 writeFile(filePath, fileString, "utf8").then(() => | 176 writeFile(filePath, fileString, "utf8").then(() => |
177 { | 177 { |
178 console.log(`Updated: ${filePath}`); | 178 console.log(`Updated: ${filePath}`); // eslint-disable-line no-console |
179 }).catch((err) => | 179 }).catch((err) => |
180 { | 180 { |
181 console.error(err); | 181 console.error(err); |
182 }); | 182 }); |
183 } | 183 } |
184 } | 184 } |
185 } | 185 } |
186 | 186 |
187 /** | 187 /** |
188 * This function currently relies on V8 to sort the object by keys | 188 * This function currently relies on V8 to sort the object by keys |
(...skipping 17 matching lines...) Expand all Loading... |
206 /** | 206 /** |
207 * Convert two dimensional array to the CSV file | 207 * Convert two dimensional array to the CSV file |
208 * @param {Object[]} csvArray - array to convert from | 208 * @param {Object[]} csvArray - array to convert from |
209 */ | 209 */ |
210 function arrayToCsv(csvArray) | 210 function arrayToCsv(csvArray) |
211 { | 211 { |
212 csv.stringify(csvArray, (err, output) => | 212 csv.stringify(csvArray, (err, output) => |
213 { | 213 { |
214 writeFile(outputFileName, output, "utf8").then(() => | 214 writeFile(outputFileName, output, "utf8").then(() => |
215 { | 215 { |
| 216 // eslint-disable-next-line no-console |
216 console.log(`${outputFileName} is created`); | 217 console.log(`${outputFileName} is created`); |
217 }).catch((error) => | 218 }).catch((error) => |
218 { | 219 { |
219 console.error(error); | 220 console.error(error); |
220 }); | 221 }); |
221 }); | 222 }); |
222 } | 223 } |
223 | 224 |
224 /** | 225 /** |
225 * Reads JSON file and assign filename and locale to it | 226 * Reads JSON file and assign filename and locale to it |
226 * @param {string} filePath - ex.: "locales/en_US/desktop-options.json" | 227 * @param {string} filePath - ex.: "locales/en_US/desktop-options.json" |
227 * @param {function} callback - fileName, locale and strings of locale file | 228 * @returns {Promise<Object>} resolves fileName, locale and strings of the |
228 * Parameters: | 229 * locale file |
229 * * Error message | 230 */ |
230 * * Object containing fileName, locale and strings | 231 function readJson(filePath) |
231 */ | 232 { |
232 function readJson(filePath, callback) | 233 return readFile(filePath, "utf8").then((data) => |
233 { | 234 { |
234 const {dir, base} = path.parse(filePath); | 235 const {dir, base} = path.parse(filePath); |
235 readFile(filePath, "utf8").then((data) => | |
236 { | |
237 const locale = dir.split(path.sep).pop(); | 236 const locale = dir.split(path.sep).pop(); |
238 const strings = JSON.parse(data); | 237 const strings = JSON.parse(data); |
239 callback(null, {fileName: base, locale, strings}); | 238 return {fileName: base, locale, strings}; |
240 }).catch((err) => | |
241 { | |
242 callback(err); | |
243 }); | 239 }); |
244 } | 240 } |
245 | 241 |
246 /** | 242 /** |
247 * Exit process and log error message | 243 * Exit process and log error message |
248 * @param {String} error error message | 244 * @param {String} error error message |
249 */ | 245 */ |
250 function exitProcess(error) | 246 function exitProcess(error) |
251 { | 247 { |
252 console.error(error); | 248 console.error(error); |
253 process.exit(1); | 249 process.exit(1); |
254 } | 250 } |
255 | 251 |
256 // CLI | 252 // CLI |
257 const helpText = ` | 253 const helpText = ` |
258 About: Converts locale files between CSV and JSON formats | 254 About: Converts locale files between CSV and JSON formats |
259 Usage: csv-export.js [option] [argument] | 255 Usage: csv-export.js [option] [argument] |
260 Options: | 256 Options: |
261 -f [FILENAME] Name of the files to be exported ex.: -f firstRun.json | 257 -f [FILENAME] Name of the files to be exported ex.: -f firstRun.json |
262 option can be used multiple times. | 258 option can be used multiple times. |
263 If omitted all files are being exported | 259 If omitted all files are being exported |
264 | 260 |
265 -o [FILENAME] Output filename ex.: | 261 -o [FILENAME] Output filename ex.: |
266 -f firstRun.json -o {hash}-firstRun.csv | 262 -f firstRun.json -o firstRun.csv |
267 Placeholders: | |
268 {hash} - Mercurial current revision hash | |
269 If omitted the output fileName is set to | 263 If omitted the output fileName is set to |
270 translations-{repo}-{hash}.csv | 264 translations.csv |
271 | 265 |
272 -i [FILENAME] Import file path ex: -i issue-reporter.csv | 266 -i [FILENAME] Import file path ex: -i issue-reporter.csv |
273 `; | 267 `; |
274 | 268 |
275 const argv = process.argv.slice(2); | 269 const argv = process.argv.slice(2); |
276 let stopExportScript = false; | 270 let stopExportScript = false; |
277 // Filter to be used export to the fileNames inside | 271 // Filter to be used export to the fileNames inside |
278 const filesFilter = []; | 272 const filesFilter = []; |
279 | 273 |
280 for (let i = 0; i < argv.length; i++) | 274 for (let i = 0; i < argv.length; i++) |
281 { | 275 { |
282 switch (argv[i]) | 276 switch (argv[i]) |
283 { | 277 { |
284 case "-h": | 278 case "-h": |
285 console.log(helpText); | 279 console.log(helpText); // eslint-disable-line no-console |
286 stopExportScript = true; | 280 stopExportScript = true; |
287 break; | 281 break; |
288 case "-f": | 282 case "-f": |
289 if (!argv[i + 1]) | 283 if (!argv[i + 1]) |
290 { | 284 { |
291 exitProcess("Please specify the input filename"); | 285 exitProcess("Please specify the input filename"); |
292 } | 286 } |
293 filesFilter.push(argv[i + 1]); | 287 filesFilter.push(argv[i + 1]); |
294 break; | 288 break; |
295 case "-o": | 289 case "-o": |
(...skipping 10 matching lines...) Expand all Loading... |
306 } | 300 } |
307 const importFile = argv[i + 1]; | 301 const importFile = argv[i + 1]; |
308 importTranslations(importFile); | 302 importTranslations(importFile); |
309 stopExportScript = true; | 303 stopExportScript = true; |
310 break; | 304 break; |
311 } | 305 } |
312 } | 306 } |
313 | 307 |
314 if (!stopExportScript) | 308 if (!stopExportScript) |
315 exportTranslations(filesFilter); | 309 exportTranslations(filesFilter); |
LEFT | RIGHT |