-
Notifications
You must be signed in to change notification settings - Fork 462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not able to set a locale or redirect to specific locale from the server in a middleware or plugin. #2554
Comments
Would you be able to provide a reproduction? 🙏 More infoWhy do I need to provide a reproduction?Reproductions make it possible for us to triage and fix issues quickly with a relatively small team. It helps us discover the source of the problem, and also can reveal assumptions you or we might be making. What will happen?If you've provided a reproduction, we'll remove the label and try to reproduce the issue. If we can, we'll mark it as a bug and prioritise it based on its severity and how many people we think it might affect. If How can I create a reproduction?We have a couple of templates for starting with a minimal reproduction: 👉 Reproduction starter (v8 and higher) A public GitHub repository is also perfect. 👌 Please ensure that the reproduction is as minimal as possible. See more details in our guide. You might also find these other articles interesting and/or helpful: |
I am unable to proceed with testing my actual bug due to the apparent malfunction of the basic language switcher. Please refer to my attempted reproduction steps. https://stackblitz.com/edit/github-m2j2vu?file=nuxt.config.ts |
Same happens for me when calling setLocale in middleware like this: export default defineNuxtRouteMiddleware(async (to) => {
const {$i18n} = useNuxtApp();
const userLocaleFromBackend = 'en';
await $i18n.setLocale(userLocaleFromBackend);
}); Using Nuxt 3.9.0. 8.0.0-rc.7 ✅ (Works fine for me) |
setting |
I took some time to investigate the issue. The problem is that |
It's still unclear what the expected behaviour is, and without a minimal reproduction I can only guess what's going on. If you're trying to change the locale in a route middleware, you're best off using a redirect from the middleware using |
https://stackblitz.com/edit/nuxt-starter-i64ji9?file=plugins%2Fi18n.ts reproduction from my use-case. I need to make a custom locale detection. I expect that calling the setLocale method from the plugin will change the language, not throw an error. |
@BobbieGoede I checked my project with edge version, everything works fine. Thank you for your work! |
@ucw that's great to hear! I just published v8.1.1 which includes these changes. I'll close this issue now, if anyone else is still experiencing this or a similar problem, please open a new issue with a (minimal) reproduction. |
Environment
Reproduction
Install Nuxt i18n, try to call setLocale in Nuxt plugin our middleware.
Describe the bug
It seems like I cannot set a locale or redirect to specific locale from the server in a middleware or plugin.
$i18n.switchLocalePath()
$i18n.setLocale( localeCookie.value )
Nitro error:
[nitro] [unhandledRejection] Error: [nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin.
Tested in both middleware and plugin.
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: