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.

Can't setActiveLanguage

See original GitHub issue

I have the locale reducer set :

import { localeReducer as locale } from 'react-localize-redux';
combineReducers({ locale, otherReducers});

The languages initialized:

const languageToUse = 'en';
const languages = ['en', 'fr'];
store.dispatch(initialize(languages));
const json = require('../static/texts/global.locale.json');
store.dispatch(addTranslation(json));
store.dispatch(setActiveLanguage(languageToUse));

And the function mapped to the props:

import { getTranslate, getActiveLanguage, setActiveLanguage } from 'react-localize-redux';

function mapStateToProps(state) {
    return {
      restOfState: state.restOfState,
      translate: getTranslate(state.locale),
      currentLanguage: getActiveLanguage(state.locale).code,
      setActiveLanguage: setActiveLanguage,
    };
  }

  export default mapStateToProps;

However calling this.props.setActiveLanguage('fr') in a connected component does not change anything (logging this.props.currentLanguage ). Is this an issue or am I missing something?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ryandrewjohnsoncommented, Nov 9, 2017

@Vict0rSch sorry missed that in original source you provided. The action creators need to be added to connect like you would any other dispatch props.

So all Action Creators would go in dispatchProps, while all Selectors would go in stateProps.

const mapStateToProps = state => ({
    translate: getTranslate(state)
});

// if using shorthand syntax for dispatchProps
const mapDispatchToProps = {
     setActiveLanguage
};

// or long form
const mapDispatchToProps = dispatch => {
  return bindActionCreators({setActiveLanguage}, dispatch);
};

const ConnectedComponent = connect(mapStateToProps, mapDispatchToProps)(MyComponent)

Then in MyComponent you’d have access to this.props.setActiveLanguage.

0reactions
vict0rschcommented, Nov 9, 2017

Ok that wokrs. Thanks a lot for your help 😃 Very useful and well done package btw!

Read more comments on GitHub >

github_iconTop Results From Across the Web

getting-started – React Localize Redux Documentation
Use the setActiveLanguage method to change localize's active language. ... i.e. You cannot pass a React component to a string like "<strong>Hello</strong> ...
Read more >
Keep getting the error MissingTranslationId - Stack Overflow
... react-localize-redux but I cannot create a tag for it yet. ... setActiveLanguage("es"); // Although Spanish was added first in the list ...
Read more >
Add an API to set active language [#2849464] | Drupal.org
The root of the issue is that Title module can't properly determine the active language, since it has no clue about routes (e.g....
Read more >
How to use setActiveLanguage function in react-localize-redux
changeLang(code) { this.props.dispatch(setActiveLanguage((languages.indexOf(code) !== -1) ? code : 'ru'));
Read more >
Factory method does not create objects properly| JBoss.org Content ...
getIdentifier()); setActiveLanguage(xl); log.info("initActiveLanguage(1): ... It works for me now, but I can't really recall how I solved the problem ...
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