我的viewmodels正在使用如下语句
[required(ErrorMessageResourceType = typeof(Resources.MyProj.Messages),ErrorMessageResourceName = "Fieldrequired")]
只要RESX被正确地包含在访问修饰符中,并且在vNext项目中似乎不起作用
有谁知道如何在MVC 6 vNext项目中使用RESX?
我在GIT中心网站上看到了几个帖子,他们说ASP.NET 5 / MVC 6的本地化故事是完整的,但是我没有找到任何可以使用资源字符串的样本。
Error CS0246 The type or namespace name ‘Resources’ could not be found
(are you missing a using directive or an assembly reference?)
编辑:更改文本以澄清我正在寻找在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; } ....
错误:
An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type
‘Microsoft.Framework.Runtime.IApplicationEnvironment’ while attempting
to activate
‘Microsoft.Framework.Localization.ResourceManagerStringLocalizerFactory’.
Microsoft.Framework.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider
provider,ISet`1 callSiteChain)
不知道它是否依赖我丢失或代码中有问题
编辑3:
给任何人仍在寻找解决方案。在这个时间点,您可以使用Muhammad Rehan SAEe的答案中的代码,以获得CSHTML中的本地化支持。然而,在验证属性中启用本地化的故事尚未完成(在编辑时:08 / Sep / 2015)
看看下面的mvc的GITHUB网站上的问题:
https://github.com/aspnet/Mvc/issues/2766#issuecomment-137192942
PS:要修复InvalidOperationException,我做了以下操作
Taking all dependencies as the beta7-* and clearing all the contents
of my C:\Users\.dnx\packages got rid of the error.
我提出的问题的细节:
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(); } }