Remove defaultMessages from the production build.

Don't run the application before active locale will be loaded
This commit is contained in:
ErickSkrauch
2020-06-05 18:24:41 +03:00
parent e962a01a4a
commit 243510a671
6 changed files with 19 additions and 6 deletions

View File

@@ -5,12 +5,14 @@ import i18n from 'app/services/i18n';
import { RootState } from 'app/reducers';
const IntlProvider: ComponentType = ({ children }) => {
const [intl, setIntl] = useState<IntlShape>(i18n.getIntl());
const [intl, setIntl] = useState<IntlShape>();
const locale = useSelector(({ i18n: i18nState }: RootState) => i18nState.locale);
useEffect(() => {
if (process.env.NODE_ENV === 'test') {
// disable async modules loading in tests
setIntl(i18n.getIntl());
return;
}
@@ -19,6 +21,11 @@ const IntlProvider: ComponentType = ({ children }) => {
})();
}, [locale]);
// don't run the application until locale bundle will be loaded
if (!intl) {
return null;
}
return <RawIntlProvider value={intl}>{children}</RawIntlProvider>;
};

View File

@@ -2,6 +2,7 @@ import { authenticate, logoutStrangers } from 'app/components/accounts/actions';
import { getActiveAccount } from 'app/components/accounts/reducer';
import request from 'app/services/request';
import { Store } from 'app/reducers';
import i18n from 'app/services/i18n';
import { changeLang } from './actions';
import bearerHeaderMiddleware from './middlewares/bearerHeaderMiddleware';
@@ -44,6 +45,10 @@ export function factory(store: Store): Promise<void> {
// auto-detect guest language
await store.dispatch(changeLang(user.lang));
})
.then(() => {
// Preload the current locale before app will be started
i18n.require(store.getState().i18n.locale);
});
return promise;

View File

@@ -70,7 +70,7 @@ class I18N {
locale: string,
): Promise<{
locale: string;
messages: { [key: string]: string };
messages: Record<string, string>;
}> {
const [{ default: messages }] = await Promise.all([
import(/* webpackChunkName: "locale-[request]" */ `app/i18n/${locale}.json`),