DEV Community

d4g0
d4g0

Posted on • Edited on

4 3

How to access localized messages of nuxt-i18n inside the setup function

Quick snippet, log inside a client check if to avoid server logs get forwarded to browser console, it works server side too.
Im mostly writing this to rember it, i allways forget how to doit 😅️.

Tested in @nuxt-i18n 7.X on a nuxt 2 project

setup(props,  ctx ) {

   const { root } = ctx;
   // all the stuff is in the root, who would imagine that ha

   if (process.client) {
      // ctx.root.$i18n.t
      console.log(ctx.root.$i18n.t('my.message.path'))
    }

}
Enter fullscreen mode Exit fullscreen mode

And that's how to use $t is nuxt-i18n inside setup function folks.

Top comments (1)

Collapse
 
silvesterwali profile image
silvesterwali

const { t: $t } = useI18n()

console.log($('message'))

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay