Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
themeDJango
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):


Code Block
languagexml
themeDJango
Home.contextTypes = {
    intl: React.PropTypes.object.isRequired
};


Step 3. Encode all strings to be collected for translation:


#As IsTo Be
1


Code Block
languagexml
themeDJango
titlePlain text line
<h2 className="ui center aligned grey header">
	SlideWiki revolutionises how educational material...
</h2>



Code Block
languagexml
themeDJango
titlePlain text line
<h2 className="ui center aligned grey header">
	<FormattedMessage 
		id='home.slogan'
		defaultMessage='SlideWiki revolutionises how educational material...'
	/>
</h2>

OR:

Code Block
languagexml
themeDJango
titlePlain 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


Code Block
languagexml
themeDJango
titleA 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>



Code Block
languagexml
themeDJango
titleA 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


Code Block
languagexml
themeDJango
titleText from component/tag property
<img 
	className="ui centered image" 
	src="/assets/images/logo_full.png"
	alt="SlideWiki beta logo"
/>



Code Block
languagexml
themeDJango
titleText 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):

Code Block
languagejs
themeDJango
titleText 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


Code Block
languagexml
themeDJango
titleYour input here



Code Block
languagexml
themeDJango
titleMy output here =)

Note: or if you've solved your issue on your own, share it with others!