Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the locale without reloading the app. #54

Open
Panoplos opened this issue Apr 6, 2018 · 2 comments
Open

Change the locale without reloading the app. #54

Panoplos opened this issue Apr 6, 2018 · 2 comments

Comments

@Panoplos
Copy link

Panoplos commented Apr 6, 2018

Is there a way to change the locale without forcing the entire app to reload? Setting the locale via the search parameter of the URL is causing the entire app to reload, which is conflicting with an update to the user's profile. It seems like this being a react solution, it should be reactive, but its current implementation imposes a bad experience on users, IMO.

@cwtuan
Copy link
Collaborator

cwtuan commented Apr 10, 2018

You could forceUpdate component after charging language.

@Panoplos
Copy link
Author

Actually, I figured it out a while back. For those who question, this is what I am doing in my redux logic:

/**
 * Logic
 */

export const initialiseLogic = createLogic({
  type: INITIALISE,
  latest: true,

  process({ getState, action }, dispatch, done) {
    const account = getState()['account'].account
    let agentLocale = account ? account.locale : intl.determineLocale({
      urlLocaleKey: "lang",
      cookieLocaleKey: "lang"
    })
    
    if (Object.keys(locales).findIndex( v => v === agentLocale ) === -1) {
      agentLocale = Locales["en-US"]
    }

    dispatch(setLocale(agentLocale))
    done()
  }
})

export const setLocaleLogic = createLogic({
  type: SET_LOCALE,
  latest: true,

  process({ getState, action }, dispatch, done) {
    const account = getState()['account'].account
    account && account.locale !== action.payload.locale && dispatch(updateAccount({locale: action.payload.locale}))
    intl.init({
      currentLocale: action.payload.locale,
      locales
    }).then(() => {
      dispatch(initialised())
      done()
    })
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants