Loading...
 
Multi-Language Add-In for Visual Studio

Multi-Language Add-In for Visual Studio


Translating Enumeration Names

Does anyone have any best practice for translating enumeration names

We have quite a few places where we use the enum name, either byitself, or inserted into a string either in its raw format or spacing out the Pascal cased words.

For example:

Public Enum eFormType
DataForm = 1
Indicator = 2
Download = 3
End Enum

The individual words could be button texts, or they could be inserted into a some text "You have selected the {0} option".

DataForm may also be displayed as "Data Form"


Martin

I've run into the same thing. Althought it is very convenient to use the ability of .NET to convert the enumeration value to a string, it's not that well suited for much more than using this string in debug output. One approach is to create a method that simply returns a unique English string for each enumeration value. You can then use the MultiLang.ml.ml_string() method to return the proper translation. You may want the default portion of the case statement to return the actual enumeration value converted to a string - that way at least the user will see something in english if you later add a new enum value and forget to update the method.

It's more code, but you have to admit that it would allow you to be more descriptive, even when working in English, since you don't have to adhere to PascalCase.


> Does anyone have any best practice for translating enumeration names
>
> We have quite a few places where we use the enum name, either byitself, or inserted into a string either in its raw format or spacing out the Pascal cased words.
>
> For example:
>
> Public Enum eFormType
> DataForm = 1
> Indicator = 2
> Download = 3
> End Enum
>
> The individual words could be button texts, or they could be inserted into a some text "You have selected the {0} option".
>
> DataForm may also be displayed as "Data Form"
>
>
> Martin

Germany

This is one way that I have used.

Get the class EnumDescConverter from http://www.codeproject.com/KB/cs/enumdescconverter.aspx.

This class is in C#, but if you need it in VB, you can convert it here (which is what I have done).

Specify this class as a type converter, and specify the names for the Enum vales using the description attribute. For example:

non localized version
<TypeConverter(GetType(EnumDescConverter))> _
Public Enum LeftOrRight
  <Description("Left hand side")>   Left = 0
  <Description("Right hand side")>  Right = 1
End Enum

I'm not sure whether this works in all circumstances, but it certainly works in a property grid (which is where I am using it).

When you scan this with the Add-In, it will detect the description strings in the attributes.

Unfortunately, attributes are very difficult to localize. When you select the string for localization, the Add-In will create a derived attribute class ml_Desctiption (in the MlString module), so that it can provide the StringID as an additional parameter to the constructor.

derived description attribute class
Friend Class ml_DescriptionAttribute
  Inherits System.ComponentModel.DescriptionAttribute
  Sub New ( ByVal StringID As Integer, ByVal text As String )
    MyBase.New ( ml_string ( StringID, text ) )
  End Sub
End Class

After localization, the enum will look like this:

localized version
<TypeConverter(GetType(EnumDescConverter))> _
Public Enum LeftOrRight
  <ml_Description(1735,"Left hand side")>   Left = 0
  <ml_Description(1736,"Right hand side")>  Right = 1
End Enum

I realize that that is bit complicated, but as I already said, it works for me in a property grid. I have no idea whether you would call it best practice.


Phil

Germany

If you have trouble downloading the class from codeproject, I can post a copy. I see that you now need to log in to start a download (which previously was not necessary).

My examples are in VB, but of course it works identically in C#.

Phil