-
Notifications
You must be signed in to change notification settings - Fork 12
/
EnumSelectionFactory.cs
43 lines (38 loc) · 1.32 KB
/
EnumSelectionFactory.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
using System;
using System.Collections.Generic;
using EPiServer.Framework.Localization;
using EPiServer.Shell.ObjectEditing;
namespace Geta.EPi.Extensions.EditorDescriptors
{
/// <summary>
/// Selection factory for enum types used by EPiServer Properties
/// </summary>
/// <typeparam name="TEnum">Enum type with options</typeparam>
public class EnumSelectionFactory<TEnum> : ISelectionFactory
{
public IEnumerable<ISelectItem> GetSelections(
ExtendedMetadata metadata)
{
var values = Enum.GetValues(typeof(TEnum));
foreach (var value in values)
{
yield return new SelectItem
{
Text = GetValueName(value),
Value = value
};
}
}
private string GetValueName(object value)
{
var staticName = Enum.GetName(typeof(TEnum), value);
string localizationPath = string.Format("/property/enum/{0}/{1}", typeof(TEnum).Name.ToLowerInvariant(), staticName.ToLowerInvariant());
string localizedName;
if (LocalizationService.Current.TryGetString(localizationPath, out localizedName))
{
return localizedName;
}
return staticName;
}
}
}