Commit ae6e41e5 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov
Browse files

fixed i18n namespaces generation

parent d3809452
{
"title": "Grin global application"
"action": {
"logout": "Logout",
"submit": "Submit"
}
}
......@@ -4,7 +4,7 @@
"license": "Apache-2.0",
"scripts": {
"clean": "rimraf lib",
"build": "gg-i18n && tsc",
"build": "yarn run i18nGenerate && tsc",
"i18nGenerate": "gg-i18n --moduleName=client",
"test": "jest"
},
......
import * as React from 'react';
import { Link } from 'react-router-dom';
import { withTranslation, WithTranslation } from 'react-i18next';
class WelcomePage extends React.Component<any> {
class WelcomePage extends React.Component<WithTranslation> {
public render() {
const { t } = this.props;
return (
<>
<h1>Welcome</h1>
<h1>{ t('p.welcome.title') }</h1>
<ul>
<li><Link to="/user">To user list</Link></li>
<li><Link to="/login">To login page</Link></li>
......@@ -18,4 +20,4 @@ class WelcomePage extends React.Component<any> {
}
export default WelcomePage;
export default withTranslation()(WelcomePage);
......@@ -28,25 +28,29 @@ const backend = {
// addPath: '/locales/{{lng}}/{{ns}}.missing.json',
};
const backendKey = 'backend';
optionsBase[backendKey] = backend;
i18n
.use(XHR)
.use(lngDetector)
.use(initReactI18next)
.init({
detection: {
order: [ langDetector.name ],
lookupFromPathIndex: 0,
},
react: {
useSuspense: false,
},
...optionsBase,
});
const i18nClient = (modules = []) => {
const backendKey = 'backend';
const optBase = optionsBase(modules);
optBase[backendKey] = backend;
i18n
.use(XHR)
.use(lngDetector)
.use(initReactI18next)
.init({
detection: {
order: [ langDetector.name ],
lookupFromPathIndex: 0,
},
react: {
useSuspense: false,
},
...optBase,
});
return i18n;
};
export { i18n as i18nClient };
export { i18nClient };
......@@ -13,23 +13,30 @@ const backend = {
// addPath: path.resolve('../assets/locales/{{lng}}/{{ns}}.missing.json'),
};
const backendKey = 'backend';
optionsBase[backendKey] = backend;
i18n
.use(Backend)
.use(i18nextMiddleware.LanguageDetector)
.use(initReactI18next)
.init({
detection: {
order: ['path'],
lookupFromPathIndex: 0,
},
react: {
useSuspense: false,
},
...optionsBase,
});
export {i18n as i18nServer} ;
const i18nServer = (modules = []) => {
const backendKey = 'backend';
const optBase = optionsBase(modules);
optBase[backendKey] = backend;
i18n
.use(Backend)
.use(i18nextMiddleware.LanguageDetector)
.use(initReactI18next)
.init({
detection: {
order: ['path'],
lookupFromPathIndex: 0,
},
react: {
useSuspense: false,
},
...optBase,
});
return i18n;
};
export { i18nServer } ;
const optionsBase = {
const optionsBase = (modules = []) => ({
fallbackLng: 'en',
preload: ['en'],
load: 'languageOnly' as 'languageOnly', // we only provide en, de -> no region specific locals like en-US, de-DE
// have a common namespace used around the full app
ns: ['translations', 'common'],
defaultNS: 'translations',
ns: [...modules],
defaultNS: modules[0] || 'translations',
nsSeparator: ':', // namespace separator
keySeparator: '.', // key separator
saveMissing: false,
......@@ -33,6 +33,6 @@ const optionsBase = {
return value;
},
},
};
});
export default optionsBase;
......@@ -73,12 +73,13 @@ const generateHashedLocales = () => {
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 newFilePath = path.substring(path.indexOf('/locales')).replace('locales', 'generated/locales');
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[mappingPath] = newFilePath.replace('./generated', ''); // deleting '/generated' from new path for further loading
fs.writeFileSync(`${rootDir}/${newFilePath.replace('.json', `-${ contentHash }.json`)}`, fileContent, { flag: 'wx' }); // adding hash before .json
fs.writeFileSync(`${rootDir}/${newFilePath}`, fileContent, { flag: 'wx' }); // copying old file for backend
localeHashMapping[mappingPath] = newFilePath.replace('/generated', ''); // deleting '/generated' from new path for further loading
// console.log(`Generated ${ newFilePath } for ${ path }`);
if (mappingPath.endsWith(`/${moduleName}.json`)) {
console.log(`Registering 'translations.json' mapping for ${mappingPath}`);
......
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