Mahesh
Mahesh

Reputation: 863

Is there any way to get current intl value outside a react component

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

Answers (2)

onmyway133
onmyway133

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

Long Ho
Long Ho

Reputation: 627

Yes you can use createIntl API: https://formatjs.io/docs/react-intl/api/#createintl

Upvotes: 2

Related Questions