Answered by:
how to catch the unsupported cultures

Question
-
User818337214 posted
Hi,
Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware:Warning:
AcceptLanguageHeaderRequestCultureProvider returned the following unsupported cultures 'en-US, en, tr'.
I wanna catch the unsupported cultures. there are two culture those are "en-GB" and "tr-TR", in my configuration
but if culture of browser is "en-US", return me the DefaultRequestCulture that is "tr-TR" .
I must catch and set it from "en-US" to "en-GB"
How to catch it in culture Middleware or anywhere? I will catch "en" in "en-US" and I will forward it to "en-GB"
var supportedCultures = new List<CultureInfo> { new CultureInfo("tr-TR"), new CultureInfo("en-GB"), }; app.UseRequestLocalization(new RequestLocalizationOptions { SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures, DefaultRequestCulture = new RequestCulture("tr-TR") });
Friday, August 30, 2019 10:20 AM
Answers
-
User1034446946 posted
https://afana.me/archive/2011/01/14/aspnet-mvc-internationalization.aspx/
theres a culturehelper class half way down the page.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 30, 2019 12:42 PM -
User818337214 posted
I have just finished it. I learned a lot of things about localization. I will share my aim:
If you are in the USA (en-US) and the WebSite have support for en-GB, you have to see default of culture what it is... (if you don't add "en" culture.... )
var supportedCultures = new List<CultureInfo> { new CultureInfo("tr-TR"), new CultureInfo("en-GB"), }; app.UseRequestLocalization(new RequestLocalizationOptions { SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures, DefaultRequestCulture = new RequestCulture("tr-TR") }); app.UseMiddleware<CustomRequestLocalization>(supportedCultures);
public class CustomRequestLocalization { private readonly RequestDelegate _next; private readonly RequestLocalizationOptions _requestLocalizationOptions; private readonly ILogger<CustomRequestLocalization> _logger; private readonly List<CultureInfo> _supportedCultures; public CustomRequestLocalization(RequestDelegate next, IOptions<RequestLocalizationOptions> requestLocalizationOptions, ILogger<CustomRequestLocalization> logger, List<CultureInfo> supportedCultures) { _next = next; _logger = logger; _requestLocalizationOptions = requestLocalizationOptions.Value; _supportedCultures = supportedCultures; } public async Task InvokeAsync(HttpContext context) { //https://stackoverflow.com/questions/43871234/how-to-get-cookiename-used-in-cookierequestcultureprovider //https://github.com/aspnet/AspNetCore/blob/master/src/Middleware/Localization/src/RequestLocalizationMiddleware.cs var supportedCultures = _supportedCultures; var browserCultures = context.Request.GetTypedHeaders().AcceptLanguage?.OrderByDescending(x => x.Quality ?? 1) // Quality defines priority from 0 to 1, where 1 is the highest. .Select(x => x.Value.ToString()) .ToArray() ?? Array.Empty<string>(); var providerCookieCulture = _requestLocalizationOptions.RequestCultureProviders.SingleOrDefault(x => x.GetType() == typeof(CookieRequestCultureProvider)); var cookieName = ((CookieRequestCultureProvider)providerCookieCulture).CookieName; var cookieCulture = context.Request.Cookies[cookieName] ?? "there is no cookie"; if (context.Request.Cookies[cookieName] == null) { var search = true; foreach (var itemBrowserCulture in browserCultures.ToArray()) { if (search) { foreach (var itemSupportedCulture in supportedCultures.ToArray()) { if (itemSupportedCulture.ToString().Contains(itemBrowserCulture.ToString())) { _logger.LogTrace($"no cookie and browserCulture: {itemBrowserCulture} equal supportedCulture: {itemSupportedCulture}"); var culture = new CultureInfo(itemSupportedCulture.ToString()); CultureInfo.CurrentCulture = culture; CultureInfo.CurrentUICulture = culture; search = false; break; } } foreach (var itemSupportedCulture in supportedCultures.ToArray()) { if (itemSupportedCulture.ToString().Contains(itemBrowserCulture.ToString().Substring(0, 2))) { _logger.LogTrace($"no cookie and browserCulture: {itemBrowserCulture} equal supportedCulture: {itemSupportedCulture}"); var culture = new CultureInfo(itemSupportedCulture.ToString()); CultureInfo.CurrentCulture = culture; CultureInfo.CurrentUICulture = culture; search = false; break; } } } } } await this._next(context); } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 1, 2019 10:41 PM
All replies
-
User1034446946 posted
https://afana.me/archive/2011/01/14/aspnet-mvc-internationalization.aspx/
theres a culturehelper class half way down the page.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 30, 2019 12:42 PM -
User818337214 posted
thank you EnenDaveyBoy
Saturday, August 31, 2019 8:57 AM -
User818337214 posted
I have just finished it. I learned a lot of things about localization. I will share my aim:
If you are in the USA (en-US) and the WebSite have support for en-GB, you have to see default of culture what it is... (if you don't add "en" culture.... )
var supportedCultures = new List<CultureInfo> { new CultureInfo("tr-TR"), new CultureInfo("en-GB"), }; app.UseRequestLocalization(new RequestLocalizationOptions { SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures, DefaultRequestCulture = new RequestCulture("tr-TR") }); app.UseMiddleware<CustomRequestLocalization>(supportedCultures);
public class CustomRequestLocalization { private readonly RequestDelegate _next; private readonly RequestLocalizationOptions _requestLocalizationOptions; private readonly ILogger<CustomRequestLocalization> _logger; private readonly List<CultureInfo> _supportedCultures; public CustomRequestLocalization(RequestDelegate next, IOptions<RequestLocalizationOptions> requestLocalizationOptions, ILogger<CustomRequestLocalization> logger, List<CultureInfo> supportedCultures) { _next = next; _logger = logger; _requestLocalizationOptions = requestLocalizationOptions.Value; _supportedCultures = supportedCultures; } public async Task InvokeAsync(HttpContext context) { //https://stackoverflow.com/questions/43871234/how-to-get-cookiename-used-in-cookierequestcultureprovider //https://github.com/aspnet/AspNetCore/blob/master/src/Middleware/Localization/src/RequestLocalizationMiddleware.cs var supportedCultures = _supportedCultures; var browserCultures = context.Request.GetTypedHeaders().AcceptLanguage?.OrderByDescending(x => x.Quality ?? 1) // Quality defines priority from 0 to 1, where 1 is the highest. .Select(x => x.Value.ToString()) .ToArray() ?? Array.Empty<string>(); var providerCookieCulture = _requestLocalizationOptions.RequestCultureProviders.SingleOrDefault(x => x.GetType() == typeof(CookieRequestCultureProvider)); var cookieName = ((CookieRequestCultureProvider)providerCookieCulture).CookieName; var cookieCulture = context.Request.Cookies[cookieName] ?? "there is no cookie"; if (context.Request.Cookies[cookieName] == null) { var search = true; foreach (var itemBrowserCulture in browserCultures.ToArray()) { if (search) { foreach (var itemSupportedCulture in supportedCultures.ToArray()) { if (itemSupportedCulture.ToString().Contains(itemBrowserCulture.ToString())) { _logger.LogTrace($"no cookie and browserCulture: {itemBrowserCulture} equal supportedCulture: {itemSupportedCulture}"); var culture = new CultureInfo(itemSupportedCulture.ToString()); CultureInfo.CurrentCulture = culture; CultureInfo.CurrentUICulture = culture; search = false; break; } } foreach (var itemSupportedCulture in supportedCultures.ToArray()) { if (itemSupportedCulture.ToString().Contains(itemBrowserCulture.ToString().Substring(0, 2))) { _logger.LogTrace($"no cookie and browserCulture: {itemBrowserCulture} equal supportedCulture: {itemSupportedCulture}"); var culture = new CultureInfo(itemSupportedCulture.ToString()); CultureInfo.CurrentCulture = culture; CultureInfo.CurrentUICulture = culture; search = false; break; } } } } } await this._next(context); } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 1, 2019 10:41 PM