簡體   English   中英

如何本地化類顯示名稱

[英]How do I Localize a Class Display Name

如何使用資源文件本地化該類的DisplayName。 Display屬性不適用於類,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 ....
}

解決方案是創建自己的顯示名稱屬性。 下面的代碼對我有用,並且可以通過傳遞Resource文件名(在我的情況下我只是對其進行硬編碼)而變得更通用。

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);
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM