asp.net-core – ASP .NET Core 1.0 RTM本地化无法正常工作
我一直在努力为
microsofts documentation之后的asp .NET Core 1.0 RTM Web应用程序实现本地化,但我无法让它工作.我遇到的问题是,无论我如何尝试设置文化,它总是显示英语资源文件中的字符串.
如果有人有5分钟的时间,我会非常感激你的意见. 这是我关于本地化的Startup.cs内容: public void ConfigureServices(IServiceCollection services) { ... services.AddMvc() .AddViewLocalization(LanguageViewLocationExpanderFormat.SubFolder) .AddDataAnnotationsLocalization(); services.AddScoped<LocalizationFilter>(); services.AddLocalization(options => options.ResourcesPath = "Resources"); var supportedCultures = new List<CultureInfo>{ new CultureInfo("en-US"),new CultureInfo("sl-SI"),new CultureInfo("de-DE"),new CultureInfo("hr-HR") }; services.Configure<RequestLocalizationOptions>(options => { options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; options.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US"),new CultureInfo("en-US")); }); } public void Configure(IApplicationBuilder app,IHostingEnvironment env,ILoggerFactory loggerFactory) { ... var supportedCultures = new List<CultureInfo>{ new CultureInfo("en-US"),new CultureInfo("hr-HR") }; var requestLocalizationOptions = new RequestLocalizationOptions { SupportedCultures = supportedCultures,SupportedUICultures = supportedCultures,DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US"),new CultureInfo("en-US")) }; app.UseRequestLocalization(requestLocalizationOptions); } 这就是我在ActionFilter中改变OnActionExecuting内部文化的方式 actionContext.HttpContext.Response.Cookies.Append( CookieRequestCultureProvider.DefaultCookieName,CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }); CultureInfo.CurrentCulture = culture; CultureInfo.CurrentUICulture = culture; 我也试过在我的控制器上做这个没有运气. 然后在我的视图中我使用@inject IViewLocalizer Localizer来显示带有@Localizer [“Name”]的本地化字符串. 我的resoucres位于Resources / Views / ControllerName文件夹中,如下所示: > Resources / Views / ControllerName / ViewName.en.resx 无论我如何尝试改变文化,显示的字符串始终来自.en资源文件. 我真的不知道我错过了什么.有什么想法吗? 解决方法
您可能会遇到以下情况:
https://github.com/aspnet/Mvc/issues/4692 主要看一下评论:https://github.com/aspnet/Mvc/issues/4692#issuecomment-223671462 简介:您可以在MVC中创建资源过滤器以解决此问题: public class CultureSettingResourceFilter : IResourceFilter,IOrderedFilter { public int Order { get { return int.MinValue; } } public void OnResourceExecuted(ResourceExecutedContext context) { // By this time the response would already have been started,so do not try to modify the response } public void OnResourceExecuting(ResourceExecutingContext context) { var culture = httpContext.GetRouteValue("your-culture-key-name")?.ToString(); // set your culture here CultureInfo.CurrentCulture = culture; CultureInfo.CurrentUICulture = culture; } } services.AddMvc(o => { o.Filters.Add(new CultureSettingResourceFilter()); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 使用FindControl:在窗体视图中访问控件
- asp.net – 如何在代码后面访问span id
- asp.net-mvc – 从Application_Start中获取网站的URL?
- asp.net-mvc – ASP.net MVC – 显示模板集合
- asp.net-mvc – 具有Fluent nHibernate和Ninject的多租户.每
- asp.net – “无法启动IIS Express Web服务器”错误
- asp.net-mvc – 在Asp.Net MVC 2中有一种更好的方法来返回4
- asp.net – 如何停止RadioButtonList标签文本从按钮下包装
- asp.net-mvc-4 – Windows身份验证和Asp.Net Web API
- asp.net-web-api – ASP身份OAuth令牌 – 我应该在移动应用
- 使用ASP.NET,JQuery和Suckerfish构建数据库驱动的
- asp.net – 在剃刀中等同于End / Response.End?
- 使用ASP.NET Core2.2创建WebApp
- ASP.NET requestValidation 4.5和WIF
- asp.net-mvc – 我可以使用Visual Studio的快速版
- MVC3中的IValidatableObject – 客户端验证
- asp.net – Html.Partial()跳过控制器动作
- asp.net-mvc-3 – 如何在ASP MVC3中创建可重用的
- 在提供页面之前确定ASP.NET页面视图状态的大小
- iis – 将整个经典ASP网站重定向到新域名的最佳方