Skip to content

Instantly share code, notes, and snippets.

@ozio
Forked from oun/formatMessage.js
Last active February 18, 2019 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozio/b914018750f3a41024c0b09681d9d01b to your computer and use it in GitHub Desktop.
Save ozio/b914018750f3a41024c0b09681d9d01b to your computer and use it in GitHub Desktop.
Use React-Intl API outside React component
import { IntlProvider, defineMessages } from 'react-intl';
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