I have created my react app using create-react-app
command. Now I would like to available my website in both hindi and english language.
So for that I am using yahoo/react-intl
. I have also installed the babel-plugin-react-intl plugin. Also added the .babelrc file.
My .babelrc
file look like:
{
"presets": ["es2015", "react"],
"plugins": [
[
"react-intl",
{
"messagesDir": "./build/messages/"
}
]
]
}
But I am not getting the extracted messages from my js file to ./build/messages/
.
I dont know where I made a mistake.
My index.js is:
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();
And all my js-files are inside the components folder.
Foe example my Hello.js
component looks like:
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;
My package.json
is:
{
"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"
}
}
This is because
react-scripts
don't use your.babelrc
. You either need to eject because then you're allowed to edit.babelrc
config or you need to run babel manually.Run
./node_modules/.bin/babel src --out-dir lib
and ignore/delete everything in--out-dir
. You're doing this only to extract messages. Also, to make build consistent, replace youres2018
andreact
presets withreact-app
preset, which is the one used bycreate-react-app
.NOTE: when using
react-app
preset you actually need to runNODE_ENV=development ./node_modules/.bin/babel src --out-dir lib
, becausereact-scripts
require NODE_ENV to be configured. You can add this to youpackage.json
to make things easier.I faced a similar issue in i18n library I'm writing, lingui.
lingui extract
command is checking if a project usescreate-react-app
and if so, it usesreact-app
preset automatically for extracting messages.