Translation utilities for React and Javascript.
This repo wraps FormatJS and React Intl primitives
yarn add @clustree/intl or npm add @clustree/intl
You'll also need babel-plugin-macros to use the macro.
This project exports Translate and Provider components.
import { Provider, Translate } from '@clustree/intl';
Provider has the same API as Provider from 'react-intl' but it leverages setLocale to make translate work as well.
Translate has the same API as FormattedMessage from 'react-intl', but it handles inline React Components.
<Translate defaultMessage="<Link>HomePage</Link>" components={{Link: <Link to="/">}} />In plain javascript, we use the setLocale and translate helper functions.
setLocale(locale, messages)
translate(defaultMessage, { id?, values })
A babel macro https://github.com/kentcdodds/babel-plugin-macros allows us to extract messages for translation and allows us to have a more explicit api for Translate.
Thus we can write:
<Translate><Link to="/">HomePage</Link></Translate>which is equivalent to the Definition above
Changing locales will unmount the subtree beneath Provider. This allows us to use translate in components without worrying about locale updates in deep subtrees.