0

I've a multilang Nuxt App using nuxt-i18n with ignored routes.

nuxt.config.js

…
seo: true,
parsePages: false,
strategy: 'prefix_except_default',
pages: {
  'cats': {
    en: '/cats',
    de: '/katzen',
    fr: false
  }
}
…

So the page is not available in french.

My lang switch looks like this – pretty simple so far:

LanguageSwitch.vue

computed: {
  availableLocales () {
    return this.$i18n.locales.filter(i => i.code !== this.$i18n.locale)
  }
}
<ul class="language-switch__list">
  <li class="language-switch__item" v-for="locale in availableLocales">
    <NuxtLink
     class="language-switch__link"
     rel="alternate" :key="locale.code"
     :to="switchLocalePath(locale.code)"
     :hreflang="locale.code" :lang="locale.code"
     active-class="none"
     exact>
     {{locale.name}}
    </NuxtLink>
  </li>
</ul>

I've changed the filter to remove missing pages/languages like that:

return this.$i18n.locales.filter(i => (i.code !== this.$i18n.locale) && (this.$nuxt.$route.path !== this.switchLocalePath(i.code)) )

That works, but I'd like a better solution. Here is my question: Is there an easy way to change the routes to the language homepage (/,/en,/de) if the route is ignored?

Bitpunk
  • 198
  • 2
  • 11

1 Answers1

0

I have solved the problem by simply wrapping an additional function around it:

<nav id="language-switch" v-if="isOpen" class="language-switch__nav arrow arrow--top">
  <ul class="language-switch__list">
    <li class="language-switch__item" v-for="locale in availableLocales">
      <NuxtLink class="language-switch__link" rel="alternate" :key="locale.code" :to="switchLocalePathFallback(locale.code)" :hreflang="locale.code" :lang="locale.code" active-class="none" exact>{{locale.name}}</NuxtLink>
    </li>
  </ul>
</nav>
…
computed: {
  availableLocales () {
    return this.$i18n.locales.filter(i => (i.code !== this.$i18n.locale) )
  }
},
methods: {
  switchLocalePathFallback(code) {
    let langPath = this.switchLocalePath(code),
        path = langPath
    if(langPath == this.$nuxt.$route.path ) {
      path = this.$i18n.defaultLocale != code ? '/'+code : '/'
    }
    return path
  }
}
…

Not very flexible but it works for me.

Bitpunk
  • 198
  • 2
  • 11