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

String without FormattedMessage #749

Closed
carlosconnected opened this issue Nov 26, 2016 · 9 comments
Closed

String without FormattedMessage #749

carlosconnected opened this issue Nov 26, 2016 · 9 comments
Labels

Comments

@carlosconnected
Copy link

I want to populate a placeholder in a form with a string based on the locale. How can I retrieve the string without using FormattedMessage?

@edelagnier
Copy link

If I understand your question correctly, you can use the formatMessage function of the API : https://github.com/yahoo/react-intl/wiki/API#formatmessage

@andregoncalvesdev
Copy link

Just add:

Component.contextTypes = {
  intl: React.PropTypes.object.isRequired,
};

Then you will have access to this.context.intl object on your component.

Then, simply use:
<h1>{this.context.intl.formatMessage(messages.example)}

@ericf
Copy link
Collaborator

ericf commented Dec 7, 2016

Using context directly works today, but not recommend because it could change in the future. The officially supported way is to wrap your component with the injectIntl() HoC: https://github.com/yahoo/react-intl/wiki/API#injection-api

@alexander-elgin
Copy link

Here is another (a shorter) solution

<FormattedMessage {...messages.placeholderMessage}>
  {
     (placeholder) =>  <input type="text" placeholder = {placeholder} />
  }
</FormattedMessage>

@krzywiecki
Copy link

krzywiecki commented Dec 13, 2018

@ericf you know, I'm using the way you've mentioned at the moment but it kinda sucks that we have to use HoC, then props and so on don't you think? I'm wondering if this would be possible just to import a function from which component is not dependent and use it. Probably testing would be also easier then. What do you think guys?

@arvinsim
Copy link

arvinsim commented Feb 7, 2019

@krzywiecki I agree with you. It's a very roundabout way of doing things and I think this should be accomplished using simpler methods.

@sundhargopal2015
Copy link

Thanks @alexander-elgin

@ucozlab
Copy link

ucozlab commented Jan 30, 2020

@krzywiecki +1. @ericf any comments on that?

@Lillebo
Copy link

Lillebo commented Mar 24, 2020

import { useIntl, FormattedMessage } from 'react-intl';

const MyComponent = () => {
    const { formatMessage } = useIntl();
    return (
        <div>
            <span title={formatMessage({id: 'my.message.key'})} />
            <FormattedMessage id="my.message.key" />
        </div>
    );
}

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

No branches or pull requests

10 participants