简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM