question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Translate State Value

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jamuhlcommented, Sep 6, 2017

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:

//...
<div className="error">{t(this.state.errorMessageKey)}</div>
//...

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

0reactions
ghostcommented, Apr 26, 2022

@jamuhl It’s working well. Thank you

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found