#Nuxt i18n not keeping language
4 messages · Page 1 of 1 (latest)
Based on the information provided in the knowledge sources, it seems that you're experiencing an issue with Nuxt 3 and i18n language detection. This behavior is not intended, and there are a few things you can check to resolve the issue:
-
Configuration Location: Make sure your i18n configuration is in the correct file. As mentioned in this Stack Overflow post (https://stackoverflow.com/questions/77430938), the configuration should be in the
nuxt.config.tsfile, not ini18n.config.ts. -
Configuration Structure: Ensure your configuration in
nuxt.config.tsincludes the necessary settings. Here's an example:
export default defineNuxtConfig({
modules: ['@nuxtjs/i18n'],
i18n: {
locales: [
{ code: 'en', iso: 'en-US' },
{ code: 'fr', iso: 'fr-FR' }
],
defaultLocale: 'en',
detectBrowserLanguage: {
useCookie: true,
cookieKey: 'i18n_redirected',
redirectOn: 'root'
}
}
})
-
Browser Detection: The
detectBrowserLanguageoption should be set to ensure the browser language is detected and maintained across page navigation. -
Strategy: If you want the language to be included in the URL path, you can set the
strategyoption. For example:
strategy: 'prefix_except_default'
This will add the language code to the URL for non-default languages.
- Persistence: To maintain the selected language across page navigation, make sure you're using the appropriate methods to change the language, such as
setLocale()from theuseI18n()composable.
If you've checked these points and are still experiencing issues, it might be helpful to review your entire Nuxt and i18n setup, or consider creating a minimal reproduction of the problem to get more specific help.
Stack Overflow - Nuxt 3 i18n browser detection not working (https://stackoverflow.com/questions/77430938)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@twin whale Kapa.ai is still learning and improving, please let me know how I did by reacting below.