Currently, I am working on a frontend interface in VueJS and integrating i18n with Vuejs 3. While the normal implementation of i18n works fine, I encountered issues when trying to use it with the composition API.
In my main.js file, I have set up i18n as follows:
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 referred to the documentation which mentioned adding legacy:false for using the composition API. However, after making this change, my $t function stopped working. Further exploration led me to this solution:
const app = Vue.createApp({
setup() {
const { t } = VueI18n.useI18n() // call `useI18n`, and spread `t` from `useI18n` returning
return { t } // return render context that includes `t`
}
})
The issue arises because my createApp is already initialized as:
const app = createApp(App)
This is the default implementation by VueJS. I attempted various modifications such as inserting setup before or after App, but nothing seemed to work. It appears I may be complicating things further.
Do you have any ideas on how to effectively integrate i18n with the composition API? My ultimate goal is to utilize $i18n within a component switchLanguage created using the composition API for language management and access to information.
I appreciate any assistance you can offer. Thank you.