Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Messages not being extracted when using React hook only #1633

Closed
ramsy-leftclick opened this issue Apr 2, 2020 · 7 comments
Closed

Messages not being extracted when using React hook only #1633

ramsy-leftclick opened this issue Apr 2, 2020 · 7 comments

Comments

@ramsy-leftclick
Copy link

Which package?
babel-plugin-react-intl

Describe the bug
When I use the React hook for translating my text in components directly the messages do not get extracted.

To Reproduce

  1. Use the following code in your component
const {formatMessage: f} = useIntl()
...
return <span>{f({id: 'notExtracted', defaultMessage: 'This is not extracted'})}</span>
...
  1. Run babel-plugin-react-intl

Expected behavior
I expected these messages to be extracted in the configured folder (eg. /build/messages/src/...)

Screenshots
n/a

Desktop (please complete the following information):

  • OS: Latest Ubuntu
  • Browser: Latest Firefox

Additional context
When using the component the messages get extracted successfully.

@longlho
Copy link
Member

longlho commented Apr 5, 2020

yes we right now do not support scraping formatMessage alias to another function right now.

@mcalthrop
Copy link

mcalthrop commented Apr 27, 2020

This also applies to the formatjs CLI when using the useIntl React hook of course.

Example (contrived) of code for which the formatjs cli does not extract translation id:

import React from 'react';
import { useIntl } from 'react-intl';

const MyComponent = () => {
  const { formatMessage } = useIntl();
  const altText = formatMessage({
    id: 'page.image.altText',
    defaultMessage: 'image text for accessibility',
  });
  return <img src="/path/to/image.png" alt={altText} />;
};

@longlho longlho transferred this issue from formatjs/formatjs-old Apr 27, 2020
@mcalthrop
Copy link

@longlho I don't suppose you have an ETA on this?

I'll have a look at the code when I can, but I'm slammed atm.

@longlho longlho closed this as completed in 3f14e5d May 6, 2020
@cinan
Copy link

cinan commented Jul 8, 2020

I can still reproduce the bug. Example repo. npx formatjs extract index.tsx output:

[
  {
    "id": "a.b"
  }
]

expected:

[
  {
    "id": "a.a"
  },
  {
    "id": "a.b"
  }
]

@longlho
Copy link
Member

longlho commented Jul 8, 2020

You need this flag https://formatjs.io/docs/tooling/cli/#--extract-from-format-message-call

@Haraldson
Copy link

@longlho Has the work you did in 3f14e5d since been reverted or regressed somehow? I have an hours fresh npm install of @formatjs/cli, but no dice. I even tried --extract-from-format-message-call-stateless true after snooping around in that commit’s code, but that’s an unrecognized option. It’s pretty unclear where each bit of code lives and any connections between them – do I perhaps have to update any other @formatjs dependencies to get this working?

@adivfinitione
Copy link

adivfinitione commented Dec 11, 2022

@longlho have an idea why Firefox doesn't recieve the values?.

Code example (copy the code to local, the sandbox have different browser implemented):
https://codesandbox.io/s/old-pond-jzelfy?file=/src/index.js:0-341

image

Browser Info: Firefox v72.0.2 (64-bit)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants