N
Nuxt10mo ago
lkjimy

i18n inside plugin

How to access the I18n instance inside a plugin? I have a plugin that fetches global data for my app from a backend, and I need to use the locale. I cant import it from the composable... error caught during app initialization SyntaxError: Must be called at the top of a setup function ... Even though the plugin is inside a setup function...
export default defineNuxtPlugin({
name: 'fetch-global-information',
enforce: 'pre',
async setup() {
const { locale } = useI18n()
// ...
}
})
export default defineNuxtPlugin({
name: 'fetch-global-information',
enforce: 'pre',
async setup() {
const { locale } = useI18n()
// ...
}
})
I can't use use it from nuxtApp, because I18n is not there yet...
export default defineNuxtPlugin({
name: 'fetch-global-information',
enforce: 'pre',
async setup(nuxtApp) {
const { locale } = nuxtApp.$i18n
// ...
}
})
export default defineNuxtPlugin({
name: 'fetch-global-information',
enforce: 'pre',
async setup(nuxtApp) {
const { locale } = nuxtApp.$i18n
// ...
}
})
(This solution is inconsistent in development, but fails 100% in production.) How do I make sure i18n is loaded before my plugin?
2 Replies
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
lkjimy
lkjimyOP10mo ago
Thanks for the reply! I had to change enforce to 'post', and that was enough to fix it
Want results from more Discord servers?
Add your server