我已经使用create-react-app
命令创建了我的反应应用程序。现在我想以印地语和英语提供我的网站。因此,我正在使用yahoo/react-intl
. 我还安装了 babel-plugin-react-intl 插件。还添加了 .babelrc 文件。
我的.babelrc
文件看起来像:
{
"presets": ["es2015", "react"],
"plugins": [
[
"react-intl",
{
"messagesDir": "./build/messages/"
}
]
]
}
但是我没有从我的 js 文件中获取提取的消息到./build/messages/
.
我不知道我在哪里犯了错误。
我的 index.js 是:
import React from "react";
import ReactDOM from "react-dom";
import { IntlProvider, addLocaleData } from "react-intl";
import hi from "react-intl/locale-data/hi";
import en from "react-intl/locale-data/en";
import registerServiceWorker from "./registerServiceWorker";
import Hello from "./components/Hello";
addLocaleData([...en, ...hi]);
ReactDOM.render(
<IntlProvider locale="hi" messages={/*comming soon*/}>
<Hello />
</IntlProvider>,
document.getElementById("root")
);
registerServiceWorker();
我所有的 js 文件都在 components 文件夹中。
敌人示例我的Hello.js
组件如下所示:
import React, { Component } from "react";
import { FormattedMessage } from "react-intl";
class Hello extends Component {
render() {
return (
<div>
<h1>
<FormattedMessage id="Hello.heading" defaultMessage="Hello world" />
</h1>
<p>
<FormattedMessage
id="Hello.paragraph"
defaultMessage="This is my world"
/>
</p>
</div>
);
}
}
export default Hello;
我package.json
的是:
{
"name": "i18n-myexamplae",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-intl": "^2.4.0",
"react-scripts": "1.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"babel-plugin-react-intl": "^2.4.0"
}
}