From 6ba09afc2aab8a4e4580b67364b8d1f53c9e8573 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Fri, 24 Dec 2021 02:44:24 +0300 Subject: [PATCH] Add a section for order request culture providers (#24404) * Add a section for order request culture providers * Apply suggestions from code review * Update localization.md Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> --- aspnetcore/fundamentals/localization.md | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/aspnetcore/fundamentals/localization.md b/aspnetcore/fundamentals/localization.md index b32877f34e..95ca418d37 100644 --- a/aspnetcore/fundamentals/localization.md +++ b/aspnetcore/fundamentals/localization.md @@ -359,6 +359,23 @@ services.Configure(options => Use `RequestLocalizationOptions` to add or remove localization providers. +### Change request culture providers order + + has three default request culture providers: , , and . Use [`RequestLocalizationOptions.RequestCultureProviders`]](xref:Microsoft.AspNetCore.Builder.RequestLocalizationOptions.RequestCultureProviders) property to change the order of these providers as shown in the following below: + +```csharp + app.UseRequestLocalization(options => + { + var questStringCultureProvider = options.RequestCultureProviders[0]; + options.RequestCultureProviders.RemoveAt(0); + options.RequestCultureProviders.Insert(1, questStringCultureProvider); + }); +``` + +In the preceding example above, the order of `QueryStringRequestCultureProvider` and `CookieRequestCultureProvider` is swithched, so the `RequestLocalizationMiddleware` looks for the cultures from the cookies first, then query string. + +As previously mentioned, add a custom provider via which sets the order to `0`, so this provider takes the precedence over the others. + ### Set the culture programmatically This sample **Localization.StarterWeb** project on [GitHub](https://github.com/aspnet/entropy) contains UI to set the `Culture`. The *Views/Shared/_SelectLanguagePartial.cshtml* file allows you to select the culture from the list of supported cultures: @@ -713,6 +730,23 @@ services.Configure(options => Use `RequestLocalizationOptions` to add or remove localization providers. +### Change request culture providers order + + has three default request culture providers: , , and . Use [`RequestLocalizationOptions.RequestCultureProviders`]](xref:Microsoft.AspNetCore.Builder.RequestLocalizationOptions.RequestCultureProviders) property to change the order of these providers as shown in the following below: + +```csharp + app.UseRequestLocalization(options => + { + var questStringCultureProvider = options.RequestCultureProviders[0]; + options.RequestCultureProviders.RemoveAt(0); + options.RequestCultureProviders.Insert(1, questStringCultureProvider); + }); +``` + +In the preceding example above, the order of `QueryStringRequestCultureProvider` and `CookieRequestCultureProvider` is swithched, so the `RequestLocalizationMiddleware` looks for the cultures from the cookies first, then query string. + +As previously mentioned, add a custom provider via which sets the order to `0`, so this provider takes the precedence over the others. + ### Set the culture programmatically This sample **Localization.StarterWeb** project on [GitHub](https://github.com/aspnet/entropy) contains UI to set the `Culture`. The *Views/Shared/_SelectLanguagePartial.cshtml* file allows you to select the culture from the list of supported cultures: