Enum localization

后端 未结 15 797
慢半拍i
慢半拍i 2020-11-30 17:56

How do you localize enums for a ListBoxFor where multiple options are possible?

For example an enum that contains roles:

pu         


        
15条回答
  •  孤独总比滥情好
    2020-11-30 18:17

    I use this currently, hope it helps!:

        /// 
        ///  Retrieves a translated value from an enumerated list.
        /// 
        /// Enum
        /// string
        /// string
        protected string GetTranslatedEnum(Enum value, string resource)
        {
            string path = String.Format("Resources.{0}", resource);
    
            ResourceManager resources = new ResourceManager(path, global::System.Reflection.Assembly.Load("App_GlobalResources"));
    
            if (resources != null) {
                return resources.GetString(value.ToString());
            } else {
                return value.ToString();
            }
        }
    

    Created a .resx file named "App_GlobalResources\ProductNames.resx".

    Usage:

    // Convert the ProductId integer on the item to its Enum equivalent.
    Products product = (Products) item.ProductId;
    
    string productName = this.GetTranslatedEnum(product, "ProductNames");
    

提交回复
热议问题