I had to jump through some hoops to get a localized enum working. I went through dozens of SO articles that never actually answered the relevant conundrum with a working solution. So I made my own:
I have two resource files: LocalizedStrings.resx and LocalizedStrings.es-MX.resx With two corresponding lines:
GENDER_FEMALE Female
GENDER_MALE Male
... and ...
GENDER_FEMALE Mujer
GENDER_MALE Hombre
… respectively.
I have an Enum Extension:
public static class EnumExtensionMethods
{
public static string GetDisplay(this Enum enumValue)
{
object[] attr = enumValue.GetType().GetField(enumValue.ToString())
.GetCustomAttributes(typeof(DisplayAttribute), false);
return attr.Length > 0
? ((DisplayAttribute)attr[0]).GetName()
: enumValue.ToString();
}
}
My actual Enum:
public enum Gender
{
[Display(ResourceType = typeof(LocalizedStrings), Name = "GENDER_MALE")]
Male = 1,
[Display(ResourceType = typeof(LocalizedStrings), Name = "GENDER_FEMALE")]
Female = 0
}
I load the values into a Winforms ComboBox’s items, (I could not get binding it to an entity to work) with the OnLoad event:
foreach (Gender value in Enum.GetValues(typeof(Gender)))
{
GenderComboBox.Items.Add(new KeyValuePair<string, Gender>(value.GetDisplay(), value));
}
GenderComboBox.DisplayMember = "Key";
GenderComboBox.ValueMember = "Value";
GenderComboBox.SelectedIndex = 1;
Finally, I have to set the entity value with this event:
private void GenderComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (!GenderComboBox.Focused)
return;
if (GenderComboBox.SelectedIndex == -1)
return;
KeyValuePair<string, Gender> gender = (KeyValuePair<string, Gender>) GenderComboBox.SelectedItem;
Person person = (Person)PersonBindingSource.Current;
person.Gender = gender.Value;
}
All of the previous code just to get a Localized Enum. Is there not a better way?