Multi-lingual enum descriptions and iteration

 

When working with "enums" you may wish to be able to output an end-user readable format.  This can be done by using a [Description] decoration on the enum.  However, if you wish to have different descriptions based on language, Advantage CSP offers a language specific extension.

API Documentation

Define multiple language example

public enum eExampleEnum
{
//Returns back the name of the enum (None)
None = 0,
//you can use partial culture codes!
[LocalDescription("en", "Good Entry"), LocalDescription("fr", "Bonne entrée")]
Good = 1,
//If spanish, it will pickup the Localized entry, otherwise default
[Description("Bad Entry")]
[LocalDescription("es", "Mala entrada")]
Bad = 2,
//Will always return the LocalDescription when English...Other culture codes return default
[Description("Default Entry")]
[LocalDescription("en", "Entry English")]
Entry = 3
}
// Culture code : en-US
var noneString1 = eExampleEnum.None.GetDescription(); //Returns Enum.ToString() -- "None"
var goodString1 = eExampleEnum.Good.GetDescription(); // Returns based on culture code -- "Good Entry"
var badString1 = eExampleEnum.Bad.GetDescription(); // Returns default description -- "Bad Entry"
var entryString1 = eExampleEnum.Entry.GetDescription(); //Returns base on culture code -- "Entry English"
var badEsCodeString1 = eExampleEnum.Bad.GetDescription("es-MX"); //Override the current culture to return "Mala entrada"
// Culture code : es-MX
var noneString2 = eExampleEnum.None.GetDescription(); //Returns Enum.ToString() -- "None"
var goodString2 = eExampleEnum.Good.GetDescription(); // Returns Enum.ToString() -- "Good"
var badString2 = eExampleEnum.Bad.GetDescription(); // Returns based on culture code -- "Mala entrada"
var entryString2 = eExampleEnum.Entry.GetDescription(); Returns Enum.ToString() -- "Entry"
var badEsCodeString2 = eExampleEnum.Bad.GetDescription("es-MX"); //Override the current culture to return "Mala entrada"

Example of iterating an enum to a dropdown list

//Sample Enum
public enum eExampleEnum
{
//Returns back the name of the enum (None)
None = 0,
//you can use partial culture codes!
[LocalDescription("en", "Good Entry"), LocalDescription("fr", "Bonne entrée")]
Good = 1,
//If spanish, it will pickup the Localized entry, otherwise default
[Description("Bad Entry")]
[LocalDescription("es", "Mala entrada")]
Bad = 2,
//Will always return the LocalDescription when English...Other culture codes return default
[Description("Default Entry")]
[LocalDescription("en", "Entry English")]
Entry = 3
}

//Sample binding
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    dropDownList1.DataSource = Enum<eExampleEnum>.GetListItems();
    dropDownList1.DataBind();

}

Back to Top Button