P
P
postya2020-11-24 05:40:11
Internationalization and localization
postya, 2020-11-24 05:40:11

How to change language in i18n in Nuxt app?

I have an application in Nuxt JS
I want to switch the language of the text using the i18n plugin

How to switch the language correctly?

Here's what I did, but it doesn't work:

Created files in the project for languages:
/lang/en.js
/lang/ru.js

Added config for i18n in nuxt.config.sj file:

['nuxt-i18n', {
      locales: [
        {
          code: 'en',
          file: 'en.js'
        },
        {
          code: 'ru',
          file: 'ru.js'
        },
      ],
      lazy: true,
      langDir: 'lang/',
      defaultLocale: 'en'
    }]


in the files of each language:
export default {
  home: 'Home'
}


used in component:

<NuxtLink to="/" @click.native="closeNav">{{home}}</NuxtLink>

computed: {
      home() {
        return this.$t('home');
      }
    },

methods: {
changeLanguageToRU() {
this.$i18n.locale = 'ru';
}

changeLanguageToENG() {
        this.$i18n.locale = 'en';       
      }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2020-11-24
@yarkov

I did not find in the documentation about the method that changes languages

You weren't looking

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question