Skip to content
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

GetLanguageFromRequestAsync and Accept-Language header mismatch #7127

Open
brakim opened this issue Apr 3, 2024 · 2 comments
Open

GetLanguageFromRequestAsync and Accept-Language header mismatch #7127

brakim opened this issue Apr 3, 2024 · 2 comments

Comments

@brakim
Copy link

brakim commented Apr 3, 2024

Today, I discovered a somewhat odd and peculiar problem with AutomaticallyDetectLanguage and GetLanguageFromRequestAsync.

Currently, NopCommerce uses UseNopRequestLocalization to add all languages to SupportedCultures and SupportedUICultures.

It then creates new CultureInfo with the LanguageCulture (e.g., en-GB) as the key.

This works fine until we come to Sweden, where both Chrome and Edge send sv as the Accept-Language header and not sv-SE as NopCommerce has registered, leading to it defaulting to English.

I don't know if this is an exception for Sweden or if the same thing applies to, for example, Norway, Denmark, etc.

Changing the browser's language to English causes it to send both en and en-US as Accept-Language.

I tried changing all instances where we use LanguageCulture to determine language to UniqueSeoCode, and then everything started to work.

I don't know if this is due to some change in the browser, my computer, .net but I just wanted to share my findings.

@brakim
Copy link
Author

brakim commented Apr 3, 2024

Examples of Accept-Language Header Values

  • A single language, English: Accept-Language: en
  • English preferred, but French is acceptable: Accept-Language: en, fr
  • English preferred, French as a second choice, and any other language as a less preferred option: Accept-Language: en;q=1, fr;q=0.9, *;q=0.8
  • English as used in the United States specifically, with British English as a second choice: Accept-Language: en-US, en-GB
  • Preference for Spanish from any region, but with a higher preference for Spanish specifically from Spain: Accept-Language: es-ES;q=1, es;q=0.9

I haven't found any real evidence on how the browser specifies when to use

  • Neutral culture: A culture that has a specified language, but not a region (for example "en", "es").
  • Specific culture: A culture that has a specified language and region (for example, "en-US", "en-GB", "es-CL").

But my findings seems to suggest when the browser provides a Specific culture, it also provides the neutral as a fallback.

@brakim
Copy link
Author

brakim commented Apr 3, 2024

In the admin area when you edit a Language, inside the view NopCommerce have this piece of code
var cultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.SpecificCultures)

maybe it should be

var cultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.NeutralCultures)

i have never encountered the need to have two languages for example English (en-GB) and English (en-US).
I don't say there isn't cases when this feature is needed, but it seemes like edge cases to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants