这是从react-intl wiki修改的示例
查看下面的代码,<IntlProvider>
您需要传入一个messages={{post.title: "Hello World", post.body: "Amazing Content"}}
. 这将是您从构建脚本中翻译的带有键的对象。
将语言环境切换为“en”将加载默认消息。addLocaleData 添加用于翻译数字和日期格式的数据,而不是字符串。
import React, {PropTypes} from 'react';
import ReactDOM from 'react-dom';
import {addLocaleData} from 'react-intl';
import en from 'react-intl/locale-data/en';
import fr from 'react-intl/locale-data/fr';
import es from 'react-intl/locale-data/es';
import pt from 'react-intl/locale-data/pt'
addLocaleData([...en, ...fr, ...es, ...pt]);
import {
injectIntl,
IntlProvider,
FormattedRelative,
FormattedMessage
} from 'react-intl';
const PostDate = injectIntl(({date, intl}) => (
<span title={intl.formatDate(date)}>
<FormattedRelative value={date}/>
</span>
));
const App = ({post}) => (
<div>
<h1>{post.title}</h1>
<p><PostDate date={post.date}/></p>
<div>{post.body}</div>
</div>
);
ReactDOM.render(
<IntlProvider locale={'pt'} messages={{post.title: "Olá Mundo", post.body: "conteúdo surpreendente"}}>
<App
post={{
title: <FormattedMessage id='post.title' defaultMessage='Hello, World!'} />,
date: new Date(1459913574887),
body: <FormattedMessage id='post.body' defaultMessage='Amazing Content!'} />,
}}
/>
</IntlProvider>,
document.getElementById('container')
);