question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access global property of i18n instance

See original GitHub issue

Hi,

is it possible to access the global property of the i18n instance for usage outside of components? I couldn’t find a way, so my current workaround is to use vue-i18n without the nuxt module and create my own plugin which exports i18n instance:

export const i18n = createI18n({
    legacy: false,
    globalInjection: true,
    locale: 'en',
    messages: {
        en,
    },
})

export default defineNuxtPlugin((nuxt) => {
    const { vueApp } = nuxt
    vueApp.use(i18n)
})

Best regards David

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
MuhammadM1998commented, Jul 17, 2022

UPDATE: This works in dev and production

<script setup>
  definePageMeta({
    middleware: () => {
      const { vueApp } = useNuxtApp();
      const i18n = vueApp.config.globalProperties.$i18n
    },
  });
</script>

Original Answer

+1. This works in dev but breaks in production

<script setup>
  definePageMeta({
    middleware: () => {
      const { vueApp } = useNuxtApp();
      const i18n = vueApp.__VUE_I18N__.global;
    },
  });
</script>
0reactions
MuhammadM1998commented, Oct 9, 2022

@brunokunace I think the problem is due to SSR too but unfortunately I dont know how to help

Read more comments on GitHub >

github_iconTop Results From Across the Web

General - Vue I18n - Intlify
If the I18n#mode is 'legacy' , then you can access to a global VueI18n instance, else then I18n#mode is 'composition' , you can...
Read more >
Register $t globally · Issue #69 · kazupon/vue-i18n
Please register $t globally per default. Now we need each time: import Vue from 'vue' const $t = Vue.t // before we can...
Read more >
How to use global variables in Vue.js templates?
hello in my template (vue files, which was used as components in router). My components' syntax is like <template> <div id="myid"> {{like $i18n....
Read more >
API references | Vue I18n
If not specified component locale messages, localize with global locale messages. If you specified locale , localize the locale messages of ...
Read more >
The Ultimate Vue Localization Guide
Let's construct the Vue I18n instance in a new module. ... We use i18n.global to access Vue I18n's global, app-wide scope.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found