"babel-plugin-react-intl" is not working as expected

2.2k views Asked by At

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"
}
}
1

There are 1 answers

5
Tom Ehrlich On BEST ANSWER

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 your es2018 and react presets with react-app preset, which is the one used by create-react-app.

NOTE: when using react-app preset you actually need to run NODE_ENV=development ./node_modules/.bin/babel src --out-dir lib, because react-scripts require NODE_ENV to be configured. You can add this to you package.json to make things easier.

"scripts": {
  "extract": "NODE_ENV=development babel src --out-dir lib",
  ...
},

I faced a similar issue in i18n library I'm writing, lingui. lingui extract command is checking if a project uses create-react-app and if so, it uses react-app preset automatically for extracting messages.