Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: build/csv-export.js

Issue 29636585: Issue 6171 - create CSV exporter and importer for translations (Closed)
Patch Set: Do not allow repetitive filenames and Don't use promisify for our functions Created June 5, 2018, 3 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « README.md ('k') | package.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /* globals process */
19
20 "use strict";
21
22 const fs = require("fs");
23 const path = require("path");
24 const csv = require("csv");
25 const {promisify} = require("util");
26 const execFile = promisify(require("child_process").execFile);
27 const csvParser = promisify(csv.parse);
28 const readFile = promisify(fs.readFile);
29 const writeFile = promisify(fs.writeFile);
30 const glob = promisify(require("glob").glob);
31
32 const localesDir = "locale";
33 const defaultLocale = "en_US";
34
35 const headers = ["Filename", "StringID", "Description", "Placeholders",
36 defaultLocale];
37 let outputFileName = "translations.csv";
38
39 /**
40 * Export existing translation - files into CSV file
41 */
42 function exportTranslations()
43 {
44 glob(`${localesDir}/**/*.json`).then((filePaths) =>
45 {
46 // Reading all existing translations files
47 return Promise.all(filePaths.map((filePath) => readJson(filePath)));
48 }).then(csvFromJsonFileObjects);
49 }
50
51 /**
52 * Creating Matrix which reflects output CSV file
53 * @param {Object[]} fileObjects - array of file objects created by readJson
54 */
55 function csvFromJsonFileObjects(fileObjects)
56 {
57 const locales = [];
58 const fileNames = [];
59 // Create Object tree from the Objects array, for easier search
60 // ex.: {dektop-options.json: {en_US: {...}, {de: {...}, {ru: {...}}}
61 const dataTreeObj = Object.create(null);
62 for (const fileObject of fileObjects)
63 {
64 const {fileName, locale, strings} = fileObject;
65
66 if (locale != defaultLocale && !locales.includes(locale))
67 locales.push(locale);
68
69 if ((!filesFilter.length || filesFilter.includes(fileName)) &&
70 !fileNames.includes(fileName))
71 fileNames.push(fileName);
72
73 if (!(fileName in dataTreeObj))
74 dataTreeObj[fileName] = Object.create(null);
75
76 dataTreeObj[fileName][locale] = strings;
77 }
78 // Create two dimensional strings array that reflects CSV structure
79 const csvArray = [headers.concat(locales)];
80 for (const fileName of fileNames)
81 {
82 const strings = dataTreeObj[fileName][defaultLocale];
83 for (const stringID of Object.keys(strings))
84 {
85 const fileObj = dataTreeObj[fileName];
86 const {description, message, placeholders} = strings[stringID];
87 const row = [fileName, stringID, description || "",
88 JSON.stringify(placeholders), message];
89
90 for (const locale of locales)
91 {
92 const localeFileObj = fileObj[locale];
93 const isTranslated = !!(localeFileObj && localeFileObj[stringID]);
94 row.push(isTranslated ? localeFileObj[stringID].message : "");
95 }
96 csvArray.push(row);
97 }
98 }
99 arrayToCsv(csvArray);
100 }
101
102 /**
103 * Import strings from the CSV file
104 * @param {string} filePath - CSV file path to import from
105 */
106 function importTranslations(filePath)
107 {
108 readFile(filePath, "utf8").then((fileObjects) =>
109 {
110 return csvParser(fileObjects);
111 }).then((dataMatrix) =>
112 {
113 const headLocales = dataMatrix.shift().slice(4);
114 const dataTreeObj = {};
115 for (const rowId in dataMatrix)
116 {
117 const row = dataMatrix[rowId];
118 let [currentFilename, stringId, description, placeholder, ...messages] =
119 row;
120 if (!stringId)
121 continue;
122
123 stringId = stringId.trim();
124 // Check if it's the filename row
125 if (!dataTreeObj[currentFilename])
126 dataTreeObj[currentFilename] = {};
127
128 description = description.trim();
129 for (let i = 0; i < headLocales.length; i++)
130 {
131 const locale = headLocales[i].trim();
132 const message = messages[i].trim();
133 if (!message)
134 continue;
135
136 // Create Object tree from the Objects array, for easier search
137 // ex.: {dektop-options.json: {en_US: {...}, {de: {...}, {ru: {...}}}
138 if (!dataTreeObj[currentFilename][locale])
139 dataTreeObj[currentFilename][locale] = {};
140
141 const localeObj = dataTreeObj[currentFilename][locale];
142 localeObj[stringId] = {};
143 const stringObj = localeObj[stringId];
144
145 // We keep string descriptions only in default locale files
146 if (locale == defaultLocale && description)
147 stringObj.description = description;
148
149 stringObj.message = message;
150 if (placeholder)
151 stringObj.placeholders = JSON.parse(placeholder);
152 }
153 }
154 writeJson(dataTreeObj);
155 });
156 }
157
158 /**
159 * Write locale files according to dataTreeObj
160 * @param {Object} dataTreeObj - ex.:
161 * {dektop-options.json: {en_US: {...}, {de: {...}, {ru: {...}}}
162 */
163 function writeJson(dataTreeObj)
164 {
165 for (const fileName in dataTreeObj)
166 {
167 for (const locale in dataTreeObj[fileName])
168 {
169 const filePath = path.join(localesDir, locale, fileName);
170 const sortedJson = sortJson(dataTreeObj[fileName][locale]);
171 let fileString = JSON.stringify(sortedJson, null, 2);
172
173 // Newline at end of file to match Coding Style
174 if (locale == defaultLocale)
175 fileString += "\n";
176 writeFile(filePath, fileString, "utf8").then(() =>
177 {
178 console.log(`Updated: ${filePath}`); // eslint-disable-line no-console
179 }).catch((err) =>
180 {
181 console.error(err);
182 });
183 }
184 }
185 }
186
187 /**
188 * This function currently relies on V8 to sort the object by keys
189 * @param {Object} unordered - json object
190 * @returns {Object}
191 */
192 function sortJson(unordered)
193 {
194 const ordered = {};
195 for (const key of Object.keys(unordered).sort())
196 {
197 ordered[key] = unordered[key];
198 if (unordered[key].placeholders)
199 ordered[key].placeholders = sortJson(unordered[key].placeholders);
200
201 ordered[key] = unordered[key];
202 }
203 return ordered;
204 }
205
206 /**
207 * Convert two dimensional array to the CSV file
208 * @param {Object[]} csvArray - array to convert from
209 */
210 function arrayToCsv(csvArray)
211 {
212 csv.stringify(csvArray, (err, output) =>
213 {
214 writeFile(outputFileName, output, "utf8").then(() =>
215 {
216 // eslint-disable-next-line no-console
217 console.log(`${outputFileName} is created`);
218 }).catch((error) =>
219 {
220 console.error(error);
221 });
222 });
223 }
224
225 /**
226 * Reads JSON file and assign filename and locale to it
227 * @param {string} filePath - ex.: "locales/en_US/desktop-options.json"
228 * @param {function} callback - fileName, locale and strings of locale file
Thomas Greiner 2018/06/12 14:50:42 Detail: This parameter no longer exists. Instead,
saroyanm 2018/06/13 15:36:25 Done.
229 * Parameters:
230 * * Error message
231 * * Object containing fileName, locale and strings
232 * @returns {Promise}
233 */
234 function readJson(filePath, callback)
235 {
236 return new Promise((resolve, reject) =>
Thomas Greiner 2018/06/12 14:50:42 You can simply return the promise you get from `re
saroyanm 2018/06/13 15:36:25 Done.
237 {
238 const {dir, base} = path.parse(filePath);
239 readFile(filePath, "utf8").then((data) =>
240 {
241 const locale = dir.split(path.sep).pop();
242 const strings = JSON.parse(data);
243 resolve({fileName: base, locale, strings});
244 }).catch((err) =>
245 {
246 reject(err);
247 });
248 });
249 }
250
251 /**
252 * Exit process and log error message
253 * @param {String} error error message
254 */
255 function exitProcess(error)
256 {
257 console.error(error);
258 process.exit(1);
259 }
260
261 // CLI
262 const helpText = `
263 About: Converts locale files between CSV and JSON formats
264 Usage: csv-export.js [option] [argument]
265 Options:
266 -f [FILENAME] Name of the files to be exported ex.: -f firstRun.json
267 option can be used multiple times.
268 If omitted all files are being exported
269
270 -o [FILENAME] Output filename ex.:
271 -f firstRun.json -o {hash}-firstRun.csv
272 Placeholders:
273 {hash} - Mercurial current revision hash
Thomas Greiner 2018/06/12 14:50:42 Detail: Again, these placeholders no longer exists
saroyanm 2018/06/13 15:36:26 Done.
274 If omitted the output fileName is set to
275 translations-{repo}-{hash}.csv
276
277 -i [FILENAME] Import file path ex: -i issue-reporter.csv
278 `;
279
280 const argv = process.argv.slice(2);
281 let stopExportScript = false;
282 // Filter to be used export to the fileNames inside
283 const filesFilter = [];
284
285 for (let i = 0; i < argv.length; i++)
286 {
287 switch (argv[i])
288 {
289 case "-h":
290 console.log(helpText); // eslint-disable-line no-console
291 stopExportScript = true;
292 break;
293 case "-f":
294 if (!argv[i + 1])
295 {
296 exitProcess("Please specify the input filename");
297 }
298 filesFilter.push(argv[i + 1]);
299 break;
300 case "-o":
301 if (!argv[i + 1])
302 {
303 exitProcess("Please specify the output filename");
304 }
305 outputFileName = argv[i + 1];
306 break;
307 case "-i":
308 if (!argv[i + 1])
309 {
310 exitProcess("Please specify the import file");
311 }
312 const importFile = argv[i + 1];
313 importTranslations(importFile);
314 stopExportScript = true;
315 break;
316 }
317 }
318
319 if (!stopExportScript)
320 exportTranslations(filesFilter);
OLDNEW
« no previous file with comments | « README.md ('k') | package.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld