0

I'm following the documentation on how to change the language (https://nuxt-community.github.io/nuxt-i18n/lang-switcher.html), which works just fine. But whenever I set detectBrowserLanguage.useCookie and detectBrowserLanguage.alwaysRedirect in my nuxt.config.js to true I should call the this.$i18n.setLocaleCookie(locale) method to persist the change (as the documentation tells).

My only question is, where should I call this method?

My current code:

<nuxt-link
  class="px-6 py-2 block"
  :click="this.$i18n.setLocaleCookie(locale)"
  v-for="locale in availableLocales"
  :key="locale.code"
  :to="switchLocalePath(locale.code)">{{ locale.code.toUpperCase() }}

export default {
  computed: {
    availableLocales () {
      return this.$i18n.locales.filter(i => i.code !== this.$i18n.locale)
    }
  }
}

As you can see i tried running this.$i18n.setLocaleCookie(locale) on the click event of the link but i'm getting Cannot read property '$i18n' of undefined

royketelaar
  • 1,389
  • 2
  • 11
  • 21

1 Answers1

-1

Try call it in plugin

export default async function ({app, store}) {
  // on change locale
  app.i18n.beforeLanguageSwitch = (oldLocale, newLocale) => {
    ...
  }
}

nuxt.config.js

  plugins: [
    '~/plugins/i18n.js'
  ],
Vladimir Golub
  • 523
  • 1
  • 8
  • 22