Commit 9d0364e1 authored by Matija Obreza's avatar Matija Obreza
Browse files

i18n: localesMapping path fix

- Report loading errors
parent 4f4980c3
......@@ -22,7 +22,8 @@ const backend = {
const localeMapping = JSON.parse(window.localesMapping);
axios.get(localeMapping[url])
.then((resp) => resp as any)
.then((resp) => callback(JSON.stringify(resp.data), resp));
.then((resp) => callback(JSON.stringify(resp.data), resp))
.catch((err) => { console.log(`Failed to load mapped locale ${url}`, err)});
},
// addPath: '/locales/{{lng}}/{{ns}}.missing.json',
};
......@@ -37,7 +38,7 @@ i18n
.use(initReactI18next)
.init({
detection: {
order: ['catalogLangDetector'],
order: [ langDetector.name ],
lookupFromPathIndex: 0,
},
react: {
......
......@@ -6,6 +6,7 @@ const fs = require('fs');
const md5 = require('js-md5');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const Path = require('path');
// console.log('Env', process.env);
// console.log('Args' , process.argv);
......@@ -60,15 +61,20 @@ const generateHashedLocales = () => {
const localeHashMapping = {}; // map commonPath -> pathWithHash
mkdirp(`${rootDir}/generated/locales`, () => {
entries.forEach((path) => {
const fullPath = Path.normalize(Path.isAbsolute(path) ? path : Path.join(process.cwd(), path));
const fileContent = fs.readFileSync(path); // reading content for generating hash
const contentHash = md5(fileContent);
const mappingPath = fullPath.substring(fullPath.indexOf('/locales'));
// console.log(`${path} --> ${fullPath} --> ${mappingPath}`);
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
console.log(`Mapped ${path} to ${newFilePath}`);
mkdirp.sync(`${rootDir}/${generatedLocaleDirPath}`); // create dir if no exist
fs.writeFileSync(`${rootDir}/${newFilePath}`, fileContent, { flag: 'wx' });
localeHashMapping[path.substring(1)] = newFilePath.replace('./generated', ''); // deleting '/generated' from new path for further loading
localeHashMapping[mappingPath] = newFilePath.replace('./generated', ''); // deleting '/generated' from new path for further loading
// console.log(`Generated ${ newFilePath } for ${ path }`);
});
console.log('Locale mappings', localeHashMapping);
fs.writeFileSync(`${rootDir}/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