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',
    ],
}

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

    bc1qgw9a8hyqqwvcls9ln7vhql4mad0qkveutr2td7

  • ETH

    ETH

    0x3A720717Da03dB6f3c4a4Ff08BC64100205A79d0

2025 © My Dynamic Production SRL All rights Reserved.