Pra*_*tik 17 asp.net-mvc asp.net-core-mvc asp.net-core
我正在尝试将我现有的ASP.NET MVC 5项目迁移到MVC 6 vNext项目,而我已经能够解决大部分问题,我似乎无法找到有关如何使用RESX资源文件的任何文档MVC中的本地化6
我的ViewModel正在使用像
[Required(ErrorMessageResourceType = typeof(Resources.MyProj.Messages), ErrorMessageResourceName = "FieldRequired")]
Run Code Online (Sandbox Code Playgroud)
只要RESX被正确包含并且访问修饰符设置正确,这在MVC 5中运行良好,但它似乎无法在vNext项目中工作有人知道如何在MVC 6 vNext项目中使用RESX吗?
我在这里和GIT中心网站上看到了一些帖子,它们说ASP.NET 5/MVC 6的本地化故事已经完成,但是我找不到任何使用资源字符串的体面样本.
使用上面的代码给我一个错误
错误CS0246找不到类型或命名空间名称"资源"(您是否缺少using指令或程序集引用?)
编辑:更改文本以澄清我正在寻找vNext(MVC 6)项目中的本地化实现,我能够使其在MVC 5中工作.
编辑2:在实施穆罕默德的答案后得到了本地化位,但我现在陷入了一个新的错误.
一旦我包括在内
"Microsoft.AspNet.Localization": "1.0.0-beta7-10364",
"Microsoft.Framework.Localization": "1.0.0-beta7-10364",
Run Code Online (Sandbox Code Playgroud)
打包并在Startup.cs中的ConfigureServices中添加以下行
services.AddMvcLocalization();
Run Code Online (Sandbox Code Playgroud)
执行以下代码时出现新错误.
public class HomeController : Controller
{
private readonly IHtmlLocalizer _localizer;
public HomeController(IHtmlLocalizer<HomeController> localizer)
{
_localizer = localizer;
}
....
Run Code Online (Sandbox Code Playgroud)
错误:
处理请求时发生未处理的异常.
InvalidOperationException:尝试激活"Microsoft.Framework.Localization.ResourceManagerStringLocalizerFactory"时,无法解析类型"Microsoft.Framework.Runtime.IApplicationEnvironment"的服务.Microsoft.Framework.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider提供程序,ISet`1 callSiteChain)
无法弄清楚我是否缺少依赖关系或代码中存在问题
编辑3:
对于仍在寻找解决方案的人.此时,您可以使用Muhammad Rehan Saee的答案中的代码来获得CSHTML中的本地化支持.但是,在验证属性中启用本地化的故事尚未完成(在编辑时:2015年9月8日)请查看以下mvc的GITHUB站点上的问题:
https://github.com/aspnet/Mvc/issues/2766#issuecomment-137192942
PS:要修复InvalidOperationException,我执行了以下操作
将所有依赖项作为beta7-*并清除我的C:\ Users\.dnx\packages的所有内容摆脱了错误.
我提出的问题的详细信息:
https://github.com/aspnet/Mvc/issues/2893#issuecomment-127164729
编辑:2015年12月25日
现在终于在MVC 6中工作了.
在这里写了一篇快速的博客文章:http://pratikvasani.github.io/archive/2015/12/25/MVC-6-localization-how-to/
您可以在此处查看 ASP.NET MVC GitHub 项目上的完整示例。在撰写本文时,这都是非常新的代码,可能会发生变化。您需要将以下内容添加到启动中:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
似乎IStringLocalizerFactory用于创建IStringLocalizerresx 类型的实例。然后您可以使用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);
}
Run Code Online (Sandbox Code Playgroud)
最后你可以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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10286 次 |
| 最近记录: |