开发者

Localization DisplayName Attribute

I want to translate property. I have two resources files: DataResource.resx and DataResource.en.resx. There are NameString string(both). My property:

[DisplayName("NameString")]
public virtual string Name { get; set; }

I have used this solution for localization DataDisplay attribute.

public class LocalizedDisplayNameAttribute: DisplayNameAttribute
{
    public LocalizedDisplayNameAttribute(string resourceId) 
        : base开发者_开发百科(GetMessageFromResource(resourceId))
    { }

    private static string GetMessageFromResource(string resourceId)
    {
        // TODO: Return the string from the resource file
    }
}

But I don't understand what I must to write in GetMessageFromResource method.

Thanks.


For your custom DataAnnotations attribute your need to write following code in your GetMessageFromResource method:

private static string GetMessageFromResource(string resourceId)
{
    var propertyInfo = typeof(DataResource).GetProperty(resourceId, BindingFlags.Static | BindingFlags.Public);
    return propertyInfo.GetValue(null, null);
}

This code should do the job assuming you have an error in your question and there are should be LocalizeDisplayNameAttribute, not the DisplayName one:

[DisplayName("NameString")]
public virtual string Name { get; set; }

Anyway I recommend using of lambda accessors for getting localized strings from resources so you can rename/navigate them using your refactoring tool.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