Nuxt - How to set the default locale based on the logged user

profile picture

Nuxt - How to set the default locale based on the logged user

Let's combine two Nuxt 2 plugins : nuxt-i18n and @nuxtjs/auth

First we need to create a new @nuxtjs/auth plugin. This is fairly easy !

Let's assume that the user's language preference is indicated in the language property of the user object.

// plugins/auth/locale.js

export default ({ app, $auth }) => {
    $auth.onRedirect((to, from) => {
        app.i18n.setLocale($auth.user.language)
    })
}

Next, register the plugin in

// nuxt.config.js

auth: {
    plugins: [
        '~/plugins/auth/locale',
    ],
}
nuxt
i18n
auth
default locale

about me

profile picture

I consider myself as an IT Business Artisan. Or Consultant CTO. I'm a self-taught Web Developper, coach and teacher. My main work is helping and guiding digital startups.

more about me

follow me

newsletter

A weekly email with the latests articles

support my work

Start trading on binance
  • BTC

    BTC

    18SY81ejLGFuJ9KMWQu5zPrDGuR5rDiauM

  • ETH

    ETH

    0x519e0eaa9bc83018bb306880548b79fc0794cd08

  • Monero

    XMR

    895bSneY4eoZjsr2hN2CAALkUrMExHEV5Pbg8TJb6ejnMLN7js1gLAXQySqbSbfzjWHQpQhQpvFtojbkdZQZmM9qCFz7BXU

2024 © My Dynamic Production SRL All rights Reserved.