asp.net-mvc – MVC 6:如何使用RESX文件?
我正在尝试将我现有的ASP.NET MVC 5项目迁移到MVC 6 vNext项目,而我已经能够通过并解决大多数问题,我似乎找不到任何有关如何使用RESX资源文件的文档本地化在MVC 6
我的ViewModels正在使用如下语句 [Required(ErrorMessageResourceType = typeof(Resources.MyProj.Messages),ErrorMessageResourceName = "FieldRequired")] 只要RESX被正确地包含在访问修饰符中,并且在vNext项目中似乎不起作用 我在GIT中心网站上看到了几个帖子,他们说ASP.NET 5 / MVC 6的本地化故事是完整的,但是我没有找到任何可以使用资源字符串的样本。 使用上面的代码给我一个错误
编辑:更改文本以澄清我正在寻找在vNext(MVC 6)项目中实现本地化,我可以使其在MVC 5中工作。 编辑2:在实现穆罕默德的答案后,本地化位工作,但现在我陷入了一个新的错误。 一旦我包含 "Microsoft.AspNet.Localization": "1.0.0-beta7-10364","Microsoft.Framework.Localization": "1.0.0-beta7-10364", 包,并在Startup.cs中的ConfigureServices中添加以下行 services.AddMvcLocalization(); 当以下代码执行时,我会收到一个新错误。 public class HomeController : Controller { private readonly IHtmlLocalizer _localizer; public HomeController(IHtmlLocalizer<HomeController> localizer) { _localizer = localizer; } .... 错误:
不知道它是否依赖我丢失或代码中有问题 编辑3: 给任何人仍在寻找解决方案。在这个时间点,您可以使用Muhammad Rehan Saee的答案中的代码,以获得CSHTML中的本地化支持。然而,在验证属性中启用本地化的故事尚未完成(在编辑时:08 / Sep / 2015) https://github.com/aspnet/Mvc/issues/2766#issuecomment-137192942 PS:要修复InvalidOperationException,我做了以下操作
我提出的问题的细节: https://github.com/aspnet/Mvc/issues/2893#issuecomment-127164729 编辑:25 / Dec / 2015 现在终于在MVC 6中工作了。 写了一篇快速博客文章:http://pratikvasani.github.io/archive/2015/12/25/MVC-6-localization-how-to/ 解决方法
您可以查看ASP.NET MVC GitHub项目
here上的完整示例。在编写本文时,这是所有非常新的代码,并可能更改。您需要将以下内容添加到您的启动中:
public class Startup { // Set up application services public void ConfigureServices(IServiceCollection services) { // Add MVC services to the services container services.AddMvc(); services.AddMvcLocalization(); // Adding TestStringLocalizerFactory since ResourceStringLocalizerFactory uses ResourceManager. DNX does // not support getting non-enu resources from ResourceManager yet. services.AddSingleton<IStringLocalizerFactory,TestStringLocalizerFactory>(); } public void Configure(IApplicationBuilder app) { app.UseCultureReplacer(); app.UseRequestLocalization(); // Add MVC to the request pipeline app.UseMvcWithDefaultRoute(); } } IStringLocalizerFactory似乎用于从resx类型创建IStringLocalizer的实例。然后,您可以使用IStringLocalizer获取本地化的字符串。这里是完整的界面(LocalizedString只是一个名称值对): /// <summary> /// Represents a service that provides localized strings. /// </summary> public interface IStringLocalizer { /// <summary> /// Gets the string resource with the given name. /// </summary> /// <param name="name">The name of the string resource.</param> /// <returns>The string resource as a <see cref="LocalizedString"/>.</returns> LocalizedString this[string name] { get; } /// <summary> /// Gets the string resource with the given name and formatted with the supplied arguments. /// </summary> /// <param name="name">The name of the string resource.</param> /// <param name="arguments">The values to format the string with.</param> /// <returns>The formatted string resource as a <see cref="LocalizedString"/>.</returns> LocalizedString this[string name,params object[] arguments] { get; } /// <summary> /// Gets all string resources. /// </summary> /// <param name="includeAncestorCultures"> /// A <see cref="System.Boolean"/> indicating whether to include /// strings from ancestor cultures. /// </param> /// <returns>The strings.</returns> IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures); /// <summary> /// Creates a new <see cref="ResourceManagerStringLocalizer"/> for a specific <see cref="CultureInfo"/>. /// </summary> /// <param name="culture">The <see cref="CultureInfo"/> to use.</param> /// <returns>A culture-specific <see cref="IStringLocalizer"/>.</returns> IStringLocalizer WithCulture(CultureInfo culture); } 最后,您可以像这样注入IStringLocalizer到控制器(请注意,IHtmlLocalizer< HomeController>继承自IStringLocalizer): public class HomeController : Controller { private readonly IHtmlLocalizer _localizer; public HomeController(IHtmlLocalizer<HomeController> localizer) { _localizer = localizer; } public IActionResult Index() { return View(); } public IActionResult Locpage() { ViewData["Message"] = _localizer["Learn More"]; return View(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Asp.net 5 MVC 6,添加facebook电子邮件的权限
- .net – FileLoadException:无法加载文件或程序集System.R
- asp.net – 视图状态中存储的项目的生命周期是多少?
- asp.net-mvc – MVC 3 tempdata容器的缺点
- asp.net – 在.NET 4.5中混合使用Windows和Forms身份验证:
- 将在公共计算机或自助服务终端上使用的ASP.Net Web应用程序
- ASP.NET IIS – 请求什么时候排队?
- asp.net – 从Web服务返回XElement
- asp.net-mvc-3 – 为什么Visual Studio不会破坏这个未处理的
- asp.net – 页面刷新和页面回发之间的区别
- asp.net-mvc-3 – ASP.NET MVC – 不同角色的菜单
- asp.net – SQL Server预登录握手确认错误
- asp.net-mvc-3 – 在razor html helper中传递dat
- asp.net上传文件到数据库的解决方案
- 实体框架 – 使用伪造的DbContext和Autofixture以
- asp.net-web-api – 如何在IdentityServer4中在运
- asp.net – 对我来说MVC?
- asp.net:runat =“server”和服务器控件之间的区
- asp.net-mvc – 如何从URL获取ID作为参数
- 像ValidationGroup这样的ASP.NET使用JQuery验证插