React-Intl How to use FormattedMessage in input placeholder

ReactjsReact IntlReact Starter-Kit

Reactjs Problem Overview


I'm unsure how to get the values from

<FormattedMessage {...messages.placeholderIntlText} />

into a placeholder format like input:

<input placeholder={<FormattedMessage {...messages.placeholderIntlText} />} />

as it would return [Object object] in the actual placeholder. Is there a way to get the actual correct value?

Reactjs Solutions


Solution 1 - Reactjs

The <Formatted... /> React components in react-intl are meant to be used in rendering scenarios and are not meant to be used in placeholders, alternate text, etc. They render HTML, not plain text, which is not useful in your scenario.

Instead, react-intl provides a lower level API for exactly this same reason. The rendering components themselves use this API under the hoods to format the values into HTML. Your scenario probably requires you to use the lower level formatMessage(...) API.

You should inject the intl object into your component by using the injectIntl HOC and then just format the message through the API.

Example:

import React from 'react';
import { injectIntl, intlShape } from 'react-intl';

const ChildComponent = ({ intl }) => {
  const placeholder = intl.formatMessage({id: 'messageId'});
  return(
     <input placeholder={placeholder} />
  );
}

ChildComponent.propTypes = {
  intl: intlShape.isRequired
}

export default injectIntl(ChildComponent);

Please note that I'm using some ES6 features here, so adapt according to your setup.

Solution 2 - Reactjs

  • You can use intl prop from injectIntl HoC
  • You can also provide function as child component:
<FormattedMessage {...messages.placeholderIntlText}>
  {(msg) => (<input placeholder={msg} />)}
</FormattedMessage>

Solution 3 - Reactjs

It's july 2019 and react-intl 3 beta is shipped with a useIntl hook to make these kind of translations easier:

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

const FunctionComponent: React.FC<{date: number | Date}> = ({date}) => {
  const intl = useIntl();
  return (
    <span title={intl.formatDate(date)}>
      <FormattedDate value={date} />
    </span>
  );
};

export default FunctionComponent;

And then you can make custom hooks to use the methods provided by the API:

import { useIntl } from 'react-intl'

export function useFormatMessage(messageId) {
  return useIntl().formatMessage({ id: messageId })
}

Solution 4 - Reactjs

For Input placeholder for more details

   <FormattedMessage id="yourid" defaultMessage="search">
    {placeholder=>  
        <Input placeholder={placeholder}/>
        }
    </FormattedMessage>

Solution 5 - Reactjs

As from React version >= 16.8, you can use useIntl hook :

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

const FunctionComponent = () => {
    const intl = useIntl();
    const lang = "en";
    const messages = {
      en: {
        'placeholderMessageId': 'placeholder in english',
      },
      fr: {
        'placeholderMessageId': 'placeholder en fançais',
      }
    }
    return ( 
      <IntlProvider locale = {lang} messages = { messages[lang] } >
          <input placeholder = { intl.formatMessage({ id: 'placeholderMessageId' })}/> 
      </IntlProvider >
      );
    };

export default FunctionComponent;

Solution 6 - Reactjs

Based on the [react intl wiki][1] the implementation of an input box with translatable placeholder will look like:

import React from 'react';
import { injectIntl, intlShape, defineMessages } from 'react-intl';

const messages = defineMessages({
  placeholder: {
    id: 'myPlaceholderText',
    defaultMessage: '{text} and static text',
  },
});

const ComponentWithInput = ({ intl, placeholderText }) => {
  return (
    <input
      placeholder={ intl.formatMessage(messages.placeholder, { text: placeholderText }) }
    />
  );
};

ComponentWithInput.propTypes = {
  intl: intlShape.isRequired
};

export default injectIntl(ComponentWithInput);

and the useage of it:

import ComponentWithInput from './component-with-input';
...
render() {
  <ComponentWithInput placeholderText="foo" />
}
...

The id: 'myPlaceholderText', part is necessary to enable the [babel-plugin-react-intl][2] to collect the messages for translation.

[1]: https://github.com/yahoo/react-intl/wiki/API#formatmessage "react-intl wiki" [2]: https://github.com/yahoo/babel-plugin-react-intl

Solution 7 - Reactjs

You are trying to render a React component named FormattedMessage into a placeholder tag which is expecting a string.

