Improve culture cookie handling and localization responses

Refactored GetCulture to return null if cookie is missing or invalid, and added GetCultureOrDefault for fallback. Updated TestLocalizerController and CultureMiddleware to use new methods for more accurate culture detection. Localizer now returns only the localized string value.
This commit is contained in:
2026-02-13 09:46:01 +01:00
parent 297ab458c7
commit a982f48ef9
3 changed files with 10 additions and 15 deletions

View File

@@ -60,20 +60,16 @@ public static class WebExtensions
#endregion
#region Cookie
public static string GetCulture(this IRequestCookieCollection cookies)
public static string? GetCulture(this IRequestCookieCollection cookies)
{
var cookieValue = cookies[CookieRequestCultureProvider.DefaultCookieName];
if (!string.IsNullOrEmpty(cookieValue))
{
var culture = CookieRequestCultureProvider.ParseCookieValue(cookieValue)?.Cultures.FirstOrDefault().Value;
if (!string.IsNullOrEmpty(culture))
return culture;
}
return CultureInfo.CurrentUICulture.Name;
if (cookies[CookieRequestCultureProvider.DefaultCookieName] is string cookieValue)
return CookieRequestCultureProvider.ParseCookieValue(cookieValue)?.Cultures.FirstOrDefault().Value;
else
return null;
}
public static string GetCultureOrDefault(this IRequestCookieCollection cookies) => GetCulture(cookies) ?? CultureInfo.CurrentCulture.Name;
public static void SetCulture(this IResponseCookies cookies, string culture)
{
var cookieOptions = new CookieOptions