"babel-plugin-react-intl" 未按预期工作
"babel-plugin-react-intl" is not working as expected
我已经使用 create-react-app
命令创建了我的 React 应用程序。现在我想以印地语和英语提供我的网站。
所以为此我使用 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 文件都在组件文件夹中。
敌人示例我的 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"
}
}
这是因为 react-scripts
没有使用您的 .babelrc
。您要么需要弹出,因为这样您就可以编辑 .babelrc
配置,或者您需要手动 运行 babel。
运行 ./node_modules/.bin/babel src --out-dir lib
和 ignore/delete --out-dir
中的所有内容。您这样做只是为了提取消息。此外,为了使构建保持一致,请将 es2018
和 react
预设替换为 react-app
预设,这是 create-react-app
.
使用的预设
注意: 使用 react-app
预设时实际上需要 运行 NODE_ENV=development ./node_modules/.bin/babel src --out-dir lib
,因为 react-scripts
需要 NODE_ENV 待配置。您可以将此添加到 package.json
以简化操作。
"scripts": {
"extract": "NODE_ENV=development babel src --out-dir lib",
...
},
我在编写的 i18n 库中遇到了类似的问题,lingui。 lingui extract
命令正在检查项目是否使用 create-react-app
,如果是,它会自动使用 react-app
预设来提取消息。
我已经使用 create-react-app
命令创建了我的 React 应用程序。现在我想以印地语和英语提供我的网站。
所以为此我使用 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 文件都在组件文件夹中。
敌人示例我的 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"
}
}
这是因为 react-scripts
没有使用您的 .babelrc
。您要么需要弹出,因为这样您就可以编辑 .babelrc
配置,或者您需要手动 运行 babel。
运行 ./node_modules/.bin/babel src --out-dir lib
和 ignore/delete --out-dir
中的所有内容。您这样做只是为了提取消息。此外,为了使构建保持一致,请将 es2018
和 react
预设替换为 react-app
预设,这是 create-react-app
.
注意: 使用 react-app
预设时实际上需要 运行 NODE_ENV=development ./node_modules/.bin/babel src --out-dir lib
,因为 react-scripts
需要 NODE_ENV 待配置。您可以将此添加到 package.json
以简化操作。
"scripts": {
"extract": "NODE_ENV=development babel src --out-dir lib",
...
},
我在编写的 i18n 库中遇到了类似的问题,lingui。 lingui extract
命令正在检查项目是否使用 create-react-app
,如果是,它会自动使用 react-app
预设来提取消息。