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.

How do I translate strings outside of the component?

See original GitHub issue

Thank you for the library. I really love it. I have just one problem. How do I translate a string outside of the React component? For example I modify a Pikaday plugin like this:

class DateInput extends React.Component {
    pikadayInitialOptions = {
        onDraw: (pikaday) => {
            (...)
            let tomorrowBtn = document.createElement('a');
            tomorrowBtn.appendChild(document.createTextNode(translate('pikaday.buttons.tomorrow')));
            (...)
        }
    }

    render() {
        (...)
    }

}

I cannot use Translate component in such case. I need a plain function (translate in this case). How can I achieve something like that with react-localize-redux? Thank you.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
ryandrewjohnsoncommented, Jul 13, 2018

This is a good question, and has pointed out a gap in the docs as this is totally doable:

When you wrap your component with the withLocalize HOC that component gains access to all the props in LocalizeContext including the translate function, which is what you’re looking for.

Using your example:

import { withLocalize } from 'react-localize-redux';

class DateInput extends React.Component {
    pikadayInitialOptions = {
        onDraw: (pikaday) => {
            (...)
            let tomorrowBtn = document.createElement('a');
            tomorrowBtn.appendChild(document.createTextNode(this.props.translate('pikaday.buttons.tomorrow')));
            (...)
        }
    }

    render() {
        (...)
    }

}

export default withLocalize(DateInput);
3reactions
khalidahmedshalabicommented, Jul 26, 2019

"react-localize-redux": "^3.5.2"

import Toast from 'react-native-simple-toast'
import { getTranslate } from 'react-localize-redux';
import { store } from '../App'; // must be exported from your App.js

export const LongToast = (message) => {
	const translate = getTranslate(store.getState().localize)
	Toast.show(translate(message), Toast.LONG)
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Translation outside React Component · Issue #909 - GitHub
I tried to translate strings exported outside React Components. But any methods in docs doesn't work, and I guess its normal, ...
Read more >
Translating strings outside react components using react-18next
Specifically about namespacing and nesting of translations. All of the translation key:value pairs are defined OUTSIDE of the react component.
Read more >
Common i18n patterns in React — LinguiJS documentation
Using jsx macros is the most straightforward way how to translate your React components. <Trans> handles translations of messages including variables and ...
Read more >
A Guide to React Localization with i18next | Phrase
We can now use i18next's t() function to localize our app's name using the translation resources we provided during setup. t() takes a...
Read more >
How to translate your React app with react-i18next
The t() function also accepts a 2nd parameter: A javascript object with parameters that i18next uses to replace parts of the translation strings....
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