1

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.

3
  • 2
    What do you mean by localization? Enum values are code identifiers, and though they usually have english-looking names, they're not really counted as text to be displayed to the user. Commented May 27, 2014 at 9:08
  • 2
    There is no localization of enum values, you can get their name as string, but that's it. You're on your own. Commented May 27, 2014 at 9:08
  • As @WilliamAndrewMontgomery pointed out, you can obtain enum names as text, but this is not what is intended to be shown to the user. Instead you have to implement a normal localization, where id can contain enum name (or value) and will have assigned text to it (which will be duplicated in english localisation, to example, "Enum.Port.Parity.Odd=Odd"), then translate it and show. Don't show names as they are. Commented May 27, 2014 at 9:58

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

11 Comments

You are correct in pointing out what you point out - an interesting question the OP brings up, however, remains: Let's say I wanted to use the "Windows default text" for something I want to display (for example: In the dialog to configure a serial port is says "Parity" in English and "Parität" in German), would I be able to somehow access that "default text"?
@ThorstenDittmar, it's not applicable for all enum anyway (as you can't use spaces or special characters in names, etc). So making a dedicated localizable string (even if they match, like "Parity") is a proper way of localizing.
I'm not talking about enums here or anything. My enhanced question is whether it is possible to re-use localized strings from Windows instead of creating my own and localizing that.
Interesting topic for another question: how to access the Windows default localisation text.
@IlyaSolovyev You should accept this answer for the very reason that it answers the question you asked. The fact that you asked the wrong question does not make this answer wrong. You asked for how to get the localization text for .NET enums. The answer here answers that question perfectly. The fact that you want to know how to get the default localized text for the serial port parity values, unrelated to .NET enums, only requires you to ask the right question the next time. The answer here still answers the question you asked.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.