I’m starting a new website with Nuxt and I’m currently setting it to be multilingual (french and english)
I followed this tutorial to setup the translation and the language switcher, and got the following:
nuxt.config.js (relevant part)
['nuxt-i18n', {
detectBrowserLanguage: {
useCookie: true,
alwaysRedirect: true
},
strategy: 'prefix_except_default',
defaultLocale: 'fr',
parsePages: false,
seo: true,
pages: {
about: {
en: '/about-us',
fr: '/a-propos'
},
portfolio: {
en: '/projects',
fr: '/portfolio'
}
},
locales: [
{
code: 'en',
iso: 'en-US',
name: 'English',
file: 'en-US.js'
},
{
code: 'fr',
iso: 'fr-FR',
name: 'Français',
file: 'fr-FR.js'
}
],
lazy: true,
langDir: 'lang/'
}]
navbar.vue
<nav class="header">
<div class="logo">
<nuxt-link :to="localePath('index')">
<img src="https://bulma.io/images/bulma-logo.png" alt="Bulma: a modern CSS framework based on Flexbox" width="112" height="28">
</nuxt-link>
</div>
<div class="links">
<nuxt-link :to="localePath('about')">
{{ $t('navbar.about') }}
</nuxt-link>
<nuxt-link :to="localePath('blog')">
Blog
</nuxt-link>
<nuxt-link :to="localePath('portfolio')">
Portfolio
</nuxt-link>
<nuxt-link :to="localePath('contact')">
Contact
</nuxt-link>
<span>|</span>
<nuxt-link v-if="$i18n.locale === 'fr'" :to="switchLocalePath('en')">
English
</nuxt-link>
<nuxt-link v-else :to="switchLocalePath('fr')">
Français
</nuxt-link>
{{ $i18n.locale }}
</div>
</nav>
Here is my directory structure (if that can help)
layouts/
front.vue
navbar.vue
pages/
index.vue
about.vue
blog.vue
portfolio.vue
contact.vue
The navbar.vue
file is called inside front.vue
, which is my layout.
The problems are the following:
- On any page, when I try to click the languageSwitcher link, I get redirected to its english version (ie:
/a-propos
becomes/en/about-us
), however the other links will bring me back to the french version. {{ $i18n.locale }}
keeps displayingfr
- I tried the following block of code:
<template>
...
<nuxt-link
v-for="locale in availableLocales"
key="locale.code"
:to="switchLocalePath(locale.code)"
>
{{ locale.name }}
</nuxt-link>
...
</template
<script>
export default {
computed: {
availableLocales () {
return this.$i18n.locales.filter(i => i.code !== this.$i18n.locale)
}
}
}
</script>
And it only displays english, while it should display me both english and french.
What have I done wrong, or what am I missing?
2
Answers
this is due to
alwaysRedirect: true
, set it tofalse
This worked for me, using vue-select and availableLocales of Nuxt-$i18n, hope it helps: