Enumeration Texts

Display text for enumeration values can be specified with Description attribute.

  1. namespace MyApplication
  2. {
  3. public enum Sample
  4. {
  5. [Description("First Value")]
  6. Value1 = 1,
  7. [Description("Second Value")]
  8. Value2 = 2
  9. }
  10. }

This enumeration and its Description attributes defines following local text keys and translations:

Key LanguageID Text (Translation)
Enums.MyApplication.Sample.Value1 First Value
Enums.MyApplication.Sample.Value2 Second Value

All texts are defined for invariant language ID by default.

You can use these keys to access translated descriptions for enumeration values, or use extension method GetText() defined for enumeration types (import namespace Serenity to make this extension method available).

  1. using Serenity;
  2. //...
  3. Console.WriteLine(MyApplication.Sample.Value1.GetText());
  1. > First Value

EnumKey Attribute

Enumeration translations use full name of enumeration type as prefix to generate local text keys. This prefix can be overriden with EnumKeyAttribute:

  1. namespace MyApplication
  2. {
  3. [EnumKey("Something")]
  4. public enum Sample
  5. {
  6. [Description("First Value")]
  7. Value1 = 1,
  8. [Description("Second Value")]
  9. Value2 = 2
  10. }
  11. }

Now defined keys and translations are:

Key LanguageID Text (Translation)
Enums.Something.Value1 First Value
Enums.Something.Value2 Second Value

EnumLocalTextRegistration Class

[namespace: Serenity.Localization, assembly: Serenity.Core]

For enumeration local text definitions to be registered, you need to call EnumLocalTextRegistration.Initialize() method in your application start:

  1. void Application_Start()
  2. {
  3. EnumLocalTextRegistration.Initialize(ExtensibilityHelper.SelfAssemblies);
  4. }

It gets list of assemblies to search for enumeration types. You can pass list of assemblies manually or use ExtensibilityHelper.SelfAssemblies which contains all assemblies that reference a Serenity assembly.

CommonInitialization.Run and CommonInitialization.InitializeLocalTexts methods call it by default.