Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The approach i found for internationalization of Enums or getting text of Enums from respective Resource files is to create an attribute class by inheriting DescriptionAttribute class </p> <pre><code>public class EnumResourceAttribute : DescriptionAttribute { public Type ResourceType { get; private set; } public string ResourceName { get; private set; } public int SortOrder { get; private set; } public EnumResourceAttribute(Type ResourceType, string ResourceName, int SortOrder) { this.ResourceType = ResourceType; this.ResourceName = ResourceName; this.SortOrder = SortOrder; } } </code></pre> <p>Create another Static class that will provide extension methods for GetString and GetStrings.</p> <pre><code>public static class EnumHelper { public static string GetString(this Enum value) { EnumResourceAttribute ea = (EnumResourceAttribute)value.GetType().GetField(value.ToString()) .GetCustomAttributes(typeof(EnumResourceAttribute), false) .FirstOrDefault(); if (ea != null) { PropertyInfo pi = ea.ResourceType .GetProperty(CommonConstants.ResourceManager); if (pi != null) { ResourceManager rm = (ResourceManager)pi .GetValue(null, null); return rm.GetString(ea.ResourceName); } } return string.Empty; } public static IList GetStrings(this Type enumType) { List&lt;string&gt; stringList = new List&lt;string&gt;(); FieldInfo[] fiArray = enumType.GetFields(); foreach (FieldInfo fi in fiArray) { EnumResourceAttribute ea = (EnumResourceAttribute)fi .GetCustomAttributes(typeof(EnumResourceAttribute), false) .FirstOrDefault(); if (ea != null) { PropertyInfo pi = ea.ResourceType .GetProperty(CommonConstants.ResourceManager); if (pi != null) { ResourceManager rm = (ResourceManager)pi .GetValue(null, null); stringList.Add(rm.GetString(ea.ResourceName)); } } } return stringList.ToList(); } } </code></pre> <p>And on the elements of your Enum you can write :</p> <pre><code>public enum Priority { [EnumResourceAttribute(typeof(Resources.AdviceModule), Resources.ResourceNames.AdviceCreateAdviceExternaPriorityMemberHigh, 1)] High, [EnumResourceAttribute(typeof(Resources.AdviceModule), Resources.ResourceNames.AdviceCreateAdviceExternaPriorityMemberRoutine, 2)] Routine } </code></pre> <p>Where <code>Resources.ResourceNames.AdviceCreateAdviceExternaPriorityMemberHigh</code> &amp; <code>Resources.ResourceNames.AdviceCreateAdviceExternaPriorityMemberRoutine</code> are constants in the resource file or you can say the strings whose values can be available in different cultures.</p> <p>If you are implementing your web application in MVC architecture then create a property </p> <pre><code>private IList result; public IList Result { get { result = typeof(Priority).GetStrings(); return result; } } </code></pre> <p>and in your <code>.cshtml</code> file you can just bind the enum to your dropdownlist like :</p> <pre><code> @Html.DropDownListFor(model =&gt; Model.vwClinicalInfo.Priority, new SelectList(Model.Result)) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload