2017-09-21 11:54:41 -04:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/*
|
|
|
|
Generates locales/<component>-msgs.js for each component (gui, etc) from the
|
|
|
|
current translation files for each language for that component
|
|
|
|
|
|
|
|
Translation files are expected to be in Chrome i18n json format:
|
|
|
|
'''
|
|
|
|
{
|
|
|
|
"message.id": {
|
|
|
|
"message": "The translated text",
|
|
|
|
"description": "Tips for translators"
|
|
|
|
},
|
|
|
|
...
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
They are named by locale, for example: 'fr.json' or 'zh-cn.json'
|
|
|
|
|
|
|
|
Converts the collection of translation files to a single set of messages.
|
|
|
|
Example output:
|
|
|
|
'''
|
|
|
|
{
|
|
|
|
"en": {
|
|
|
|
"action.addBackdrop": "Add Backdrop",
|
|
|
|
"action.addCostume": "Add Costume",
|
|
|
|
"action.recordSound": "Record Sound",
|
|
|
|
"action.addSound": "Add Sound"
|
|
|
|
},
|
|
|
|
"fr": {
|
|
|
|
"action.addSound": "Ajouter Son",
|
|
|
|
"action.addCostume": "Ajouter Costume",
|
|
|
|
"action.addBackdrop": "Ajouter Arrière-plan",
|
|
|
|
"action.recordSound": "Enregistrement du Son"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
2018-05-21 09:42:53 -04:00
|
|
|
NOTE: blocks messages are plain key-value JSON files
|
2017-09-21 11:54:41 -04:00
|
|
|
|
|
|
|
Missing locales are ignored, react-intl will use the default messages for them.
|
|
|
|
*/
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import {sync as mkdirpSync} from 'mkdirp';
|
2018-05-21 09:42:53 -04:00
|
|
|
import defaultsDeep from 'lodash.defaultsdeep';
|
2017-09-21 11:54:41 -04:00
|
|
|
import locales from '../src/supported-locales.js';
|
|
|
|
|
|
|
|
const MSGS_DIR = './locales/';
|
2018-05-21 09:42:53 -04:00
|
|
|
mkdirpSync(MSGS_DIR);
|
2017-09-21 11:54:41 -04:00
|
|
|
let missingLocales = [];
|
|
|
|
|
2018-05-21 09:42:53 -04:00
|
|
|
// generate messages for gui components - files are Chrome i18n format json
|
|
|
|
let components = ['interface', 'extensions', 'paint-editor'];
|
|
|
|
let editorMsgs = {};
|
2017-10-17 11:59:48 -04:00
|
|
|
components.forEach((component) => {
|
|
|
|
let messages = Object.keys(locales).reduce((collection, lang) => {
|
|
|
|
let langMessages = {};
|
|
|
|
try {
|
|
|
|
let langData = JSON.parse(
|
2018-05-21 09:42:53 -04:00
|
|
|
fs.readFileSync(path.resolve('editor', component, lang + '.json'), 'utf8')
|
2017-10-17 11:59:48 -04:00
|
|
|
);
|
|
|
|
Object.keys(langData).forEach((id) => {
|
|
|
|
langMessages[id] = langData[id].message;
|
|
|
|
});
|
2018-05-21 09:42:53 -04:00
|
|
|
collection[lang] = langMessages;
|
2017-10-17 11:59:48 -04:00
|
|
|
} catch (e) {
|
|
|
|
missingLocales.push(lang);
|
|
|
|
}
|
|
|
|
return collection;
|
|
|
|
}, {});
|
2018-05-21 09:42:53 -04:00
|
|
|
|
2017-10-17 11:59:48 -04:00
|
|
|
let data =
|
|
|
|
'// GENERATED FILE:\n' +
|
2018-05-21 09:42:53 -04:00
|
|
|
'export default ' +
|
2017-10-17 11:59:48 -04:00
|
|
|
JSON.stringify(messages, null, 2) +
|
2018-05-21 09:42:53 -04:00
|
|
|
';\n';
|
2017-10-17 11:59:48 -04:00
|
|
|
fs.writeFileSync(MSGS_DIR + component + '-msgs.js', data);
|
2018-05-21 09:42:53 -04:00
|
|
|
defaultsDeep(editorMsgs, messages);
|
2017-09-21 11:54:41 -04:00
|
|
|
|
2017-10-17 11:59:48 -04:00
|
|
|
if (missingLocales.length > 0) {
|
|
|
|
process.stdout.write('missing locales: ' + missingLocales.toString());
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
});
|
2018-05-21 09:42:53 -04:00
|
|
|
|
|
|
|
// generate the blocks messages: files are plain key-value JSON
|
|
|
|
let blocksMessages = Object.keys(locales).reduce((collection, lang) => {
|
|
|
|
try {
|
|
|
|
let langData = JSON.parse(
|
|
|
|
fs.readFileSync(path.resolve('editor', 'blocks', lang + '.json'), 'utf8')
|
|
|
|
);
|
|
|
|
collection[lang] = langData;
|
|
|
|
} catch (e) {
|
|
|
|
missingLocales.push(lang);
|
|
|
|
}
|
|
|
|
return collection;
|
|
|
|
}, {});
|
|
|
|
let blockData =
|
|
|
|
'// GENERATED FILE:\n' +
|
|
|
|
'export default ' +
|
|
|
|
JSON.stringify(blocksMessages, null, 2) +
|
|
|
|
';\n';
|
|
|
|
|
|
|
|
fs.writeFileSync(MSGS_DIR + 'blocks-msgs.js', blockData);
|
|
|
|
|
|
|
|
// generate combined editor-msgs file
|
|
|
|
let editorData =
|
|
|
|
'// GENERATED FILE:\n' +
|
|
|
|
'export default ' +
|
|
|
|
JSON.stringify(editorMsgs, null, 2) +
|
|
|
|
';\n';
|
|
|
|
fs.writeFileSync(MSGS_DIR + 'editor-msgs.js', editorData);
|