Step 1. Include the libraries:
import {FormattedMessage, defineMessages} from 'react-intl';
Step 2. (If necessary) Include those to the end of the component, right before export default (see example 3 below):
Home.contextTypes = { intl: React.PropTypes.object.isRequired };
Step 3. Encode all strings to be collected for translation:
# | As Is | To Be |
---|---|---|
1 | Plain text line <h2 className="ui center aligned grey header"> SlideWiki revolutionises how educational material... </h2> | Plain text line <h2 className="ui center aligned grey header"> <FormattedMessage id='home.slogan' defaultMessage='SlideWiki revolutionises how educational material...' /> </h2> OR: Plain text line (optional) class Home extends React.Component { render() { ... const messages = defineMessages({ slogan:{ id: 'home.slogan', defaultMessage: 'SlideWiki revolutionises how educational material...' } }); return ( ... <FormattedMessage {...messages.slogan} /> ... ) } } Note: the first way is recommended |
2 | A fragment with links <p> If ... follow us on <a target="_blank" href="https://twitter.com/SlideWiki">Twitter</a> or visit the <a href="https://slidewiki.eu">SlideWiki project website</a>. </p> | A fragment with links <p> <FormattedMessage id='home.welcome12' values={{ link_1: <a target="_blank" href="https://twitter.com/SlideWiki">Twitter</a>, link_2: <a href="https://slidewiki.eu"> <FormattedMessage id="home.welcome12.1" defaultMessage="SlideWiki project website"/> </a> }} defaultMessage={'If ... follow us on {link_1} or visit the {link_2}.'} /> </p> Note: The text inside the first link ('Twitter') will not be translated, while the text inside the second link ('SlideWiki project website') will be translated |
3 | Text from component/tag property <img className="ui centered image" src="/assets/images/logo_full.png" alt="SlideWiki beta logo" /> | Text from component/tag property: Using a function <FormattedMessage id="home.logo_alt" defaultMessage='SlideWiki beta logo'> { (alt) => <img className="ui centered image" src="/assets/images/logo_full.png" alt={alt} /> } </FormattedMessage> OR (as we probably will have this situation a lot): Text from component/tag property: Using a lower-level intl API class Home extends React.Component { render() { ... const messages = defineMessages({ logo_alt: { id: 'home.logo_alt', defaultMessage: 'SlideWiki beta logo', } }); return ( ... <img className="ui centered image" src="/assets/images/logo_full.png" alt={this.context.intl.formatMessage(messages.logo_alt)} /> ... ) } } Home.contextTypes = { intl: React.PropTypes.object.isRequired }; export default Home; Note: The second way is recommended, however if there is only 1-2 cases in your component, the first way is better. |
4 | Your input here | My output here =) Note: or if you've solved your issue on your own, share it with others! |