Skip to content

Instantly share code, notes, and snippets.

@genadyp
Created February 7, 2018 09:29
Show Gist options
  • Select an option

  • Save genadyp/435f4e264cb6e377836cf63bee8987d8 to your computer and use it in GitHub Desktop.

Select an option

Save genadyp/435f4e264cb6e377836cf63bee8987d8 to your computer and use it in GitHub Desktop.
How to use react-intl outside components
import { IntlProvider } from 'react-intl';
// copied from https://gist.github.com/oun/395fabdc24cacbd78b1b37422b07e2c2
const language = 'en';
// Usually messages is declared in another file.
const messages = {
greeting: 'Hello'
}
export const mesg = defineMessages({
greeting: {
id: 'greeting',
defaultMessage: 'Whats up'
}
};
const { intl } = new IntlProvider({ locale: language, messages: messages }, {}).getChildContext();
intl.formatMessage(mesg.greeting); // "Hello"
intl.formatNumber(1000); // "1,000"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment