2020-01-17 23:37:52 +03:00
|
|
|
import React, { useState, useEffect, ComponentType } from 'react';
|
|
|
|
import { useSelector } from 'react-redux';
|
2019-12-07 13:28:52 +02:00
|
|
|
import { RawIntlProvider, IntlShape } from 'react-intl';
|
2019-12-07 21:02:00 +02:00
|
|
|
import i18n from 'app/services/i18n';
|
|
|
|
import { RootState } from 'app/reducers';
|
2016-05-19 22:41:43 +03:00
|
|
|
|
2020-01-17 23:37:52 +03:00
|
|
|
const IntlProvider: ComponentType = ({ children }) => {
|
2019-11-27 11:03:32 +02:00
|
|
|
const [intl, setIntl] = useState<IntlShape>(i18n.getIntl());
|
2020-01-17 23:37:52 +03:00
|
|
|
const locale = useSelector(
|
|
|
|
({ i18n: i18nState }: RootState) => i18nState.locale,
|
|
|
|
);
|
2016-05-19 22:41:43 +03:00
|
|
|
|
2019-11-27 11:03:32 +02:00
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
setIntl(await i18n.changeLocale(locale));
|
|
|
|
})();
|
|
|
|
}, [locale]);
|
2016-05-19 22:41:43 +03:00
|
|
|
|
2019-11-27 11:03:32 +02:00
|
|
|
return <RawIntlProvider value={intl}>{children}</RawIntlProvider>;
|
2020-01-17 23:37:52 +03:00
|
|
|
};
|
2016-05-19 22:41:43 +03:00
|
|
|
|
2020-01-17 23:37:52 +03:00
|
|
|
export default IntlProvider;
|