I18n-module: Lazy loading translations with @nuxtjs/axios ?

Created on 2 Dec 2020  ·  2Comments  ·  Source: nuxt-community/i18n-module

Hello. 👋

I'm trying to achieve a similar goal as described in issue #931.
I would like to have access to the $axios instance from @nuxtjs/axios while lazy-loading the translations - as it is already configured properly to talk with my API.

I visited https://i18n.nuxtjs.org/lazy-load-translations but this example does not mention compatibility with @nuxtjs/axios.

In the file i do this :

export default async ({ $axios }, locale) => {
  await $axios.$get('/translations', { params: { locale: locale } })
}

Unfortunately it only find the $axios property on client mode.
This does not work during server first render where context.$axios is undefined.

I get the infamous error we all know in my node process terminal :

ERROR  Cannot read property '$get' of undefined

Does someone have any idea how i can get access to the injected $axios instance or is it happening too early ?

I also tried from a different place using the store as an intermediary but got eh same result

export default async ({store}, locale) => {
  //await $axios.$get('/translations', { params: { locale: locale } })
  await store.dispatch('getTranslations', locale);
}

  async getTranslations(ctx, locale) {
    return await this.$axios.$get('/translations', { params: { locale: locale } });
  },



md5-e6dc916202bdedfb9505710e77f98f32



ERROR  Cannot read property '$get' of undefined

Thank you ! Have a good day 🥳

question❓

Most helpful comment

It should work as long as nuxtjs/axios module is registered after the nuxt-i18n module (yes, it's counter-intuitive). If it doesn't then maybe create a repro using the bug report template so that I don't have to.

All 2 comments

It should work as long as nuxtjs/axios module is registered after the nuxt-i18n module (yes, it's counter-intuitive). If it doesn't then maybe create a repro using the bug report template so that I don't have to.

Hello @rchl 👋

You just blow my mind thank you. 🎉 I was really expecting the module order to work the other way around.
Thanks a lot.

Was this page helpful?
0 / 5 - 0 ratings