2015-02-12 4 views
0

Как локализовать с помощью файлов ресурсов DisplayName для класса. Атрибут отображения не работает в классах, а атрибут DisplayName не поддерживает локализацию.Как локализовать отображаемое имя класса

[MetadataTypeAttribute(typeof(ServiceType.ServiceTypeMetadata))] 
// Compile Error: Attribute 'Display' is not valid on this declaration type. It is only valid on 'method, property, indexer, field, param' declarations. 
// [Display(ResourceType = typeof(Resources.DisplayNames), Name = "ServiceType")] 
[System.ComponentModel.DisplayName("Service Type")] 
public partial class ServiceType : ILookupEntity<EdmBilingualStringVarCharSingleLine>, ISelectListEntity, IUpdateableEntity 
{ 
    #region Metadata 
    internal sealed class ServiceTypeMetadata 
    { 
     [DataType(DataType.Date)] 
     [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
     [Display(ResourceType = typeof(Resources.DisplayNames), Name = "InactiveDate")] 
     public DateTime? InactiveDate { get; set; } 
     [Display(ResourceType = typeof(Resources.DisplayNames), Name = "ModifiedBy")] 
     [Required] 
     public string ModifiedBy { get; set; } 
     [Display(ResourceType = typeof(Resources.DisplayNames), Name = "ModifiedDate")] 
     [Required] 
     public System.DateTime ModifiedDate { get; set; } 

    } 
    #endregion 

    // ... the rest of the class .... 
} 

ответ

0

Решение заключается в создании собственного атрибута Display Name. Следующие работали для меня и могли быть сделаны более универсальными, передав имя файла ресурсов, которое в моем случае я просто жестко запрограммировано.

using System.ComponentModel; 
using System.Resources; 


namespace SCC.Case.Entities 
{ 
    /// <summary> 
    /// Provides a class attribute that lets you specify the localizable string for the entity class 
    /// </summary> 
    class DisplayNameForClassAttribute : DisplayNameAttribute 
    { 
     public string Name { get; set; } 
     public override string DisplayName 
     { 
      get 
      { 
       ResourceManager resourceManager = new ResourceManager("SCC.Case.Entities.DisplayNames", typeof(DisplayNameForClassAttribute).Assembly); 
       return resourceManager.GetString(this.Name); 
      } 
     } 
    } 
} 
Смежные вопросы