Search code examples
asp.netaspnetboilerplate

Localization in EnumDropDownListFor using asp.net boilerplate


I have an enum dropdown

//control
 @Html.EnumDropDownListFor(
                        m => m.OrderBy,
                        new {@class = "btn btn-default dropdown-toggle toggle", onchange = "document.getElementById('hf_Pagename').value,this.form.submit();"})

//my enum
 public enum OrderByOptions
    {
        Default,
        PriceLowToHigh,
        PriceHighToLow,
        MostRecent
    }

Now the problem is I need to localize them. But in this case from" PriceLowToHigh" needs to change to " Price- low to high"


Solution

  • You can use AbpDisplayNameAttribute:

    public enum OrderByOptions
    {
        [AbpDisplayName(MyConsts.LocalizationSourceName, "OrderByOptions.Default")]
        Default,
        [AbpDisplayName(MyConsts.LocalizationSourceName, "OrderByOptions.PriceLowToHigh")]
        PriceLowToHigh,
        [AbpDisplayName(MyConsts.LocalizationSourceName, "OrderByOptions.PriceHighToLow")]
        PriceHighToLow,
        [AbpDisplayName(MyConsts.LocalizationSourceName, "OrderByOptions.MostRecent")]
        MostRecent
    }
    

    Define them in your localization files:

    <text name="OrderByOptions.PriceLowToHigh">Price - Low to High</text>
    

    Update

    AbpDisplayName works on type class

    You can define:

    [AttributeUsage(AttributeTargets.Field)]
    public class FieldAbpDisplayNameAttribute : AbpDisplayNameAttribute
    {
        // ...
    }
    

    Then use [FieldAbpDisplayNameAttribute(...)] instead.