nuxt-i18n: Lang Switcher Error "Object is possibly 'undefined'

1.7k views Asked by At

I want to build a language switcher on my nuxt app with nuxt-i18n plugin. Important note is, that I am using TypeScript instead of JavaScript.

As in the documentation (https://i18n.nuxtjs.org/lang-switcher) I implemented the following code into my Navbar component:

TheNavbar.vue

<template>
  <nav class="navbar">
    <div class="container">
      <div>
        <NuxtLink
          v-for="locale in availableLocales"
          :key="locale.code"
          :to="switchLocalePath(locale.code)">{{ locale.name }}
        </NuxtLink>
      </div>
    </div>
  </nav>
</template>

<script lang="ts">
import Vue from 'vue';

export default Vue.extend({
  name: "TheNavbar",
  computed: {
    availableLocales() {
      return this.$i18n.locales.filter(i => i.code !== this.$i18n.locale);
    }
  }
})

In my app this solution works. But the console prints out the two following errors:

 ERROR  ERROR in src/components/shared/TheNavbar.vue:25:14                                                                                                                                                       18:49:05
TS2532: Object is possibly 'undefined'.
    23 |   computed: {
    24 |     async availableLocales() {
  > 25 |       return this.$i18n.locales.filter(i => i?.code !== this.$i18n.locale);
       |              ^^^^^^^^^^^^^^^^^^
    26 |     }
    27 |   }
    28 | })


ERROR in src/components/shared/TheNavbar.vue:25:48
TS2339: Property 'code' does not exist on type 'string | LocaleObject'.
  Property 'code' does not exist on type 'string'.
    23 |   computed: {
    24 |     async availableLocales() {
  > 25 |       return this.$i18n.locales.filter(i => i?.code !== this.$i18n.locale);
       |                                                ^^^^
    26 |     }
    27 |   }
    28 | })

My i18n configuration in nuxt.config.js:

  i18n: {
    locales: [
      {
        code: 'de',
        name: 'Deutsch',
      },
      {
        code: 'en',
        name: 'English'
      }
    ],
    defaultLocale: 'de',
    vueI18n: {
      fallbackLocale: 'de',
      messages: {
        de: {
          welcome: 'Willkommen'
        },
        en: {
          welcome: 'Welcome'
        }
      }
    }
  },
2

There are 2 answers

0
catmal On BEST ANSWER

Try this:

<NuxtLink
   v-if="availableLocales"
   v-for="locale in availableLocales"
   :key="locale.code"
   :to="switchLocalePath(locale.code)">{{ locale.name }}
</NuxtLink>
0
Balázs Czukovács On

To get rid of those errors, add ! post-fix expression operator to this.$i18n.locales, because it won't be null or undefined, and tell typescript that i can be anything:

public get availableLocales(): any {
  return this.$i18n.locales!.filter(i => (i as any).code  !== this.$i18n.locale)
}

Edit: Apr 9 '21

If you are using the current version of Nuxt (v2.15.4), this is the working code:

public get availableLocales(): any {
  return ((this.$i18n.locales as (string | number)[])).filter((i: any) => i.code !== this.$i18n.locale)
}