public void IsSupportedTest() { var cultures = new AppSupportedCultures(); Assert.True(cultures.IsSupported("en-GB")); Assert.False(cultures.IsSupported("xx-GB")); }
public void IsSupportedFailTest() { var cultures = new AppSupportedCultures(); Assert.False(cultures.IsSupported(null)); Assert.False(cultures.IsSupported("")); }
public string GetText(string resourceName, string uiCultureTab = null, params Object[] list) { var rc = "[not found]"; try { if (_supportedCultures != null) { if (_rm == null) { var asm = typeof(C19QCalcLib.C19QCalcLib).Assembly; var baseName = asm.GetName().Name + ".Properties.Resources"; _rm = new ResourceManager(baseName, asm); } var cookies = new MxCookies(_httpContextAccessor); if (_supportedCultures.IsSupported(uiCultureTab) == false) { uiCultureTab = _supportedCultures.GetUiCultureTab(cookies.GetValue(MxSupportedCultures.CookieName)); } var uiCultureInfo = _supportedCultures.GetCultureInfo(uiCultureTab); if (uiCultureInfo != null) { var res = _rm?.GetString(resourceName ?? "NotFound", uiCultureInfo) ?? "[Not Found]"; rc = string.Format(uiCultureInfo, res, list); } } } catch (Exception e) { // ReSharper disable once UnusedVariable var msg = e.Message; //ignore } return(rc); }
//Create an extension method for the User that returns the Culture that the User has stored in his Profile //public static string GetCulture(this ClaimsPrincipal claimsPrincipal) //{ // if (claimsPrincipal == null) // throw new ArgumentNullException(nameof(claimsPrincipal)); // return claimsPrincipal.FindFirstValue(Constants.ClaimTypes.Culture); //or Constants.ClaimTypes.locale - see https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims //} public override async Task <ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext) { if (httpContext == null) { throw new ArgumentNullException(); } var updateCookie = true; //var configuration = httpContext.RequestServices.GetService<IConfigurationRoot>(); //Configuration providers read configuration data from key-value pairs using a variety of configuration sources https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1 var culture = AppSupportedCultures.DefaultTab; //configuration[CultureKey]; var uiCulture = AppSupportedCultures.DefaultUiTab; //configuration[UiCultureKey]; if (httpContext.User.Identity.IsAuthenticated) { //https://ml-software.ch/posts/writing-a-custom-request-culture-provider-in-asp-net-core-2-1 culture = AppSupportedCultures.DefaultTab; //GetCultureTabFromValue(httpContext.User.GetCulture()); uiCulture = AppSupportedCultures.DefaultUiTab; //GetUiCultureTabFromValue(httpContext.User.GetUiCulture()); } else { if (Options != null) { foreach (var provider in Options.RequestCultureProviders) //0: this, 1: query string, 2: cookie, 3: Accept-Language header { var providerTypeName = provider.GetType().ToString(); if (providerTypeName.Equals(this.GetType().ToString()) == false) { var val = await provider.DetermineProviderCultureResult(httpContext); if (val?.Cultures?.Count > 0) { culture = val.Cultures[0].ToString(); } if (val?.UICultures?.Count > 0) { uiCulture = val.UICultures[0].ToString(); } if (val?.Cultures?.Count > 0) { if (providerTypeName.Equals(CookieProviderTypeName)) { updateCookie = false; } break; } } } } } var cookies = new MxCookies(httpContext.RequestServices.GetService <IHttpContextAccessor>()); if (_supportedCultures.IsSupported(culture) == false) { culture = _supportedCultures.GetNearestMatch(culture); updateCookie = true; } if (_supportedCultures.IsSupported(uiCulture) == false) { uiCulture = _supportedCultures.GetNearestMatch(uiCulture, true); updateCookie = true; } if (updateCookie) { cookies.SetValue(MxSupportedCultures.CookieName, _supportedCultures.GetCulturesEncodedValue(culture, uiCulture)); } return(new ProviderCultureResult(culture, uiCulture)); }