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 required agetype
{
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 ))
{
Prefix = Value. GetType (). Name;
}
String Rk = String. Format ( " {0 }_{ 1} " , Prefix, value );
String Localiza = _ Resources. getstring (rk );
If (Localiza = Null )
{
Return Value. tostring ();
}
Return Localiza;
}
Public Static Ienumerable < Selectlistitem > Toselectlist ( This Enum enumvalue)
{
Return From Enum E In Enum. getvalues (enumvalue. GetType ())
Select New Selectlistitem {selected = E. Equals (enumvalue), text = Enumerableextension. tolocalizable (E), value = E. tostring ()};
}
} Usage:
[Localizable ( Typeof (Corelanguage ), " Uesrtitle " )]
Public Enum Usertitle
{
Mr,
Miss
}
// In View
@ Html. dropdownlistfor (M => M. usertitle, model. usertitle. toselectlist (), New {ID = " Usertitle " })