2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* This script extracts all the translations in the messagesDescriptors files
|
|
|
|
* in the app, and generates a complete locale file for English.
|
|
|
|
*
|
|
|
|
* This script is meant to be run through `npm run extractTranslations`.
|
|
|
|
*/
|
2016-07-28 23:14:52 +02:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import {sync as globSync} from 'glob';
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Path to look for
|
2016-07-28 23:14:52 +02:00
|
|
|
const MESSAGES_PATTERN = './app/locales/messagesDescriptors/**/*.js';
|
|
|
|
|
|
|
|
// 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 flat collection of `id: message` pairs for the app's default locale.
|
|
|
|
let defaultMessages = globSync(MESSAGES_PATTERN)
|
|
|
|
.map((filename) => require("../" + filename).default)
|
|
|
|
.reduce((collection, descriptors) => {
|
|
|
|
descriptors.forEach(({id, description, defaultMessage}) => {
|
|
|
|
if (collection.hasOwnProperty(id)) {
|
|
|
|
throw new Error(`Duplicate message id: ${id}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
collection.push({
|
|
|
|
id: id,
|
|
|
|
description: description,
|
|
|
|
defaultMessage: defaultMessage
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return collection;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// Sort by id
|
|
|
|
defaultMessages = defaultMessages.sort(function (item1, item2) {
|
|
|
|
return item1.id.localeCompare(item2.id);
|
|
|
|
});
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Output the English translation file
|
2016-07-28 23:14:52 +02:00
|
|
|
console.log("module.exports = {");
|
|
|
|
defaultMessages.forEach(function (item) {
|
|
|
|
console.log(" " + JSON.stringify(item.id) + ": " + JSON.stringify(item.defaultMessage) + ", // " + item.description);
|
|
|
|
});
|
|
|
|
console.log("};");
|