-
Notifications
You must be signed in to change notification settings - Fork 795
/
EnumLocalTextRegistration.cs
48 lines (43 loc) · 1.93 KB
/
EnumLocalTextRegistration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
namespace Serenity.Localization;
/// <summary>
/// Contains initialization method for adding local text translations defined by
/// Description attributes in enumeration classes.
/// </summary>
public static class EnumLocalTextRegistration
{
/// <summary>
/// Adds local text translations defined implicitly by Description attributes in
/// enumeration classes. Only enum values that has Description attribute are added as
/// local text. By default, enums are registered in format:
/// "Enums.{EnumerationTypeFullName}.{EnumValueName}". EnumerationTypeFullName, is
/// fullname of the enumeration type. This can be overridden by attaching a EnumKey
/// attribute.
/// </summary>
/// <param name="typeSource">Type source to search for enumeration classes in</param>
/// <param name="languageID">Language ID texts will be added (default is invariant language)</param>
/// <param name="registry">Registry</param>
public static void AddEnumTexts(this ILocalTextRegistry registry, ITypeSource typeSource,
string languageID = LocalText.InvariantLanguageID)
{
if (typeSource == null)
throw new ArgumentNullException(nameof(typeSource));
if (registry is null)
throw new ArgumentNullException(nameof(registry));
foreach (var type in typeSource.GetTypes())
{
if (type.IsEnum)
{
var enumKey = EnumMapper.GetEnumTypeKey(type);
foreach (var name in Enum.GetNames(type))
{
var member = type.GetMember(name);
if (member.Length == 0)
continue;
var descAttr = member[0].GetCustomAttribute<DescriptionAttribute>();
if (descAttr != null)
registry.Add(languageID, "Enums." + enumKey + "." + name, descAttr.Description);
}
}
}
}
}