Translate State Value
See original GitHub issueI have a state string I’m translating like this:
this.setState({ errorMessage: t('This email address is already in use.') + t('Please try again.') });
<div>{this.state.errorMessage}</div>
When the page first loads the translation works. If I change the language it’s not translated. Should this work or is it a bug?
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
state value - Spanish translation – Linguee
Many translated example sentences containing "state value" – Spanish-English dictionary and search engine for Spanish translations.
Read more >translate() - CSS: Cascading Style Sheets - MDN Web Docs
The translate() CSS function repositions an element in the horizontal and/or vertical directions. Its result is a data type.
Read more >Translate a field value - Product Documentation | ServiceNow
Field values are the text entries that are used for fields with the type translated_field, such as the Title or Hint field in...
Read more >How to translate your React app with react-i18next
Changing languages · HeaderComponent() · { · const [t, i18n] = useTranslation('common'); · return <div> · <h1>{t('welcome.title', {framework:'React'})}</h1> · <button ...
Read more >how to translate string in a component using react-i18next?
... <Item key={value[this.props.value_prop]} value={value} on_select={this.change}> {t(value[this.props.label_prop])} // i want to translate ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

That’s expected. Changing the language does trigger a rerender on the component tree -> but components do not get recreated -> so the content of state.errorMessage still is a translated string that was set in old language.
So better solution is setting the key to state and using t function inside render:
This way you can even show a generic error if that is not found like: https://www.i18next.com/essentials.html#multiple-fallback-keys
Further you should avoid string concatenation
t('This email address is already in use.') + t('Please try again.')-> this makes translating to other languages harder; Simpler just use the nesting feature of i18next:t('This email address is already in use. ${Please try again.}')-> https://www.i18next.com/nesting.html
@jamuhl It’s working well. Thank you