scratch-l10n/scripts/tx_util.js
chrisgarrity c286f62d69 Add scripts for automation
* bumped minor version
* added localeMap for converting between transifex locales and the ones used by Scratch (e.g., pt-br - pt_BR)
* using the transifex node package to automatically sync translations
* added scripts:
  * sync_tx_src: uploads an en.json source file, for use by client packages
  * sync_tx_translations: downloads gui translations, used by this repo, flattens Chrome i18n json into plain key-value json.
  * sync_tx_blocks: same as above, but blocks need slightly different handling
  * validate_translations: check the translation json files for basic translation errors
  * tx_util - methods shared by sync and validate
* simplified build-data because jsons have already been flattened
* added new npm tasks
2018-11-25 18:05:44 -05:00

34 lines
1.4 KiB
JavaScript

import assert from 'assert';
import parse from 'format-message-parse';
const flattenJson = (translations) => {
let messages = Object.keys(translations).reduce((collection, id) => {
collection[id] = translations[id].message;
return collection;
}, {});
return JSON.stringify(messages, null, 4);
};
const validMessage = (message, source) => {
// this will throw an error if the message is not valid icu
const t = parse(message);
const s = parse(source);
// the syntax tree for both messages should have the same number of elements
return t.length === s.length;
};
const validateTranslations = (translation, source) => {
const locale = translation.locale;
const translations = translation.translations;
const transKeys = Object.keys(translations);
const sourceKeys = Object.keys(source);
assert.strictEqual(transKeys.length, sourceKeys.length, `locale ${locale} has a different number of message keys`);
transKeys.map(item => assert(sourceKeys.includes(item), `locale ${locale} has key ${item} not in the source`));
sourceKeys.map(item => assert(transKeys.includes(item), `locale ${locale} is missing key ${item}`));
sourceKeys.map(item => assert(
validMessage(translations[item], source[item]),
`locale ${locale}: "${translations[item]}" is not a valid translation for "${source[item]}"`)
);
};
export {flattenJson, validateTranslations, validMessage};