Try our conversational search powered by Generative AI!

Localization for content editors

Vote:
 

Hi,

 

I'm quite new to EPiServer, so this question may already be solved, but I didn't find a solution yet. What is the standard way of having shared localized content. For instance, if I want to have a dropdown list on the site binded to enum with a possibility of content editors to edit it?

Just an example: I want to display a a dropdown with car names:

 

<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

 

I want to take a values from enum and Texts from some global translation(content editors should edit it in Edit mode). Is there already this kind of localization provider in EPi Server?

The solution for this I see now is to create a PageType - LocalizationPage, and put a separate blocks on it. For instance, here it will be a block CarNamesBlock with a fields

Volvo, Saab, Mercedes, Audi.

Is it a kind of standard way?

#85248
Apr 18, 2014 16:26
Vote:
 

I would say that proper way would be to create editor descriptor that is providing possible values from your enum. For instance like this:

    public class EnumEditorDescriptor<TEnum> : EditorDescriptor where TEnum : struct, IComparable, IConvertible, IFormattable
    {
        public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
        {
            SelectionFactoryType = typeof(EnumSelectionFactory<TEnum>);
            ClientEditingClass = "epi-cms/contentediting/editors/SelectionEditor";
            base.ModifyMetadata(metadata, attributes);
        }
    }

    

    public class EnumSelectionFactory<TEnum> : ISelectionFactory
    {
        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {
            var values = Enum.GetValues(typeof(TEnum));
            return (from object value in values
                select new SelectItem
                       {
                               Text = EnumExtensions.GetLocalizedEnumValue(value),
                               Value = value
                       });
        }
    }

    

Then you need to decorate your page/block type property to provide necessary info for EPiServer to render dropdown for that property:

        [Display(GroupName = SystemTabNames.Content, Order = 7)]
        [BackingType(typeof(PropertyNumber))]
        [EditorDescriptor(EditorDescriptorType = typeof(EnumEditorDescriptor<HeaderType>))]
        public virtual HeaderType HeaderLevel { get; set; }

    

 

Then it's up to you how you implement `EnumExtensions.GetLocalizedEnumValue(value)`. There are many ways to implement custom localization provider (few samples here).

Simplest solution is to have a Xml file with resources and use something like this to provide editor for editors :)

Implementation to read localized value from Xml files via EPiServer infrastructure:

    public static class EnumExtensions
    {
        public static string GetLocalizedEnumValue(this Enum value, CultureInfo culture = null)
        {
            return GetLocalizedEnumValue((object) value, culture);
        }

        public static string GetLocalizedEnumValue(object value, CultureInfo culture = null)
        {
            culture = culture ?? CultureInfo.CurrentUICulture;

            Type type = value.GetType();

            string staticName = Enum.GetName(type, value);

            if (staticName != null)
            {
                string localizationPath = string.Format(
                    "/property/enum/{0}/{1}",
                    type.Name.GenerateSlug(),
                    staticName.GenerateSlug());

                return LocalizationService.Current.GetStringByCulture(localizationPath, FallbackBehaviors.Echo, culture);
            }

            return null;
        }
    }

    

#85797
May 06, 2014 0:19
* 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.