You should instead just create a function named FormattedMessage that returns a string into the placeholder.

function FormattedMessage(props) {
    ...
}

<input placeholder=`{$(FormattedMessage({...messages.placeholderIntlText})}` />

Solution 8 - Reactjs

Consider this possibility.

The simplest solution

<IntlMessages id="category.name">
    {text => (
        <Input placeholder={text} />
    )}
</IntlMessages>

OR

enter image description here

Solution 9 - Reactjs

In my case I had the whole app in one file, so using export wouldn't work. This one uses the normal class structure so you can use the state and other functionality of React if needed.

class nameInputOrig extends React.Component {
  render () {
    const {formatMessage} = this.props.intl;
    return (
        <input type="text" placeholder={formatMessage({id:"placeholderIntlText"})} />
    );
  }
}

const nameInput = injectIntl(nameInputOrig);

Apply using the created constant:

class App extends React.Component {
    render () {
        <nameInput />
    }
}

Solution 10 - Reactjs

Starting from the @gazdagerg 's answer, I have adapted his code in order to:

  • having a new component that is a wrapper over an input
  • receives an ID of a string from locale conf
  • based on the ID, it returns the string in respect to the global locale setting
  • handling the situation when the string ID is not set (this caused exception and page to crash)

    import React from 'react';
    import { injectIntl, intlShape, defineMessages } from 'react-intl';
    
    
    const InputWithPlaceholder = ({ intl, placeholder }) => {
    
      const messages = defineMessages({
        placeholder: {
          id: placeholder,
          defaultMessage: '',
        },
      });
    
      if(messages.placeholder.id) {
        return (
          <input placeholder={ intl.formatMessage(messages.placeholder) } />
        );
      } else {
        return (
          <input/>
        );
      }
    };
    
    InputWithPlaceholder.propTypes = {
      intl: intlShape.isRequired
    };
    
    export default injectIntl(InputWithPlaceholder);

You can use it in other file by:

  1. import the new component
  2. use it with the ID of the locale string as parameter
import InputWithIntlPlaceholder from 'your/path/to/component/InputWithIntlPlaceholder';

... more code here ...

<InputWithIntlPlaceholder placeholder="your.locale.string.id" />

Solution 11 - Reactjs

I would like to suggest this solution :

import { useIntl } from "react-intl";

export default function MyComponent() {
  const intl = useIntl();

  return (
    <input placeholder={intl.formatMessage({ id: "messageId" })} />
  );
}

Solution 12 - Reactjs

Like this:

import React, {PropTypes} from 'react';
import { injectIntl, FormattedMessage } from 'react-intl';
 
/**
* {
* "hello": "Hello",
* "world": "World"
* }
*/// pure function
const PureFunciton = injectIntl(({ intl }) => {
return (
  <div>
    <p>{intl.formatMessage({ id: 'hello' })}</p>
    <p><FormattedMessage id="world" /></p>
  </div>
)
});
 
// class Component
class componentName extends Component {
  handleStr = () => {
    // return 'Hello';
    const { intl } = this.props;
    return intl.formatMessage({ id: 'hello' })
  }
  render() {
    return (
      <div>
        <p>{this.handleStr()}</p>
        <p><FormattedMessage id="world" /></p>
      </div>
    );
  }
}
 
export default injectIntl(connect(componentName));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBryanView Question on Stackoverflow
Solution 1 - ReactjslsoliveiraView Answer on Stackoverflow
Solution 2 - ReactjslangpavelView Answer on Stackoverflow
Solution 3 - ReactjsFinrodView Answer on Stackoverflow
Solution 4 - ReactjsEL TEGANI MOHAMED HAMAD GABIRView Answer on Stackoverflow
Solution 5 - ReactjsChillerView Answer on Stackoverflow
Solution 6 - ReactjsgazdagergoView Answer on Stackoverflow
Solution 7 - ReactjsLottamusView Answer on Stackoverflow
Solution 8 - ReactjsFarrukh MalikView Answer on Stackoverflow
Solution 9 - ReactjsaksuView Answer on Stackoverflow
Solution 10 - ReactjsVictorView Answer on Stackoverflow
Solution 11 - ReactjsMohamed JellaliView Answer on Stackoverflow
Solution 12 - Reactjsycjcl868View Answer on Stackoverflow