Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

78
Views
vuejs3 I18n and composition API

I'm right now doing a frontend interface in vueJS, and i'm currently working with vuejs 3 and i18n. The implementation of i18n works quite fine the normal way but when I want to use it with the composition API starts the problems.

So what I did. My main.js looks like this:

const i18n = createI18n({
    messages: {
        en: en,
        fr: fr
    },
    fallbackLocale: 'en',
    locale: localStorage.Language || navigator.language.split('-')[0] || 'en',
})
const app = createApp(App)


app.use(router)
app.use(i18n)

app.mount('#app')

I saw in the documentation to use composition API I have to add legacy:false so I did it. Then my $t doesn't work anymore. I go firther in the documentation and arrive at the point I'm lost with. The documentation saiys to use this:

const app = Vue.createApp({
  setup() {
    const { t } = VueI18n.useI18n() // call `useI18n`, and spread `t` from  `useI18n` returning
    return { t } // return render context that included `t`
  }
})

My problem is that my createApp is already used like this:

const app = createApp(App)

which is the default implementation by Vuejs. I tried to modify it adding the setup after App, before, removing App nothing works and I think I do more and more stupid things.

Does anyone has an idea how to make i18n works with the composition API? The final objective is basically in a component switchLanguage made with composition API to have access to $i18n (to get some informations and manage my language switch)

Thanks in advance for the help you can provide.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You already instantiated i18n on your app, in main.js. That's the important bit.

The example presented in docs doesn't necessarily have to be done on the instance defined inside createApp. It works in any component, as long as you have instantiated i18n on main.(js|ts)

This will work in any component (wherever you need t):

import { useI18n } from "vue-i18n";

export default defineComponent({
  setup() {
    const { t } = useI18n();
    // you can now use t('some.text.to.be.translated')
    // t('some.text.to.be.pluralized', { n: 1 }, 1);

    return {
      // expose `t` to <template>:
      t, 
    }
  },
  // if you want it inside any method, computed or hook
  // in case you still use Options API
  computed() {
    someTranslatedText() {
      return useI18n().t('translate.me');
    }
  },
  methods: {
    methodNeedingTranslation() {
      const { t } = useI18n();
      // do stuff with `t`
    }
  }
})

Side note: All $tc (pluralization) functionality has been moved to t.

If you're upgrading an existing app and you don't want to go through templates and replace all instances of $t and $tc with t:

setup: () => ({ 
  $t: useI18n().t
  $tc: useI18n().t 
})

 
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!