nopSolutions / nopCommerce

ASP.NET Core eCommerce software. nopCommerce is a free and open-source shopping cart.
https://www.nopcommerce.com
Other
9.14k stars 5.25k forks source link

GetLanguageFromRequestAsync and Accept-Language header mismatch #7127

Open brakim opened 4 months ago

brakim commented 4 months ago

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 commented 4 months ago

Examples of Accept-Language Header Values

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

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

brakim commented 4 months ago

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.