morteza mortezaie
morteza mortezaie

Reputation: 1564

$i18n Locale get changed when navigating

I use Nuxt $i18n for multi language website , I change the default language and as I navigate between pages , the selected language get change to default language ? this is $i18n config in nuxt config why is that ?

i18n: {
  locales: [
    { code: 'en', iso: 'en-US', dir: 'ltr' },
    { code: 'de', iso: 'de-GER', dir: 'ltr' }
  ],
  defaultLocale: 'de',
  vueI18n: {
    fallbackLocale: 'de',
    messages: {
      en: en.messages,
      de: de.messages
    }
  }
},

Upvotes: 0

Views: 1557

Answers (3)

Ethan Seow
Ethan Seow

Reputation: 11

For people who are dealing with this in nuxt3 with locale cookie, try

<script setup>
const localeRoute = useLocaleRoute()
const { locale } = useI18n()
const linkPath = computed(() => {
  const route = localeRoute('blog', locale.value)
  return route != null ? route.path : '/'
})
</script>

<template>
  <NuxtLink :to="linkPath">{{ $t('blog') }}</NuxtLink>
</template>

found in https://v8.i18n.nuxtjs.org/api/composables

Upvotes: 0

morteza mortezaie
morteza mortezaie

Reputation: 1564

finally i got this is answer :

<nuxt-link :to="localePath('/home')"> home </nuxt-link>

Upvotes: 2

Dzianis Sadouski
Dzianis Sadouski

Reputation: 121

For create route path use

method this.localePath(...), ctx.app.localePath(...)

only this method prepare path with current locale or you can do this himself

Upvotes: 1

Related Questions