nuxt-modules / i18n

I18n module for Nuxt
https://i18n.nuxtjs.org
MIT License
1.7k stars 474 forks source link

Detect browser language not working with strategy no_prefix with SSG #3066

Closed sivo1981 closed 1 week ago

sivo1981 commented 3 weeks ago

Environment


Reproduction

-

Describe the bug

Detect browser language not working with strategy no_prefix with SSG. When running app in DEV mode on localhost language is detected correctly. When code is generated with nuxt generate and copied to server then browser detection doesn't work anymore.

This worked fine with version 8.2.0.

After some investigation the problem from logs:

detectBrowserLanguage: {ssg: 'ssg_ignore', callType: 'setup', firstAccess: true}
detectLocale: {detectBrowserLanguage: {…}}detectBrowserLanguage: {locale: '', reason: 'detect_ignore_on_ssg'}

Additional context

No response

Logs

No response

github-actions[bot] commented 3 weeks ago

Would you be able to provide a reproduction? 🙏

More info ### Why 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 `needs reproduction` labeled issues don't receive any substantial activity (e.g., new comments featuring a reproduction link), we'll close them. That's not because we don't care! At any point, feel free to comment with a reproduction and we'll reopen it. ### How can I create a reproduction? We have a couple of templates for starting with a minimal reproduction: 👉 [Reproduction starter (v8 and higher)](https://stackblitz.com/fork/github/BobbieGoede/nuxt-i18n-starter/tree/v8) 👉 [Reproduction starter (edge)](https://stackblitz.com/fork/github/BobbieGoede/nuxt-i18n-starter/tree/edge) A public GitHub repository is also perfect. 👌 Please ensure that the reproduction is as **minimal** as possible. See more details [in our guide](https://nuxt.com/docs/community/reporting-bugs/#create-a-minimal-reproduction). You might also find these other articles interesting and/or helpful: - [The Importance of Reproductions](https://antfu.me/posts/why-reproductions-are-required) - [How to Generate a Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve)
sivo1981 commented 3 weeks ago

I can't provide reproduction because of SSG so i'm posting logs from both versions. Detect locale from browser should be sl.

Version: 8.2.0 (working fine)

isSSG true
DKFR7yiG.js:48 useCookie on setup true
DKFR7yiG.js:48 defaultLocale on setup en
DKFR7yiG.js:48 detectLocale: initialLocale - en
DKFR7yiG.js:48 detectLocale: (ssg, callType, firstAccess) -  ssg_ignore setup true
DKFR7yiG.js:48 detectBrowserLanguage: (ssg, callType, firstAccess) -  ssg_ignore setup true
DKFR7yiG.js:48 detectLocale: detectBrowserLanguage (browserLocale, stat, reason, from) -  true detect_ignore_on_ssg undefined
DKFR7yiG.js:48 first detect initial locale en
DKFR7yiG.js:48 final initial locale: en
DKFR7yiG.js:48 locale-changing middleware {fullPath: '/login', hash: '', query: {…}, name: 'login', path: '/login', …} {fullPath: '/', path: '/', query: {…}, hash: '', name: undefined, …}
DKFR7yiG.js:48 detectLocale: initialLocale - en
DKFR7yiG.js:48 detectLocale: (ssg, callType, firstAccess) -  ssg_ignore routing true
DKFR7yiG.js:48 detectBrowserLanguage: (ssg, callType, firstAccess) -  ssg_ignore routing true
DKFR7yiG.js:48 detectLocale: detectBrowserLanguage (browserLocale, stat, reason, from) -  true detect_ignore_on_ssg undefined
DKFR7yiG.js:48 detect locale en
DKFR7yiG.js:48 localeSetup false
DKFR7yiG.js:48 setLocale: new ->  en  old ->  en  initial ->  false
DKFR7yiG.js:48 detectRedirect: targetLocale ->  en
DKFR7yiG.js:48 detectRedirect: route ->  {to: {…}, from: {…}}
DKFR7yiG.js:48 detectRedirect: calledWithRouting ->  true en
DKFR7yiG.js:48 redirectPath on locale-changing middleware 
DKFR7yiG.js:48 navigate options  302  false false false
DKFR7yiG.js:48 navigate isSSG true
DKFR7yiG.js:48 hook app:mounted
DKFR7yiG.js:48 detectBrowserLanguage: (ssg, callType, firstAccess) -  ssg_setup setup true
DKFR7yiG.js:48 detectBrowserLanguage: (path, strategy, alwaysRedirect, redirectOn, locale) - / no_prefix false root en
DKFR7yiG.js:48 detectBrowserLanguage: cookieLocale undefined
DKFR7yiG.js:48 getBrowserLocale (navigator.languages, ret) - (2) ['sl', 'en'] sl
DKFR7yiG.js:48 detectBrowserLanguage: browserLocale sl
DKFR7yiG.js:48 detectBrowserLanguage: (matchedLocale, cookieLocale, localeFrom) - sl undefined navigator_or_header
DKFR7yiG.js:48 detectBrowserLanguage: first finaleLocale (finaleLocale, cookieLocale, localeFrom) - sl undefined navigator_or_header
DKFR7yiG.js:48 detectBrowserLanguage: vueI18nLocale en
DKFR7yiG.js:48 app:mounted: detectBrowserLanguage (browserLocale, stat, reason, from) - sl true undefined navigator_or_header
DKFR7yiG.js:48 setLocale: new ->  sl  old ->  en  initial ->  true
DKFR7yiG.js:48 setLocale: new ->  sl  old ->  sl  initial ->  true
DKFR7yiG.js:48 detectRedirect: targetLocale ->  sl
DKFR7yiG.js:48 detectRedirect: route ->  {to: Proxy(Object)}
DKFR7yiG.js:48 detectRedirect: calledWithRouting ->  false 
DKFR7yiG.js:48 detectRedirect: calculate domain or ssg routePath ->  
DKFR7yiG.js:48 redirectPath on setLocale 
DKFR7yiG.js:48 navigate options  302  false false true
DKFR7yiG.js:48 navigate isSSG true
DKFR7yiG.js:48 detectRedirect: targetLocale ->  sl
DKFR7yiG.js:48 detectRedirect: route ->  {to: Proxy(Object)}
DKFR7yiG.js:48 detectRedirect: calledWithRouting ->  false 
DKFR7yiG.js:48 detectRedirect: calculate domain or ssg routePath ->  
DKFR7yiG.js:48 redirectPath on setLocale 
DKFR7yiG.js:48 navigate options  302  false false true
DKFR7yiG.js:48 navigate isSSG true

