Enum property translation not working

Vote:
 

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

http://joelabrahamsson.com/enum-properties-with-episerver/

https://world.episerver.com/Blogs/Linus-Ekstrom/Dates/2014/5/Enum-properties-for-EPiServer-75/

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:

        [Display(
            Name = "Column count",
            GroupName = SystemTabNames.Content,
            Order = 10)]
        [Enum(typeof(BlockColumnCount))]
        [BackingType(typeof(PropertyNumber))]
        public virtual BlockColumnCount ColumnCount { get; set; }

My localization configuration in web config:

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

My language file:

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

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.

#225330
Jul 13, 2020 11:29
Vote:
 

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.

#225332
Jul 13, 2020 11:37
Vote:
 

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.

#225333
Jul 13, 2020 11:43
Vote:
 

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

https://github.com/Geta/EPi.Extensions/blob/47504aeae31d75ffe0cb7495d124b99649c934a4/src/Geta.EPi.Extensions/EditorDescriptors/EnumSelectionFactory.cs#L32

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.

#225334
Jul 13, 2020 11:47
Vote:
 

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,

        [Display(
            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; }
#225335
Jul 13, 2020 11:48
Vote:
 

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.

#225336
Jul 13, 2020 11:58
Vote:
 

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.

#225338
Jul 13, 2020 12:19
Vote:
 

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.

#225340
Jul 13, 2020 13:48
Vote:
 

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">

#225341
Jul 13, 2020 14:03
Vote:
 

Yep. that was it!

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