I18n-module: How i can detect current language?

Created on 25 Jul 2019  路  7Comments  路  Source: nuxt-community/i18n-module

Hi, how can i detect current language. At previously, i got it like that: store.state.i18n.locale.
But now i get undefined.
Please, help me)

This question is available on Nuxt community (#c269)

Most helpful comment

  • context.app.i18n.locale where you have access to NuxtContext
  • this.$i18n.locale in component
  • $i18n.locale in template

All 7 comments

This issue as been imported as question since it does not respect nuxt-i18n issue template. Only bug reports and feature requests stays open to reduce maintainers workload.
If your issue is not a question, please mention the repo admin or moderator to change its type and it will be re-opened automatically.
Your question is available at https://cmty.app/nuxt/nuxt-i18n/issues/c269.

  • context.app.i18n.locale where you have access to NuxtContext
  • this.$i18n.locale in component
  • $i18n.locale in template

@rchl but how can i get locale from asyncData:

async asyncData({ store }) {
    const currentProduct = await axios.get(process.env.BASE_API + store.state.i18n.locale + '/v1/products')
    return {
      products: currentProduct.data
    }
  }

asyncData is passed the NuxtContext so my first suggestion applies here

@rchl Thanks, that works for me)

If set to detectBrowserLanguage.alwaysRedirect I cannot get the current locale:

    @Component({
        async asyncData(ctx: Context): Promise<any> {
            console.log(ctx.app.i18n.locale); // prev locale
        },
    })

Only works if setTimeout is used:

    @Component({
        async asyncData(ctx: Context): Promise<any> {
            return new Promise(((resolve) => {
                setTimeout(() => {
                    console.log(ctx.app.i18n.locale); // current locale

                    resolve();
                }, 10);
            }));
        },
    })

How can I get the current locale without setTimeout?

Please create new issue and try to include reproduction steps and/or repo (can use https://codesandbox.io).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

johnboylesingfield picture johnboylesingfield  路  17Comments

vodnicearv picture vodnicearv  路  20Comments

pi0 picture pi0  路  18Comments

fanwenfu picture fanwenfu  路  15Comments

adetbekov picture adetbekov  路  21Comments