大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – MVC 6:如何使用RESX文件?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的ViewModels正在使用如下语句
[required(ErrormessageresourceType = typeof(resources.MyProj.messages),Errormessageresourcename = "Fieldrequired")]
只要RESX被正确地包含在访问修饰符中,并且在vNext项目中似乎不起作用
有谁知道如何在MVC 6 vNext项目中使用RESX?
我在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)
看看下面的mvc的GITHUB网站上的问题:
https://github.com/aspnet/Mvc/issues/2766#issuecomment-137192942
PS:要修复InvalidoperationException,@R_902_10673@下操作
我提出的问题的细节:
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/
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-mvc – MVC 6:如何使用RESX文件?全部内容,希望文章能够帮你解决asp.net-mvc – MVC 6:如何使用RESX文件?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。