Enum property translation not working


I'm using `Enum` from Geta EPi.Extensions attribute on a block propert, which implement ideas following these blog posts:



Everything is working fine, except the translations for enum properties are not being picked in the UI.

My enum looks like this:

  public enum BlockColumnCount
        OneColumn = 0,
        TwoColumns = 1,
        ThreeColumn = 2

My property like that:

            Name = "Column count",
            GroupName = SystemTabNames.Content,
            Order = 10)]
        public virtual BlockColumnCount ColumnCount { get; set; }

My localization configuration in web config:

<localization fallbackBehavior="Echo, MissingMessage, FallbackCulture" fallbackCulture="en">
        <add virtualPath="~/Resources/LanguageFiles" 
             type="EPiServer.Framework.Localization.XmlResources.FileXmlLocalizationProvider, EPiServer.Framework.AspNet"/>

My language file:

<?xml version="1.0" encoding="utf-8" ?>
  <language name="en">
          <onecolumn>One Column</onecolumn>
          <twocolumns>Two Columns</twocolumns>
          <threecolumn>Three Columns</threecolumn>

I have the file in the correct localization:

This is how the UI looks - lables from XML file are not showing up.

According to this post, it should work to add /property/enum/<type>/<member> into /languages/language, but it's not working for me and at that point of time I don't have any idea what's going on. Please advise.

Jul 13, 2020 11:29

I think <enums> should appear directly beneath <language>.

In this case I think the <property> between them is incorrect, as its the enum itself you're defining a translation for not a property.

Jul 13, 2020 11:37

Actually that may be just difference in our implementation.

Our usage uses:

    public class EnumSelectionFactory<TEnum> : ISelectionFactory
        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
            var values = Enum.GetValues(typeof(TEnum));

            var items = new List<ISelectItem>();
            foreach (TEnum value in values)
                items.Add(new SelectItem { Text = value.GetLocalisedString(), Value = value });
            return items.OrderBy(i => i.Text);

With the extension method: 

        public static string GetLocalisedString<TEnum>(this TEnum value)
            var name = Enum.GetName(typeof(TEnum), value);

            string path = $"/enums/{typeof(TEnum).Name.ToLowerInvariant()}/{name?.ToLowerInvariant() ?? "null"}";
            return LocalizationService.Current
                .TryGetString(path, out var localisedName)
                ? localisedName
                : name;

Might be a case of checking which bits you've borrowed from each article.

Jul 13, 2020 11:43

I'm suing Get.EPiExtenensions, and according to their implementation:


the key is build like `property/enum/<enum_type_name>/<enum_member_name>`. But the problem possibly might be elswhere. I dumped all localization entries, calling `

LocalizationService.Current.GetAllStrings();` and it turns our that I can't find anything that related the entries that I've created in thr XML, no `property` no `enum` no `blockcolumncount` and no entries for the enum members.

Jul 13, 2020 11:47

We're actually mostly using enums with SelectOne and Select Many, and not actually using the Geta extensions so perhaps I've miunderstood. But that's how we do it anyhow,

            GroupName = SystemTabNames.Content,
            Order = 250)]
        [SelectMany(SelectionFactoryType = typeof(EnumSelectionFactory<MemberStatus>))]
        public virtual string MemberStatusTypes { get; set; }

        [Display(GroupName = SystemTabNames.Content, Order = 350)]
        [SelectOne(SelectionFactoryType = typeof(EnumSelectionFactory<Global.Theme>))]
        public virtual Global.Theme Theme { get; set; }
Jul 13, 2020 11:48

Web.config entry looks fine - I presume that's within <episerver.framework> and that the /Resources/LanguageFiles/ is within the web app project and not a class library.

I believe that alone should be enough to see it if you inspect the localization service directly. I have found that I have sometimes needed to recycle the app pool when adding for them to show up.

Other things worth looking out for may be typos, errors in the logs reading files perhaps, perhaps checking XML files don't contain any odd leading characters. I tend to create mine directly in Visual Studio on Windows but if I'm receiving files sometimes I use Visual Studio code to look for any hidden characters which might throw out the processing.

Jul 13, 2020 11:58

Just to confirm if there is an issue parsing the XML file it doesn't cause the app to crash but does show in the logs, and obviously does cause it to be missing from the locaization service - I deliberately added a leading space to my file to test it, I get this:

2020-07-13 13:12:48,661 [172] ERROR EPiServer.Framework.Localization.XmlResources.FileXmlLocalizationProvider: Unhandled exception while handling FileSystemWatcher event Renamed on file C:\<path to web app root>\Resources\LanguageFiles\Enums.xml.
System.Configuration.ConfigurationErrorsException: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 1, position 4. (/Resources/LanguageFiles/Enums.xml line 1) ---> System.Xml.XmlException: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 1, position 4.

You could also use this trick to check it is seeing and attempting to read the file itself by deliberately adding a leading space and seeing if that causes you to see the error message. I'm presuming it could be the case for other characters too.

Jul 13, 2020 12:19

I had some errors from FileXmlLocalizationProvider but when I intentionally messed up the configuration while testing. I can see that when I edit the XML file diretctly, it throws an error because it complains that the file is being used by another process, so it clearly sees the file and listens to changes. Yet the entries are not resolved.

Jul 13, 2020 13:48

Hmm, try updating the language XML file so that language looks like the following with the name attribute - just realised I have that on mine and you don't and I think that may be making a difference:

<language name="English" id="en">

Jul 13, 2020 14:03

Yep. that was it!

Jul 13, 2020 15:26
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.