* Added Blockly.ScratchMsgs

* Added script to create combined locales `Blockly.ScratchMsgs.locales`
* Regenerated en.json after changing single quoted strings in messages.js to double quoted strings.
* Include `create_scratch_msgs.js` in `npm run translate`
locale=<locale-string>` to the URL.
* Added assertions so translate fails if `messages.js` has incorrect strings, or if any of the downloaded locale json files have errors in the translations.
* Playground will load different locales.
This commit is contained in:
chrisgarrity 2018-06-08 13:58:59 -04:00
parent 737994330c
commit aa4ecd7e2e
11 changed files with 269 additions and 48 deletions

View file

@ -0,0 +1,64 @@
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const assert = require('assert');
// Globals
const PATH_INPUT = path.resolve(__dirname, '../msg/json/*.json');
const PATH_OUTPUT = path.resolve(__dirname, '../msg');
let en = fs.readFileSync(path.resolve(__dirname, '../msg/json/en.json'));
en = JSON.parse(en);
const enKeys = Object.keys(en).sort().toString();
// Check that translation is valid:
// elt: array [key, translation] from <locale>.json
// - messages with placeholders have the same number of placeholders
const validatePlaceholders = function (elt) {
const re = /(%\d)/g;
const [key, translation] = elt;
const placeholdersCount = en[key].match(re) ? en[key].match(re).length : 0;
if (placeholdersCount > 0) {
const tMatch = translation.match(re);
const enMatch = en[key].match(re);
assert.notStrictEqual(tMatch, null, `${key} is missing a placeholder: ${translation}`);
assert.strictEqual(
tMatch.sort().toString(),
enMatch.sort().toString(),
`${key} is missing or has duplicate placeholders: ${translation}`
);
}
};
const validate = function (json, name) {
// this is a little stricter than we need - it would be harmless if the translation had extra keys
assert.strictEqual(Object.keys(json).sort().toString(), enKeys, `${name}: Locale json keys do not match en.json`);
Object.entries(json).forEach(validatePlaceholders);
};
let file = '';
file += '// This file was automatically generated. Do not modify.\n';
file += '\n';
file += '\'use strict\';\n';
file += '\n';
file += `goog.provide(\'Blockly.ScratchMsgs.locales\');\n`;
file += `goog.require(\'Blockly.ScratchMsgs\');\n`;
file += '\n';
let files = glob.sync(PATH_INPUT);
files.forEach(function (uri) {
const name = path.parse(uri).name;
if (name !== 'qqq' && name !== 'synonyms') {
let body = fs.readFileSync(uri);
// Convert file body into an object (let this throw if invalid JSON)
body = JSON.parse(body);
validate(body, name);
file += '\n';
file += `Blockly.ScratchMsgs.locales["${name}"] =\n`;
file += JSON.stringify(body, null, 4);
file += ';\n';
}
});
// write combined file
fs.writeFileSync(`${PATH_OUTPUT}/scratch_msgs.js`, file);

View file

@ -1,6 +1,7 @@
const es = require('event-stream');
const fs = require('fs');
const path = require('path');
const assert = require('assert');
// Storage object
const storage = {};
@ -12,6 +13,7 @@ const PATH_OUTPUT = path.resolve(__dirname, '../msg/json/en.json');
// Match function
const match = function (str) {
if (str.indexOf('Blockly.Msg.') !== 0) return false;
assert.notStrictEqual(str.indexOf("';"), str.length - 2, `[${str}] uses single quoted string, should use double quotes.`);
if (str.indexOf('";') !== str.length - 2) return false;
return true;
}

View file

@ -11,7 +11,8 @@ const CONCURRENCY_LIMIT = 4;
// Processing task
const work = function (uri, callback) {
fs.readFile(uri, function (err, body) {
if (err) return callback(err);
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);
@ -22,7 +23,7 @@ const work = function (uri, callback) {
file += '\n';
file += '\'use strict\';\n';
file += '\n';
file += 'goog.provide(\'Blockly.Msg.en\');\n';
file += `goog.provide(\'Blockly.Msg.${name}\');\n`;
file += 'goog.require(\'Blockly.Msg\');\n';
file += '\n';
@ -32,7 +33,6 @@ const work = function (uri, callback) {
}
// Write file to disk
const name = path.parse(uri).name;
fs.writeFile(`${PATH_OUTPUT}/${name}.js`, file, callback);
});
};