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'))

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay