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.

Prevent reload on switchLocalePath

See original GitHub issue

What problem does this feature solve?

When you change the locale the page is reloaded. If you switch the language when typing a form, or editing something, all your changes are lost (one could prevent that with the provided callbacks, but it’s quite a lot of work to do it for all the cases in your app

What does the proposed changes look like?

It’s easy to simply change the locale with

 this.$i18n.locale = lang

But it doesn’t change the current path.

I would be nice if one could pass a second parameter to switchLocalePath to prevent the reload the page, while still modifying the path and changing the locale

<div align="right">This feature request is available on Nuxt.js community (#c60)</div>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:12

github_iconTop GitHub Comments

11reactions
lukaVargacommented, Mar 22, 2019

The this.$router.push solution still reloads the page. Here’s how I solved this issue:

  switchLang(locale: TAvailableLocales): void {
    if (locale === this.$store.state.i18n.locale) {
      return;
    }

    // update store info
    this.$store.commit('i18n/i18nSetLocale', locale);

    // fetch new locale file
    import(`../lang/${locale}`).then(module => {
      // update i18n's locale instance
      this.$i18n.locale = locale;
      // set new messages from new locale file
      this.$i18n.setLocaleMessage(locale, module.default);
      // update url to point to new path, without reloading the page
      window.history.replaceState('', '', this.switchLocalePath(locale));
    });
  }
1reaction
Xzandrocommented, Jun 28, 2021

I also needed a way to switch the locale without refreshing the whole page. After attempting the solution from this issue, I noticed that the active states of menus are basically gone. Is this a negative side effect @rchl talked about? Would there be any solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent reload on switchLocalePath · Issue #68 · nuxt ... - GitHub
switchLocalePath returns only the route name (String), based on the given locale code. If you want to change the locale on your javascript...
Read more >
Language change without full page reload - Vue Storefront Docs
Language change without full page reload. By default, language changes cause a full page reload. To not trigger a full reload, you can...
Read more >
Language Switcher doesn't apply the new locale
I was looking for this anwer, but I have a problem, his.$router.replace(this.switchLocalePath(language)); only changes URL but does not refresh ...
Read more >
Lang Switcher - i18n-module
For this purpose, @nuxtjs/i18n's store module exposes a routeParams state property that will be merged with route params when generating lang switch routes...
Read more >
How to prevent browser refresh, URL changes, or route ...
How to prevent browser refresh, URL changes, or route navigation in Vue · Prevent URL change and/or page reload. · Prevent router navigation....
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