scratch-blocks/i18n/json_to_js.js
chrisgarrity 5cb1651e56 Clarifications and consistency
*  removed all the generated files (<locale>.json, <locale>.js) except for ‘en’ versions and added them to git ignore.
* updated cleanup.sh to use our messages.
* added validation check to fail if any translations include newlines
* skip generating js files other than ‘en’ as we don’t use them
* Revised messages.js to use _single_ quoted strings instead of double (consistent with Blockly - if we need to import more strings), and switch the validations to check for single not double quotes.
* additional small changes for simplicity and ergonomics

Not handling category menu at this time as it’s managed differently in scratch-gui.
2018-06-13 10:50:20 -04:00

52 lines
1.6 KiB
JavaScript

const async = require('async');
const fs = require('fs');
const path = require('path');
const glob = require('glob');
// Globals
const PATH_INPUT = path.resolve(__dirname, '../msg/json/en.json');
// If you want to generate js files for other languages, comment out the line above,
// and use the one below instead.
// const PATH_INPUT = path.resolve(__dirname, '../msg/json/*.json');
const PATH_OUTPUT = path.resolve(__dirname, '../msg/js');
const CONCURRENCY_LIMIT = 4;
// Processing task
const work = function (uri, callback) {
fs.readFile(uri, function (err, body) {
const name = path.parse(uri).name;
if (err) return callback(err);
// Convert file body into an object (let this throw if invalid JSON)
body = JSON.parse(body);
// File storage object and preamble
let file = '';
file += '// This file was automatically generated. Do not modify.\n';
file += '\n';
file += '\'use strict\';\n';
file += '\n';
file += `goog.provide(\'Blockly.Msg.${name}\');\n`;
file += 'goog.require(\'Blockly.Msg\');\n';
file += '\n';
// Iterate over object and build up file
for (let i in body) {
file += `Blockly.Msg["${i}"] = "${body[i].replace(/"/g, '\\"')}";\n`
}
// Write file to disk
fs.writeFile(`${PATH_OUTPUT}/${name}.js`, file, callback);
});
};
// Create async processing queue
const q = async.queue(work, CONCURRENCY_LIMIT);
// Handle errors
q.error = function (err) {
throw new Error(err);
};
// Get all JSON files in input directory and add to queue
q.push(glob.sync(PATH_INPUT));