|
const es = require('event-stream');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const assert = require('assert');
|
|
|
|
|
|
let locale = '';
|
|
let keys = [];
|
|
|
|
|
|
let en = fs.readFileSync(path.resolve(__dirname, '../msg/json/en.json'));
|
|
en = JSON.parse(en);
|
|
const enKeys = Object.keys(en);
|
|
|
|
|
|
const PATH_INPUT = path.resolve(__dirname, '../msg/scratch_msgs.js');
|
|
|
|
|
|
|
|
|
|
|
|
const match = function (str) {
|
|
if (str.indexOf('Blockly.ScratchMsgs.locales') !== 0) return true;
|
|
if (str.indexOf('": "') !== 0) return true;
|
|
if (str.indexOf('End of combined translations') !== 0) return true;
|
|
return false;
|
|
}
|
|
|
|
|
|
const extract = function (str) {
|
|
let m = str.match(/locales\["(.+)"\]/);
|
|
if (m) {
|
|
|
|
return m[1];
|
|
}
|
|
m = str.match(/"(.*)": "(.*)",?$/);
|
|
if (m) {
|
|
return {
|
|
key: m[1],
|
|
value: m[2]
|
|
}
|
|
}
|
|
|
|
m = str.match(/^\/\/ End of combined translations$/);
|
|
if (m) return 'last';
|
|
return null;
|
|
};
|
|
|
|
const validateKeys = function () {
|
|
|
|
if (keys.length === 0) return;
|
|
assert.strictEqual(keys.length, Object.keys(en).length, `scratch_msgs-${locale}: number of keys doesn't match`);
|
|
keys.map(item => assert(enKeys.includes(item), `scratch_msgs-${locale}: has key ${item} not in en`));
|
|
enKeys.map(item => assert(keys.includes(item), `scratch_msgs-${locale}: is missing key ${item}`));
|
|
}
|
|
|
|
|
|
const stream = fs.createReadStream(PATH_INPUT);
|
|
stream
|
|
.pipe(es.split('\n'))
|
|
.pipe(es.mapSync(function (str) {
|
|
if (!match(str)) return;
|
|
const result = extract(str);
|
|
if (!result) return;
|
|
if (typeof result === 'string') {
|
|
|
|
try {
|
|
validateKeys();
|
|
}
|
|
catch (err) {
|
|
console.error('Key validation FAILED: %O', err);
|
|
process.exit(1);
|
|
}
|
|
|
|
locale = result;
|
|
keys = [];
|
|
} else {
|
|
keys.push(result.key);
|
|
}
|
|
}))
|
|
.pipe(es.wait(function (err) {
|
|
if (err) {
|
|
console.err(err);
|
|
process.exit(1);
|
|
}
|
|
process.exit(0)
|
|
}));
|
|
|