diff --git a/application/shared-kernel/SharedKernel/Authentication/UserInfo.cs b/application/shared-kernel/SharedKernel/Authentication/UserInfo.cs
index 0650f8f59..d417d81e1 100644
--- a/application/shared-kernel/SharedKernel/Authentication/UserInfo.cs
+++ b/application/shared-kernel/SharedKernel/Authentication/UserInfo.cs
@@ -1,4 +1,5 @@
using System.Security.Claims;
+using PlatformPlatform.SharedKernel.SinglePageApp;
namespace PlatformPlatform.SharedKernel.Authentication;
@@ -8,13 +9,15 @@ namespace PlatformPlatform.SharedKernel.Authentication;
///
public class UserInfo
{
+ private const string DefaultLocale = "en-US";
+
///
/// Represents the system user, typically used for background tasks or where no user is directly authenticated.
///
public static readonly UserInfo System = new()
{
IsAuthenticated = false,
- Locale = "en-US"
+ Locale = DefaultLocale
};
public bool IsAuthenticated { get; init; }
@@ -37,14 +40,14 @@ public class UserInfo
public string? AvatarUrl { get; init; }
- public static UserInfo Create(ClaimsPrincipal? user, string defaultLocale)
+ public static UserInfo Create(ClaimsPrincipal? user, string? browserLocale)
{
if (user?.Identity?.IsAuthenticated != true)
{
return new UserInfo
{
IsAuthenticated = user?.Identity?.IsAuthenticated ?? false,
- Locale = defaultLocale
+ Locale = GetValidLocale(browserLocale)
};
}
@@ -59,7 +62,27 @@ public static UserInfo Create(ClaimsPrincipal? user, string defaultLocale)
LastName = user.FindFirstValue(ClaimTypes.Surname),
Title = user.FindFirstValue("title"),
AvatarUrl = user.FindFirstValue("avatar_url"),
- Locale = user.FindFirstValue("locale")
+ Locale = GetValidLocale(user.FindFirstValue("locale"))
};
}
+
+ private static string GetValidLocale(string? locale)
+ {
+ if (string.IsNullOrEmpty(locale))
+ {
+ return DefaultLocale;
+ }
+
+ if (SinglePageAppConfiguration.SupportedLocalizations.Contains(locale, StringComparer.OrdinalIgnoreCase))
+ {
+ return locale;
+ }
+
+ // Fallback to base language. E.g. if locale is `en-UK` use `en` which would then return `en-US`
+ var baseLanguageCode = locale[..2];
+ var foundLocale = SinglePageAppConfiguration.SupportedLocalizations
+ .FirstOrDefault(sl => sl.StartsWith(baseLanguageCode, StringComparison.OrdinalIgnoreCase));
+
+ return foundLocale ?? DefaultLocale;
+ }
}
diff --git a/application/shared-kernel/SharedKernel/ExecutionContext/HttpExecutionContext.cs b/application/shared-kernel/SharedKernel/ExecutionContext/HttpExecutionContext.cs
index 24c67cba4..d77725aa2 100644
--- a/application/shared-kernel/SharedKernel/ExecutionContext/HttpExecutionContext.cs
+++ b/application/shared-kernel/SharedKernel/ExecutionContext/HttpExecutionContext.cs
@@ -35,9 +35,9 @@ public UserInfo UserInfo
return _userInfo;
}
- var defaultLocale = httpContextAccessor.HttpContext?.Features.Get()?.RequestCulture.Culture.Name;
+ var browserLocale = httpContextAccessor.HttpContext?.Features.Get()?.RequestCulture.Culture.Name;
- return _userInfo = UserInfo.Create(httpContextAccessor.HttpContext?.User, defaultLocale ?? "en-US");
+ return _userInfo = UserInfo.Create(httpContextAccessor.HttpContext?.User, browserLocale);
}
}
}