我想用react-intl
来做语言翻译。当我使用这个<FormattedMessage id='importantNews' />
时,它工作得很完美。但是,当我使用intl.formatMessage()
下面的代码时,它不起作用并抛出一些错误。我不知道它有什么问题。intl.formatMessage不起作用 - react-intl
import { injectIntl, FormattedMessage } from 'react-intl';
function HelloWorld(props) {
const { intl } = props;
const x = intl.formatMessage('hello') + ' ' + intl.formatMessage('world'); //not working
const y = <FormattedMessage id='hello' />; //working
return (
<button>{x}</button>
);
}
export default injectIntl(HelloWorld);
我的根组件是这样的,
import ReactDOM from 'react-dom';
import { addLocaleData, IntlProvider } from 'react-intl';
import enLocaleData from 'react-intl/locale-data/en';
import taLocaleData from 'react-intl/locale-data/ta';
import HelloWorld from './hello-world';
addLocaleData([
...enLocaleData,
...taLocaleData
]);
const messages = {
en: {
hello: 'Hello',
world: 'World'
},
ta: {
hello: 'வணக்கம்',
world: 'உலகம்'
}
};
ReactDOM.render(
<IntlProvider key={'en'} locale={'en'} messages={messages['en']}>
<HelloWorld />
</IntlProvider>,
document.getElementById('root')
);
有人可以帮我解决这个问题?提前致谢。
检查道具传递正确与否。 – Vasi
我希望我正确传递道具。你能告诉我你在说什么道具吗? –
国际道具。你能解释你有什么错误吗? – Vasi