Simple Solution for Enumeration type localization operations, and applied to the DropdownList of Asp.net MVC.
/// <summary>
/// Enum Localizable. default resouce key={Prefix}_{Enum}
/// If you ignore Prefix,will return {EnumTypeName}_{Enum} format
/// </summary>
[AttributeUsage(AttributeTargets.Enum, AllowMultiple = true, Inherited = true)]
public class LocalizableAttribute : Attribute
{
public LocalizableAttribute(Type language)
{
this.LanguageType = language;
}
public LocalizableAttribute(Type language, String prefix)
:this(language)
{
Prefix = prefix;
}
public Type LanguageType
{
get;
set;
}
public string Prefix
{
get;
set;
}
}
public static class EnumerableExtension
{
public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
{
foreach (T item in enumerable)
{
action(item);
}
}
public static string ToLocalizable(this Enum value)
{
LocalizableAttribute customAttr=(LocalizableAttribute) Attribute.GetCustomAttribute(value.GetType(), typeof(LocalizableAttribute));
ResourceManager _resources = new ResourceManager(customAttr.LanguageType);
var prefix=customAttr.Prefix;
if (string.IsNullOrEmpty(prefix))
&nbs