Reputation: 863
can we get current intl(locale) value outside react component?
const locales = {
locale: 'en',
messages: {},
};
import { IntlProvider, addLocaleData } from 'react-intl';
//below is for creating
const intlProvider = new IntlProvider(locales, {});
const { intl } = intlProvider.getChildContext();
//is there any code snippet to get current value from IntlProvider?
Upvotes: 2
Views: 1780
Reputation: 48055
You can use createIntl like
This allows you to create an IntlShape object without using Provider. This allows you to format things outside of React lifecycle while reusing the same intl object.
import { createIntl } from 'react-intl';
const cache = createIntlCache()
const intl = createIntl({
locale: 'en-US',
messages: {
invalid_username: 'Invalid user name'
}
}, cache)
const message = intl.formatMessage({id: invalid_username})
Upvotes: 0
Reputation: 627
Yes you can use createIntl
API: https://formatjs.io/docs/react-intl/api/#createintl
Upvotes: 2