nuxt-modules / i18n

I18n module for Nuxt
https://i18n.nuxtjs.org
MIT License
1.76k stars 485 forks source link

lazy:true does not work with nuxt #2381

Closed gileneusz closed 1 year ago

gileneusz commented 1 year ago

Environment


Reproduction

nuxt.config.js

  modules: [ '@nuxtjs/i18n'],
  i18n: {
    locales: [
      {
        code: 'en',
        file: 'en.json',
        iso: 'en-US'  
      },
      {
        code: 'pl',
        file: 'pl.json',
        iso: 'pl-PL'  
      }
    ],

    defaultLocale: 'en',
    lazy: true,
    langDir: 'locales/',
    vueI18n: {
      fallbackLocale: 'en'
    }

locales/en.json:

{
  "welcome": "Welcome"
}

locales/pl.json:

{
    "welcome": "Witaj"
}

index.vue:

<template>
<div>
<div>
{{ $t('welcome') }}
</div>
<div>
<button @click="changeLanguage('en')">English</button>
<button @click="changeLanguage('pl')">Polski</button>
</div>
</div>          
  </template>
<script>
  methods: {
    changeLanguage(lang) {
      this.$i18n.locale = lang;
    }}
</script>

Describe the bug

with lazy:false, it's working correctly, with lazy:true, I've got this error inside console:

 [vue-i18n] Fall back to translate the keypath 'welcome' with 'en' locale.

Additional context

No response

Logs

No response

BobbieGoede commented 1 year ago

You should change your language switcher so that it updates the locale cookie and triggers navigation if necessary, like so:

changeLanguage(lang) {
  this.$i18n.locale = lang;
}}

Check out the v7 docs for more information.