Version: 8.5.0 (not working)

isSSG true
DNsLBDbf.js:48 useCookie on setup true
DNsLBDbf.js:48 defaultLocale on setup en
DNsLBDbf.js:48 getLocaleCookie {useCookie: true, cookieKey: 'locale', localeCodes: Array(2)}
DNsLBDbf.js:48 getLocaleCookie (client) - none
DNsLBDbf.js:48 detectLocale: {initialLocale: 'en'}
DNsLBDbf.js:48 detectBrowserLanguage: {ssg: 'ssg_ignore', callType: 'setup', firstAccess: true}
DNsLBDbf.js:48 detectLocale: {detectBrowserLanguage: {…}}
DNsLBDbf.js:48 first detect initial locale en
DNsLBDbf.js:48 final initial locale: en
DNsLBDbf.js:48 locale-changing middleware {fullPath: '/login', hash: '', query: {…}, name: 'login', path: '/login', …} {fullPath: '/', path: '/', query: {…}, hash: '', name: undefined, …}
DNsLBDbf.js:48 getLocaleCookie {useCookie: true, cookieKey: 'locale', localeCodes: Array(2)}
DNsLBDbf.js:48 getLocaleCookie (client) - none
DNsLBDbf.js:48 detectLocale: {initialLocale: 'en'}
DNsLBDbf.js:48 detectBrowserLanguage: {ssg: 'ssg_ignore', callType: 'routing', firstAccess: true}
DNsLBDbf.js:48 detectLocale: {detectBrowserLanguage: {…}}
DNsLBDbf.js:48 detect locale en
DNsLBDbf.js:48 localeSetup false
DNsLBDbf.js:48 setLocale: new ->  en  old ->  en  initial ->  false
DNsLBDbf.js:48 detectRedirect: targetLocale ->  en
DNsLBDbf.js:48 detectRedirect: route ->  {to: {…}, from: {…}}
DNsLBDbf.js:48 detectRedirect: calledWithRouting ->  true en
DNsLBDbf.js:48 redirectPath on locale-changing middleware 
DNsLBDbf.js:48 navigate options  302  false false false
DNsLBDbf.js:48 navigate isSSG true
DNsLBDbf.js:48 hook app:mounted
DNsLBDbf.js:48 getLocaleCookie {useCookie: true, cookieKey: 'locale', localeCodes: Array(2)}
DNsLBDbf.js:48 getLocaleCookie (client) - locale from cookie:  en
DNsLBDbf.js:48 detectBrowserLanguage: {ssg: 'ssg_setup', callType: 'setup', firstAccess: true}
DNsLBDbf.js:48 detectBrowserLanguage: {locale: 'en', path: '/', strategy: 'no_prefix', alwaysRedirect: false, redirectOn: 'root'}
DNsLBDbf.js:48 getBrowserLocale (navigator.languages, ret) - (2) ['sl', 'en'] sl
DNsLBDbf.js:48 detectBrowserLanguage: {locale: 'en', cookieMatch: 'en', browserMatch: 'sl', from: 'cookie'}
DNsLBDbf.js:48 app:mounted: detectBrowserLanguage (locale, reason, from) - (2) ['en', 'cookie']
DNsLBDbf.js:48 setLocale: new ->  en  old ->  en  initial ->  false
DNsLBDbf.js:48 detectRedirect: targetLocale ->  en
DNsLBDbf.js:48 detectRedirect: route ->  {to: Proxy(Object)}
DNsLBDbf.js:48 detectRedirect: calledWithRouting ->  false 
DNsLBDbf.js:48 detectRedirect: calculate domain or ssg routePath ->  
DNsLBDbf.js:48 redirectPath on setLocale 
DNsLBDbf.js:48 navigate options  302  false false true
DNsLBDbf.js:48 navigate isSSG true
pretty-cwj commented 3 weeks ago

我也出现了这种了问题。

Zhang-Wei-666 commented 1 week ago

@BobbieGoede

我这里有一个复现, 项目地址为: https://github.com/Zhang-Wei-666/nuxt-i18n-ssg 复现步骤:

  1. 运行 pnpm preview 启动预览
  2. 切换语言 Clip_2024-09-04_08-54-11
  3. 刷新页面, 语言被重置

也可以在这里预览: https://nuxt-i18n-ssg.vercel.app


I have a reproduction here, the project address is: https://github.com/Zhang-Wei-666/nuxt-i18n-ssg Reproduction steps:

  1. Run pnpm preview to start the preview
  2. Switch language Clip_2024-09-04_08-54-11
  3. Refresh the page, the language is reset

You can also preview it here: https://nuxt-i18n-ssg.vercel.app

BobbieGoede commented 1 week ago

Thanks for the reproduction @Zhang-Wei-666 ! I think I have an idea of what could be causing it and may have a fix for it soon.