Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

Does this work with create-react-app? #108

Closed
bmueller-sykes opened this issue Apr 20, 2017 · 10 comments
Closed

Does this work with create-react-app? #108

bmueller-sykes opened this issue Apr 20, 2017 · 10 comments

Comments

@bmueller-sykes
Copy link

I'm pretty sure the answer to my question is "no", but I want to ask it anyway, just to be sure.

So my understanding is that I were to go the .babelrc route that when I build a project, I'd wind up with a translations JSON file somewhere in my build folder, which I could then hand off to a translation service. But after some digging around, it would appear that the only way to get this plugin to work with a create-react-app-based app is to eject the app, which I don't want to do.

So given that issue, I think the only currently solution is to write a script that I can invoke directly in NodeJS at the command line. I did that, and the code seems to do the right thing (I can share that code, if people want it).

Obviously, it would be cleaner if this could just be part of the translation process, so I was hoping that I was missing something, and that it could be done with a CRA-based app.

Thanks in advance!

@evenchange4
Copy link

Related issue: facebook/create-react-app#1227 (comment)

@sunknudsen
Copy link

It’s possible to trigger babel-plugin-react-intl manually without ejecting (at least using a recent version of react-create-app).

Also, using react-intl-translations-manager it’s possible to easily maintain the translations required by react-intl.

Step 1 Install the following packages.

npm install @babel/cli babel-plugin-react-intl react-intl-translations-manager --save-dev

Step 2 Create a .babelrc file in the root directory of your project.

{
  "presets": ["react-app"],
  "plugins": [
    [
      "react-intl", {
        "messagesDir": "translations/messages",
      }
    ]
  ]
}

Step 3 Create a translationRunner.js file in the root directory of your project.

const manageTranslations = require('react-intl-translations-manager').default;

manageTranslations({
  messagesDirectory: 'translations/messages',
  translationsDirectory: 'src/translations/locales/',
  languages: ['fr', 'en']
});

Step 4 Add the following properties (messages, locales and translate) to your package.json under scripts.

{
  "scripts": {
    "messages": "NODE_ENV=production babel src/**/*.tsx >/dev/null",
    "locales": "node ./translationRunner.js",
    "translate": "npm run messages; npm run locales"
  }
}

Step 5 Run npm run translate and enjoy life again.

@glecetre
Copy link

Hello guys,

I am also trying to use react-intl with a create-react-app (with TypeScript), and I tried what your mention @sunknudsen, but it doesn't extract my messages. Shouldn't it?

For now I try it on only one component which uses defineMessages (from package react-intl).

Could you provide more information please?

@sunknudsen
Copy link

@glecetre I personally use the FormattedMessage component vs defineMessages. Have you tried this approach?

<FormattedMessage
  id="about.description"
  defaultMessage="Lickstats is..."
/>

@glecetre
Copy link

Nope, doesn't work either. I suspect that nothing is even happening in Babel, the console output is completely empty except for the translation manager:

image

I also tried with npm instead of yarn, just in case, but with no success.

@sunknudsen
Copy link

@glecetre Can you please share a screenshot of the folders and files in your project? Anything under translations either in the root of the project or in src?

@guy-blockchain
Copy link

This worked for me:

facebook/create-react-app#1227 (comment)

@jamieallen59
Copy link

jamieallen59 commented Mar 18, 2019

I was having lots of problems with this so wrote a post here for using react-intl, babel-plugin-react-intl and create-react-app, without ejecting.

@rlahoda
Copy link

rlahoda commented Mar 25, 2019

@jamieallen59 Thanks for writing that post! It finally got me working.

@longlho
Copy link
Member

longlho commented May 26, 2019

close due to stale

@longlho longlho closed this as completed May 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants