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

41 lines
1.1 KiB
TypeScript
Raw Normal View History

import React, { useState, useEffect, ComponentType } from 'react';
2019-12-07 16:58:52 +05:30
import { RawIntlProvider, IntlShape } from 'react-intl';
2021-03-25 09:05:50 +05:30
import { Helmet } from 'react-helmet-async';
import { getLangDir } from 'rtl-detect';
import i18n from 'app/services/i18n';
import { useReduxSelector } from 'app/functions';
2016-05-20 01:11:43 +05:30
const IntlProvider: ComponentType = ({ children }) => {
const [intl, setIntl] = useState<IntlShape>();
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
setIntl(i18n.getIntl());
2020-05-24 04:38:24 +05:30
return;
}
2020-05-24 04:38:24 +05:30
(async () => {
setIntl(await i18n.changeLocale(locale));
})();
}, [locale]);
2016-05-20 01:11:43 +05:30
// don't run the application until locale bundle will be loaded
if (!intl) {
return null;
}
2021-03-25 09:05:50 +05:30
return (
<RawIntlProvider value={intl}>
<Helmet htmlAttributes={{ lang: locale, dir: getLangDir(locale) }} />
{children}
</RawIntlProvider>
);
};
2016-05-20 01:11:43 +05:30
export default IntlProvider;