Ilya Solovyev
Ilya Solovyev

Reputation: 333

Get Windows localization for .NET enums

Is it possible to get Windows localization for .NET enums? For example, I'd like to get translation of System.IO.Ports.Parity values.

Here is System.IO.Ports.Parity:

public enum Parity
{
    None = 0,
    Odd = 1,
    Even = 2,
    Mark = 3,
    Space = 4,
}

Windows shows them as {"Чет", "Нечет", "Нет", "Маркер", "Пробел"} in COM-port properties window (I use Russian version of Windows 8).

The thing is, I don't want to hardcode these "translations". I'd like to get them automatically according to current culture.

Upvotes: 1

Views: 230

Answers (1)

Matt Wilko
Matt Wilko

Reputation: 27342

No, there is no localization for Enums.

The whole point of Enums is that you can use a descriptive name rather than a value, so it makes it easier to code if you are setting a value to Mark rather than 3

Then when you are comparing the value, you don't have to remember that 3 represents Mark

You shouldn't display the EnumValue.ToString() to the user, but you could create your own resource file named strings, add a resource named Mark with the appropriate value, and then lookup the value like this:

ResourceManager rm = strings.ResourceManager;
Debug.WriteLine(rm.GetString(System.IO.Ports.Parity.Mark.ToString()));

See How to use localization in C# and Getting a string dynamically from strings resources for more information.

But that does involve some leg work on your part creating all the translations.

Upvotes: 2

Related Questions