2016-10-20 22:42:39 +03:00
|
|
|
const path = require('path');
|
|
|
|
const thenify = require('thenify');
|
|
|
|
const fs = require('fs');
|
|
|
|
const Ncp = require('ncp');
|
|
|
|
|
|
|
|
const readdir = thenify(fs.readdir);
|
|
|
|
const writeFile = thenify(fs.writeFile);
|
|
|
|
const ncp = thenify(Ncp.ncp);
|
|
|
|
|
|
|
|
const root = path.join(__dirname, '../locales');
|
|
|
|
const sttic = path.join(__dirname, '../static/locales');
|
|
|
|
const intl = path.join(__dirname, '../node_modules/react-intl/locale-data');
|
|
|
|
|
|
|
|
const source = ({
|
|
|
|
name,
|
|
|
|
json
|
|
|
|
}) => `
|
|
|
|
(() => {
|
|
|
|
const Locales = window.Locales || {};
|
|
|
|
Locales['${name}'] = ${json};
|
|
|
|
window.Locales = Locales;
|
|
|
|
})();
|
|
|
|
`;
|
|
|
|
|
2017-01-10 14:25:11 +02:00
|
|
|
const flattenMessages = (nestedMessages, prefix='') => {
|
|
|
|
return Object.keys(nestedMessages).reduce((messages, key) => {
|
|
|
|
const value = nestedMessages[key];
|
|
|
|
const prefixedKey = prefix ? `${prefix}.${key}` : key;
|
|
|
|
|
|
|
|
if(typeof value === 'string') {
|
|
|
|
messages[prefixedKey] = value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Object.assign(messages, flattenMessages(value, prefixedKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
2016-10-20 22:42:39 +03:00
|
|
|
const compile = async () => {
|
|
|
|
const files = await readdir(root);
|
|
|
|
const jsons = files.filter(filename => path.extname(filename) === '.json');
|
|
|
|
|
2016-10-24 16:11:50 +03:00
|
|
|
const locales = jsons.reduce((res, filename) => {
|
2016-10-20 22:42:39 +03:00
|
|
|
const name = path.parse(filename).name;
|
2017-01-10 14:25:11 +02:00
|
|
|
const messages = require(path.join(root, filename));
|
|
|
|
const flattenedMessages = flattenMessages(messages);
|
|
|
|
const json = JSON.stringify(flattenedMessages);
|
2016-10-20 22:42:39 +03:00
|
|
|
const lang = name.split(/\-/)[0];
|
|
|
|
|
|
|
|
return {
|
|
|
|
...res,
|
|
|
|
[name]: {
|
|
|
|
lang,
|
|
|
|
json,
|
|
|
|
filename
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
await Promise.all(Object.keys(locales).map((name) => {
|
|
|
|
console.log(`Copying locale-data for ${name}`);
|
|
|
|
|
|
|
|
const locale = locales[name];
|
|
|
|
const source = path.join(intl, `${locale.lang}.js`);
|
|
|
|
const destination = path.join(sttic, `${locale.lang}.js`);
|
|
|
|
|
|
|
|
return ncp(source, destination);
|
|
|
|
}));
|
|
|
|
|
|
|
|
return await Promise.all(Object.keys(locales).map((name) => {
|
|
|
|
console.log(`Writing ${name}.js`);
|
|
|
|
|
|
|
|
const locale = locales[name];
|
|
|
|
|
|
|
|
return writeFile(path.join(sttic, `${name}.js`), source({
|
|
|
|
...locale,
|
|
|
|
name
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log('Building Locales');
|
|
|
|
compile().then(() => {
|
|
|
|
console.log('Locales Built');
|
|
|
|
}, (err) => {
|
|
|
|
throw err;
|
|
|
|
});
|