Commit 89c242cf authored by Viacheslav Pavlov's avatar Viacheslav Pavlov
Browse files

i18n Module, fixed generating of a translations

parent f76a050e
{
"p": {
"welcome": {
"title": "Grin global application"
}
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@
"license": "Apache-2.0",
"scripts": {
"clean": "rimraf target",
"i18nGenerate": "cross-env moduleName=client lerna run i18n:generateI18n --stream",
"test": "jest"
},
"dependencies": {
......
{
"p": {
"welcome":{
"title": "Grin global application"
}
}
}
{
"label": {
"loadingData": "Loading data..."
}
}
......@@ -7,11 +7,11 @@ const rimraf = require('rimraf');
const getPrefix = (path) => {
console.log(`Converting ${path}`);
return path.substring(path.indexOf('./src/') + './src/'.length, path.indexOf('/translations.json'));
return path.substring(path.indexOf('/src/') + '/src/'.length, path.indexOf('/translations.json'));
};
// TODO fix generated paths
fg(['../../**/src/**/translations.json', '../../**/src/translations.json'])
fg([`${process.env.INIT_CWD}/src/**/translations.json`, `${process.env.INIT_CWD}/rc/translations.json`])
.then((entries) => {
console.log(`Inspecting ${entries}`);
return entries.filter((e) => ! /\/node_modules\//.test(e));
......@@ -24,33 +24,42 @@ fg(['../../**/src/**/translations.json', '../../**/src/translations.json'])
});
return result;
})
.then((content) => fs.writeFileSync('locales/en/translations.json', JSON.stringify(content, null, 2)))
.then((content) => {
if (!fs.existsSync(`${process.env.INIT_CWD}/locales/en`)) {
mkdirp.sync(`${process.env.INIT_CWD}/locales/en`); // create dir if no exist
}
fs.writeFileSync(`${process.env.INIT_CWD}/locales/en/${process.env.moduleName || 'translations'}.json`, JSON.stringify(content, null, 2))
})
.then(() => {
const content = fs.readFileSync('locales/en/translations.json');
const content = fs.readFileSync(`${process.env.INIT_CWD}/locales/en/${process.env.moduleName || 'translations'}.json`);
JSON.parse(content);
}).catch((error) => {
console.log('Error reading resulting locales/en/translations.json', error);
console.log(`Error reading resulting locales/en/${process.env.moduleName || 'translations'}.json`, error);
process.exit(-1);
})
.then(() => generateHashedLocales());
const generateHashedLocales = () => {
rimraf('./generated/locales', (err) => { // delete old generated locales
fg(['./locales/**/translations.json', './locales/**/common.json']) // scan for all translations files
const includedLocales = process.env.includedLocales
? process.env.includedLocales.replace(/[\[\]]/gi, '').split(',')
: [];
rimraf(`${process.env.INIT_CWD}/generated/locales`, (err) => { // delete old generated locales
fg([`${process.env.INIT_CWD}/locales/**/${process.env.moduleName || 'translations'}.json`, ...includedLocales]) // scan for all translations files
.then((entries) => {
const localeHashMapping = {}; // map commonPath -> pathWithHash
mkdirp('./generated/locales', () => {
mkdirp(`${process.env.INIT_CWD}/generated/locales`, () => {
entries.forEach((path) => {
const fileContent = fs.readFileSync(path); // reading content for generating hash
const contentHash = md5(fileContent);
const newFilePath = path.replace('locales', 'generated/locales').replace('.json', `-${ contentHash }.json`); // adding hash before .json
const newFilePath = path.substring(path.indexOf('/locales')).replace('locales', 'generated/locales').replace('.json', `-${ contentHash }.json`); // adding hash before .json
const generatedLocaleDirPath = newFilePath.substring(0, newFilePath.lastIndexOf('/')); // getting all but the filename as folder of locale lang
mkdirp.sync(generatedLocaleDirPath); // create dir if no exist
fs.writeFileSync(newFilePath, fileContent, { flag: 'wx' });
mkdirp.sync(`${process.env.INIT_CWD}/${generatedLocaleDirPath}`); // create dir if no exist
fs.writeFileSync(`${process.env.INIT_CWD}/${newFilePath}`, fileContent, { flag: 'wx' });
localeHashMapping[path.substring(1)] = newFilePath.replace('./generated', ''); // deleting '/generated' from new path for further loading
// console.log(`Generated ${ newFilePath } for ${ path }`);
});
fs.writeFileSync('./generated/locales/localesMapping.json', JSON.stringify(localeHashMapping), { encoding: 'utf8' }); // after all locales moved creating file with mappings
fs.writeFileSync(`${process.env.INIT_CWD}/generated/locales/localesMapping.json`, JSON.stringify(localeHashMapping), { encoding: 'utf8' }); // after all locales moved creating file with mappings
});
});
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment