Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
AI OnAI Off
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Hi,
There are couple of options here:
public class EnumSelectionFactory<TEnum, TUnderlying> where TEnum : struct { public IEnumerable GetSelections(ExtendedMetadata metadata) { return Enum.GetValues(typeof(TEnum)) .Cast<TEnum>() .Select(s => new SelectItem { Value = ..., Text = ValueName(s as Enum) }); } private static string ValueName(Enum value) { return value.Translate(); } }
Also, IMHO if you are using that Enum as flags, value for the "Small" should not be "3" but "4".
Good luck!
thank you Valdis..
I tried both approaches and both do the job. Slowly i'm getting the hang of this nice package. Thanks alot for your response.
And yes the Flags attribute was just a test and was not needed. Thanks for pointing it out.
Hey Guys,
i'm using DbLocalizationProvider with an EnumSelectionFactory to fill in a dropdown property in the CMS.
I want to be able to translate the text of the dropdown items.
But with below code i always get "String.System" as result of the translation. What am i missing here?
I checked the github : https://github.com/valdisiljuconoks/LocalizationProvider/commit/f7c4536c75fbf48f67e3fee1bd2d77da6f273463 and they do the same thing basically.
I'm on the latest NUget packages of everything.
using EPiServer.Framework.Localization; using DbLocalizationProvider; using EPiServer.ServiceLocation; using EPiServer.Shell.ObjectEditing; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Hyva.Web.Implementation.Services { public class EnumSelectionFactory : BaseService, ISelectionFactory
{
public IEnumerable GetSelections(ExtendedMetadata metadata)
{
return Enum.GetValues(typeof(TEnum))
.Cast()
.Select(s => new SelectItem
{
Value = Convert.ChangeType(s, typeof(TUnderlying)),
Text = ValueName(s)
});
}
private static string ValueName(TEnum value)
{
var staticName = Enum.GetName(typeof(TEnum), value);
var resourceKey = $"{value.GetType().FullName}.{value}";
var translationService = ServiceLocator.Current.GetInstance();
var test = translationService.GetString(() => resourceKey);
return test;
}
}
}
mu enum class:
using DbLocalizationProvider; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Hyva.Web.Logic.Enums { [Flags] [LocalizedResource] public enum ImageSize { Large = 1, Medium = 2, Small = 3 } }
[Pasting files is not allowed]