accounts-frontend/packages/app/components/i18n/IntlProvider.tsx

28 lines
789 B
TypeScript
Raw Normal View History

import React, { useState, useEffect, ComponentType } from 'react';
import { useSelector } from 'react-redux';
2019-12-07 16:58:52 +05:30
import { RawIntlProvider, IntlShape } from 'react-intl';
import i18n from 'app/services/i18n';
import { RootState } from 'app/reducers';
2016-05-20 01:11:43 +05:30
const IntlProvider: ComponentType = ({ children }) => {
const [intl, setIntl] = useState<IntlShape>(i18n.getIntl());
const locale = useSelector(
({ i18n: i18nState }: RootState) => i18nState.locale,
);
2016-05-20 01:11:43 +05:30
useEffect(() => {
if (process.env.NODE_ENV === 'test') {
// disable async modules loading in tests
return;
}
(async () => {
setIntl(await i18n.changeLocale(locale));
})();
}, [locale]);
2016-05-20 01:11:43 +05:30
return <RawIntlProvider value={intl}>{children}</RawIntlProvider>;
};
2016-05-20 01:11:43 +05:30
export default IntlProvider;