相关疑难解决方法(0)

DisplayNameAttribute的本地化

我正在寻找一种方法来本地化PropertyGrid中显示的属性名称.可以使用DisplayNameAttribute属性"覆盖"属性的名称.不幸的是,属性不能有非常量表达式.所以我不能使用强类型资源,例如:

class Foo
{
   [DisplayAttribute(Resources.MyPropertyNameLocalized)]  // do not compile
   string MyProperty {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

我浏览了一下,发现了一些从DisplayNameAttribute继承的建议,以便能够使用资源.我最终会得到如下代码:

class Foo
{
   [MyLocalizedDisplayAttribute("MyPropertyNameLocalized")] // not strongly typed
   string MyProperty {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

但是我失去了强大的类型资源优势,这绝对不是一件好事.然后我遇到了DisplayNameResourceAttribute,这可能是我正在寻找的.但它应该在Microsoft.VisualStudio.Modeling.Design命名空间中,我找不到我应该为此命名空间添加的引用.

有人知道是否有更好的方法以一种好的方式实现DisplayName本地化?或者是否有使用Microsoft似乎用于Visual Studio的方法?

c# attributes localization visual-studio-2008

119
推荐指数
7
解决办法
8万
查看次数

模型验证错误本地化

我正在开发一个ASP.Net Core Web应用程序,需要本地化我们在应用程序中使用的所有静态字符串.

我通过IStringLocalizerFactory在Startup.cs中实现并将其添加为服务来对Controller和View字符串进行本地化.

我现在正在尝试在使用注释时向模型验证添加本地化.在我的用户模型中,我有

[Required(ErrorMessageResourceType = typeof(UserModelErrorMessages), ErrorMessageResourceName = "FullNameRequired")]
[MinLength(2, ErrorMessageResourceType = typeof(UserModelErrorMessages), ErrorMessageResourceName ="FullNameMinLength")]
[MaxLength(100, ErrorMessageResourceType =typeof(UserModelErrorMessages), ErrorMessageResourceName ="FullNameMaxLength")]
public string FullName { get; set; }
Run Code Online (Sandbox Code Playgroud)

我已经把 "FullNameRequired", "FullNameMinLength" 在UserModelErrorMessage.resx文件, "FullNameMaxLength".我还有一个UserModelErrorMessage.fr.resx文件,其中包含相同的值但法语等效消息.当浏览器设置为英语时,会显示正确的错误消息.当我将浏览器切换为使用法语('fr')语言时,我在视图和控制器中定义的静态字符串以法语显示,但数据验证消息仍以英语显示.我已经尝试过Models.User.fr.resx并且手动生成.resource文件也无济于事.

在Startup.cs ConfigureServices中:

services.AddMvc()
    .AddViewLocalization(options=>options.ResourcesPath="Resources")
    .AddDataAnnotationsLocalization();
Run Code Online (Sandbox Code Playgroud)

在Startup.cs中 - 配置

app.UseRequestLocalization(new RequestLocalizationOptions
{
    SupportedCultures = new List<CultureInfo>
        {
            new CultureInfo("fr"),
            new CultureInfo("zh"),
            new CultureInfo("tr"),
            new CultureInfo("en"),
            new CultureInfo("en-US"),
        },
        SupportedUICultures = new List<CultureInfo>
        {
            new CultureInfo("fr"),
            new CultureInfo("zh"),
            new CultureInfo("tr"),
            new CultureInfo("en"),
            new CultureInfo("en-US"),
        }
}, …
Run Code Online (Sandbox Code Playgroud)

c# validation data-annotations asp.net-core asp.net-core-localization

5
推荐指数
0
解决办法
885
查看次数

MVC - 更改比较属性错误消息

在我的MVC应用程序中,我能够从文本文件中获取错误消息,而不是使用默认错误消息.这完全适用于Required属性(Serverside和Clientside).

我现在需要对Compare属性做同样的事情,但我无法弄清楚如何覆盖Compare属性.

作为参考,这是我使用Required属性的方式(我希望类似的代码使用Compare属性)...

Public Class RequiredFieldAttribute
    Inherits ValidationAttribute
    Implements IClientValidatable

    Private innerAttribute As New RequiredAttribute()
    Private errormessagecontrolid As String

    Public Sub New(ErrorMessageControlID As String)

        Me.errormessagecontrolid = ErrorMessageControlID

    End Sub

    Protected Overrides Function IsValid(value As Object, validationContext As ValidationContext) As ValidationResult

        If Not innerAttribute.IsValid(value) Then
            Return New ValidationResult(ErrorMsgs.Text(Me.errormessagecontrolid))
        End If

        Return ValidationResult.Success

    End Function

    Public Function GetClientValidationRules(metadata As ModelMetadata, context As ControllerContext) As IEnumerable(Of ModelClientValidationRule) Implements IClientValidatable.GetClientValidationRules

        Dim result = New List(Of ModelClientValidationRule)

        Dim rule = New …
Run Code Online (Sandbox Code Playgroud)

vb.net asp.net-mvc

2
推荐指数
1
解决办法
6895
查看次数