Last active
March 18, 2022 18:40
-
-
Save oun/395fabdc24cacbd78b1b37422b07e2c2 to your computer and use it in GitHub Desktop.
Use React-Intl API outside React component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IntlProvider } 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
Ok, found out. When they write :
It means you can do one of them, both, but they are separate ways of using it. It's more : the first one OR the other.