Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

DbLocalizationProvider with Enum question

Vote:
 

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]

#194711
Edited, Jun 28, 2018 16:13
Vote:
 

Hi,

There are couple of options here:

  • firstly, you are passing in "string" as object to lambda expression for the translation resolution. In this case (when you have already resolved the key to the resource, you should use just "GetString(resourceKey)"
  •  secondly, you can omit whole resource key calculation as use one of the extensions of the Enum class, but then you would need to change code a bit:
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!

#194716
Jun 28, 2018 18:14
Vote:
 

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. 

#194730
Edited, Jun 29, 2018 8:36
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.