panter / vue-i18next

Internationalization for vue using the i18next i18n ecosystem.
https://panter.github.io/vue-i18next/
176 stars 47 forks source link

Async i18next backend leads to missing keys request #46

Closed spotman closed 5 years ago

spotman commented 5 years ago

Hi there!

Thanks for your lib, it's very well done. However, I'm facing with an issue with VueI18next initialization before translation is fetched by i18next async backend. This leads to triggering all used keys as missing in i18next-xnr-backend (coz they are not loaded yet). I've used i18next.init with callback as a temporary fix but I think that this case needs to be covered by your library so VueI18next init will be executed only after i18next successful init.

This is an example of my wrapper for i18n (issue is covered here):

import Vue from 'vue';
import i18next from 'i18next';
import XhrBackend from 'i18next-xhr-backend';
import LangDetector from 'i18next-browser-languagedetector';
import VueI18Next from "@panter/vue-i18next";

export default {
  init(afterInitCallback) {
    i18next
      .use(XhrBackend)
      .use(LangDetector)
      .init({ ...options... }, (err) => {
        if (err) {
          return console.log('Something went wrong with i18n loading', err);
        }

        Vue.use(VueI18Next);

        const i18n = new VueI18Next(i18next);

        afterInitCallback(Vue, i18n);
      });
  }
};

and example of using this wrapper in the client code:

vueI18n.init((Vue, i18n) => {
  new Vue({
    el: '#container',
    store: Store,
    i18n,
    render: h => h(Form)
  });
});
claudiocro commented 5 years ago

Hi,

you should be able to use the directive v-t: https://panter.github.io/vue-i18next/guide/directive.html#v-t .

v-t will wait for the i18next to be initialized. If not initialized it sets the element to hidden = true and wait for i18next to be initialized and then set's hidden=false

If you run into problem, feel free to reopen this issue.

Thanks!