Browser language detection

In XbyK is detecting the user's language "standard" .NET functionality we implement now or some other Kentico middleware that XbyK has?  Not finding much documentation on it in XbyK, just KX13 and earlier.  The "Ask AI" feature in the Kentico docs says this:

For automatic language detection, you would typically need to:

  • Detect the user's preferred language (e.g., from the browser'sAccept-Languageheader).
  • Redirect the user to the appropriate language version of your site based on that preference.
  • Ensure your URL routing supports language codes (e.g.,/en/,/es/).


While this logic isn't provided out-of-the-box, you can implement it in your site's startup or middleware logic.


Any direction would be great because what we did in KX13 doesn't seem to be working with XbyK when I expect it should.


Environment

  • Xperience by Kentico version: [31.7.1]

  • .NET version: [10]

  • Execution environment: [SaaS]

Tags:
Localization SaaS v31.7.0

Answers

Using the browser's Accept-Language header is a very good way to do it, but we normally let the language be determined by the URL using XbyK's IPreferredLanguageRetriever. This prevents a redirect from happening when a user may have specifically navigated to language URL that is different than their default.

You can also use .NET's request localizaiton middleware, which uses Accept-Language under the hood if a cookie or query string doesn't override it. This will automatically set CultureInfo.CurrentCulture.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization/select-language-culture

Thanks Mike.

The client is hoping for something that auto detects the language upon first request. For instance if the user is in France and has French as their browsers default language that is what would be served up. We have the default as you mentioned using the IPreferredLanguageRetriever just not sure how to enhance it more.

From my experience with Accepts-Language it's not always as accurate as you would like but it's easy enough to implement. I've always created a controller at the root, which looks at the accepts language header and tries to find a match in the languages assigned to the site. if it finds a match it redirects to /(language-url-code) otherwise you can just redirect to the homepage for the default language. but I do always do a check for not just the language locale but the language code. i.e. if someone is looking for fr-CN you can probably safely send them to the french translation of your site which might be just /fr, but that might actually have the language code of fr-FR so not a precise match.

To response this discussion, you have to login first.