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);
|
2016-10-20 22:42:39 +03:00
|
|
|
const readFile = thenify(fs.readFile);
|
2016-10-20 22:42:39 +03:00
|
|
|
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;
|
|
|
|
})();
|
|
|
|
`;
|
|
|
|
|
|
|
|
const compile = async () => {
|
|
|
|
const files = await readdir(root);
|
|
|
|
const jsons = files.filter(filename => path.extname(filename) === '.json');
|
|
|
|
|
2016-10-20 22:42:39 +03:00
|
|
|
const locales = files.reduce((res, filename) => {
|
2016-10-20 22:42:39 +03:00
|
|
|
const name = path.parse(filename).name;
|
|
|
|
const json = JSON.stringify(require(path.join(root, filename)));
|
|
|
|
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);
|
|
|
|
}));
|
|
|
|
|
2016-10-20 22:42:39 +03:00
|
|
|
|
|
|
|
|
2016-10-20 22:42:39 +03:00
|
|
|
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;
|
|
|
|
});
|