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 NuxtContextthis.$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).
Most helpful comment
context.app.i18n.localewhere you have access to NuxtContextthis.$i18n.localein component$i18n.localein template