我必须在我的网络应用程序中添加本地化。
请求是 grpc 服务将为所有用户提供默认语言,因此在启动时我尝试读取该值,但不知道如何在 new 中使用注册的服务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());
});
}
我的定制课程
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);
}
}
但在这儿httpContext.User
从来没有Authenticated
,即使页面是
我最好的猜测是您在 UseAuthentication 之前调用了 UseRequestLocalization。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)