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.

Ilya Solovyev
  • 313
  • 1
  • 3
  • 8
  • 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. – Lasse V. Karlsen May 27 '14 at 09: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. – user703016 May 27 '14 at 09: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. – Sinatr May 27 '14 at 09:58

1 Answers1

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.

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • 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"? – Thorsten Dittmar May 27 '14 at 09:59
  • @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. – Sinatr May 27 '14 at 10:01
  • 1
    I'm not talking about `enum`s 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. – Thorsten Dittmar May 27 '14 at 10:05
  • 2
    Interesting topic for another question: how to access the Windows default localisation text. – david.pfx May 27 '14 at 10:08
  • @ThorstenDittmar Exactly. This is what my question is about. – Ilya Solovyev May 27 '14 at 10:09
  • @MattWilko, I can't accept your answer. Windows already has translations for all Enum values (meaning those used for serial ports). The question is how to get them for every culture. – Ilya Solovyev May 27 '14 at 10:30
  • Understood, I think the question has evolved a bit from the original, so I will leave my answer up in case it helps anyone else out. – Matt Wilko May 27 '14 at 10:32
  • @IlyaSolovyev - you're mistaken, there is a difference between windows localization and .NET framework localization. You're trying to get resource text of some windows dialogs - yes enum values are matching, but there is no connection - heck, you can have even windows without .NET framework installed. – Ondrej Svejdar May 27 '14 at 11:17
  • 1
    @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. – Lasse V. Karlsen May 27 '14 at 11:21
  • @Ondrej If you are shure, that there is no connection, then post your statement as an answer and I will mark it so. – Ilya Solovyev May 28 '14 at 01:51
  • @LasseV.Karlsen I didn't ask the wrong question. There was lack of details - I fixed it. – Ilya Solovyev May 28 '14 at 01:56