ASP.NET Core DisplayAttribute本地化

时间:2016-06-15 11:06:27

标签: asp.net-core asp.net-core-1.0 displayattribute asp.net-core-localization

根据documentation

  

运行时不会查找非验证属性的本地化字符串。在上面的代码中,“电子邮件”(来自[显示(姓名="电子邮件")]将不会被本地化。

我正在寻找一种在DisplayAttribute中本地化文本的方法。有任何建议以适当的方式进行吗?

4 个答案:

答案 0 :(得分:9)

您可以在ResourceType上设置DisplayAttribute.resx可用于本地化您的文字。

将资源MyResources.resx文件添加到您的项目,例如MyResources,并为您的字段添加资源:

enter image description here

然后在DisplayAttribute

中引用字段名称和[Display(Name = "RememberMe", ResourceType = typeof(MyResources))] public bool RememberMe { get; set; } 类型
NotImplementedException

本地化资源将自动拉出(参见文本框)

enter image description here

注意:如果您在资源值中使用非拉丁字符,RC2中当前存在错误,该错误会引发import numpy as np while 1: angle=input("Please enter the angle you want to convert,\n\n"\ "If you wish to convert degrees in radiant or vise-versa,\n"\ "follow this format: 'angle/D or R'").split('/') if angle is not list():break angle[0]=float(angle[0]) radiant= (angle[0]*(np.pi))/180 degre=((angle[0]*180)/np.pi) if (angle[0]>=0 or angle[0]<=360) and angle[1] is 'D' : print(radiant,'radiants') elif angle[1] is 'R': print(degre,'degrés') else:break https://github.com/aspnet/Razor/issues/760

答案 1 :(得分:6)

在视图或数据注释中拥有所有本地化的中心位置是我能想到的最佳方法,以及我如何开展工作。  在安装nuget软件包进行本地化后,在 Startup.cs 文件中添加以下代码

services.AddMvc().AddViewLocalization().AddDataAnnotationsLocalization(options => 
    options.DataAnnotationLocalizerProvider = (type, factory) => new StringLocalizer<Resources>(factory));

services.Configure<RequestLocalizationOptions>(options => {
   var cultures = new[]
   {
       new CultureInfo("en"),
       new CultureInfo("ar")
   };
   options.DefaultRequestCulture = new RequestCulture("en", "en");
   options.SupportedCultures = cultures;
   options.SupportedUICultures = cultures;
});

这样 DataAnnotationLocalizerProvider 将来自资源。{culture} .rex - (资源文件必须具有无代码生成<的访问修饰符< / strong>) - 假设默认语言不需要任何资源,并且能够访问资源文件,因为不会生成任何代码,并且必须创建具有相同名称的空类。

并在 _ViewImports.cshtml 文件中注入以下内容

@inject IHtmlLocalizer<Resources> Localizer

通过执行此操作,您现在可以在任何视图中使用全局变量 Localizer 以进行本地化。

This is how a Central Location for String Localization

您可以在Globalization and localization in ASP.NET Core

找到更多信息

答案 2 :(得分:2)

对于那些挣扎的人(@lucius,@ vladislav),有错误:

  

无法检索属性&#39; 名称&#39;因为本地化失败了输入&#39; Xxxx.EmployeeResx &#39;不公开或不包含名称为&#39; FirstName &#39;的公共静态字符串属性。

它是由.resx文件上的访问修饰符引起的,默认设置为内部(在我的情况下,它是无代码生成)。在资源文件工具栏的“访问修改器”下拉列表中将其更改为 public

[https://i.imgur.com/q3BK8T5.png]

之后,您应该能够从资源类型中看到属性:

enter image description here

另外,请考虑不在字段名称中使用特殊符号,因为它们是自动生成的C#属性名称的基础。字段名称将转换为C#友好名称,这就是为什么您最终可能会在资源文件名称字段和自动生成的属性名称之间出现不一致。最好避免使用任何连字符-或点.下划线_都可以。您始终可以在相关资源文件下的 resource_file_name.Designer.cs 类中查找自动生成的属性的外观。

enter image description here

非常感谢Bala Murugan在Code Digest上写了一篇关于这个主题的好文章。

答案 3 :(得分:1)

我刚刚创建了一个项目,该项目演示了本地化,包括类属性和枚举的Display属性的本地化。

该项目可在https://github.com/feradz/ASPNetCoreLocalization/wiki

找到

必须使用ASP.NET Core 1.0之前的方法对Display属性进行本地化。查看项目中的DataAnnotations.resx文件。

Name的{​​{1}}属性不能包含空格和特殊字符。

Display

[Display(Name = "NoSpacesAndSpecialChanractersHere", ResourceType = typeof(Resources.DataAnnotations))] public string FirstName { get; set; } 应该是完全限定的资源类名称(即包括名称空间)。