Add script for building en.json

Add dependency on babel-cli and the intl plugins for extracting strings from source.

Include script for combining extracted strings into a single source chrome-i18n file as a binary from this package.
This commit is contained in:
chrisgarrity 2017-10-31 11:44:02 -04:00
parent 782c4498a2
commit 12a43c94d3
2 changed files with 53 additions and 1 deletions

View file

@ -3,6 +3,9 @@
"version": "2.0.0", "version": "2.0.0",
"description": "Localization for the Scratch 3.0 components", "description": "Localization for the Scratch 3.0 components",
"main": "./dist/l10n.js", "main": "./dist/l10n.js",
"bin": {
"build-i18n-src": "./scripts/build-i18n-src.js"
},
"scripts": { "scripts": {
"build:data": "babel-node scripts/build-data", "build:data": "babel-node scripts/build-data",
"build": "npm run clean && npm run build:data && webpack --progress --colors --bail", "build": "npm run clean && npm run build:data && webpack --progress --colors --bail",
@ -20,8 +23,12 @@
"url": "https://github.com/LLK/scratch-l10n/issues" "url": "https://github.com/LLK/scratch-l10n/issues"
}, },
"homepage": "https://github.com/LLK/scratch-l10n#readme", "homepage": "https://github.com/LLK/scratch-l10n#readme",
"devDependencies": { "dependencies": {
"babel-cli": "^6.26.0", "babel-cli": "^6.26.0",
"babel-plugin-intl": "^0.1.1",
"babel-plugin-react-intl": "^2.3.1"
},
"devDependencies": {
"babel-core": "^6.26.0", "babel-core": "^6.26.0",
"babel-eslint": "^7.2.3", "babel-eslint": "^7.2.3",
"babel-loader": "^7.1.2", "babel-loader": "^7.1.2",

45
scripts/build-i18n-src.js Executable file
View file

@ -0,0 +1,45 @@
#!/usr/bin/env node
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const mkdirp = require('mkdirp');
var args = process.argv.slice(2);
if (!args.length) {
process.stdout.write('You must specify the messages dir generated by babel-plugin-react-intl.\n');
process.exit(1);
}
const MESSAGES_PATTERN = args.shift() + '/**/*.json';
if (!args.length) {
process.stdout.write('A destination directory must be specified.\n');
process.exit(1);
}
const LANG_DIR = args.shift();
// Aggregates the default messages that were extracted from the example app's
// React components via the React Intl Babel plugin. An error will be thrown if
// there are messages in different components that use the same `id`. The result
// is a chromei18n format collection of `id: {message: defaultMessage,
// description: description}` pairs for the app's default locale.
let defaultMessages = glob.sync(MESSAGES_PATTERN)
.map((filename) => fs.readFileSync(filename, 'utf8'))
.map((file) => JSON.parse(file))
.reduce((collection, descriptors) => {
descriptors.forEach(({id, defaultMessage, description}) => {
if (collection.hasOwnProperty(id)) {
throw new Error(`Duplicate message id: ${id}`);
}
collection[id] = {message: defaultMessage, description: description};
});
return collection;
}, {});
mkdirp.sync(LANG_DIR);
fs.writeFileSync(path.join(LANG_DIR, 'en.json'), JSON.stringify(defaultMessages, null, 2));