Tengo que agregar la localización en mi aplicación web.
Una solicitud es que un servicio grpc proporcione el idioma predeterminado para todos los usuarios, por lo que en el inicio intento leer ese valor pero no sé cómo usar un servicio registrado dentro del nuevo CustomRequestCultureProvider
public static CultureInfo[] supportedCultures = new[] { new CultureInfo("it-IT"), new CultureInfo("en-US")}; public void ConfigureServices(IServiceCollection services) { services.AddTransient<ISpaClient, GrpcSpaClient>(); services.AddLocalization(options => options.ResourcesPath = "Resources"); services.AddMvc() .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) .AddDataAnnotationsLocalization(); services.Configure<RequestLocalizationOptions>(options => { options.DefaultRequestCulture = new RequestCulture(culture: supportedCultures[0].ToString(), uiCulture: supportedCultures[0].ToString()); options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; options.RequestCultureProviders.Clear(); options.RequestCultureProviders.Add(new MyCustomRequestCultureProvider()); }); }
Mi clase personalizada
public class MyCustomRequestCultureProvider : RequestCultureProvider { public override async Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext) { await Task.Yield(); if (httpContext == null) { throw new ArgumentNullException(nameof(httpContext)); } if (!httpContext.User.Identity.IsAuthenticated) { return null; } var culture = httpContext.User .Claims .FirstOrDefault(c => c.Type == TipoClaim.linguaPredefinita.ToString())? .Value; if (culture == null) { return null; } return new ProviderCultureResult(culture); } }
Pero aquí httpContext.User
nunca se Authenticated
, incluso si la página es
Mi mejor suposición es que llamó a UseRequestLocalization antes de UseAuthentication.