Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Use of i18nInstance in useI18n #22

Open
duebbert opened this issue Sep 12, 2022 · 1 comment
Open

Use of i18nInstance in useI18n #22

duebbert opened this issue Sep 12, 2022 · 1 comment

Comments

@duebbert
Copy link

I ran into issues when using createI18n and then useI18n (most likely something due to my vue-cli webpack setup). But it made me wonder why you are using the i18nInstance from createI18n?

I slightly amended the useI18n code to use the $i18n instance that is on the current Vue instance instead:

function useI18n() {
  const instance = getCurrentInstance();
  const vm = (instance == null ? void 0 : instance.proxy) || instance || new Vue({});

  const i18n = vm.$i18n;
  if (!i18n)
    throw new Error("vue-i18n not initialized");

  const locale = computed({
    get() {
      return i18n.locale;
    },
    set(v) {
      i18n.locale = v;
    }
  });
  return {
    locale,
    t: vm.$t.bind(vm),
    tc: vm.$tc.bind(vm),
    d: vm.$d.bind(vm),
    te: vm.$te.bind(vm),
    n: vm.$n.bind(vm)
  };
}

This way vue-i18n can be instantiated as usual and createI18n isn't needed (but could still be used). In the component you just use useI18n.

I might be missing something but wouldn't this make the code much easier to use?

@gavmck
Copy link

gavmck commented Nov 4, 2022

Bet this would fix the complications I'm having sharing i18n between vue cli and nuxt builds.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants