2020-01-18 02:07:52 +05:30
|
|
|
import React, { useState, useEffect, ComponentType } from 'react';
|
2019-12-07 16:58:52 +05:30
|
|
|
import { RawIntlProvider, IntlShape } from 'react-intl';
|
2020-07-22 15:31:12 +05:30
|
|
|
|
2019-12-08 00:32:00 +05:30
|
|
|
import i18n from 'app/services/i18n';
|
2020-07-22 15:31:12 +05:30
|
|
|
import { useReduxSelector } from 'app/functions';
|
2016-05-20 01:11:43 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
const IntlProvider: ComponentType = ({ children }) => {
|
2020-06-05 20:54:41 +05:30
|
|
|
const [intl, setIntl] = useState<IntlShape>();
|
2020-07-22 15:31:12 +05:30
|
|
|
const locale = useReduxSelector(({ i18n: i18nState }) => i18nState.locale);
|
2016-05-20 01:11:43 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
useEffect(() => {
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
// disable async modules loading in tests
|
2020-06-05 20:54:41 +05:30
|
|
|
setIntl(i18n.getIntl());
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return;
|
|
|
|
}
|
2020-05-21 23:38:47 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
(async () => {
|
|
|
|
setIntl(await i18n.changeLocale(locale));
|
|
|
|
})();
|
|
|
|
}, [locale]);
|
2016-05-20 01:11:43 +05:30
|
|
|
|
2020-06-05 20:54:41 +05:30
|
|
|
// don't run the application until locale bundle will be loaded
|
|
|
|
if (!intl) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return <RawIntlProvider value={intl}>{children}</RawIntlProvider>;
|
2020-01-18 02:07:52 +05:30
|
|
|
};
|
2016-05-20 01:11:43 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export default IntlProvider;
|