Strongly Localized EPiServer Categories

Initially strongly typed localization provider was not planned to be used everywhere in EPiServer, but localizing categories came as complimentary feature:

[LocalizedResource(KeyPrefix = "/categories/")]
public class Categories  
{
    [ResourceKey("category[@name=\"" + nameof(SampleCategory) + "\"]/description")]
    public static string SampleCategory => "Some Category !";
}

Still there is lot of ceremony to get things right..

Sick and tired of generating proper resource keys for localizing EPiServer categories? Say no more. Localization provider gives you now (within latest version) possibility to decorate your class with attribute and necessary translations will be added automagically.

Here is a sample category:

namespace MyProject  
{
    [LocalizedCategory]
    public class SomeCategory : Category { }
}

By adding [LocalizedCategory] attribute to your class, provider will generate following resource key:

$"/categories/category[@name=\"{categoryName}\"]/description"

NB! Class you decorate with [LocalizedCategory] must inherit from EPiServer.DataAbstraction.Category class.

This is the key EPiServer will look for when translating categories for UI.
By default class name of the category type will be taken as translation for default culture.
You can change this by setting Name of the category (you might receive warning - that virtual member is used in constructor):

namespace MyProject  
{
    [LocalizedCategory]
    public class SomeCategory : Category
    {
        public SomeCategory()
        {
            Name = "Some Category !";
        }
    }
}



Pst! Thinking why categories should be defined in code and why my category class should be even inheriting from EPiServer category built-in class?? You definitely wanna check out blog post and effort made by brilliant colleague of mine! Patrick made it possible to auto register discovered categories in EPiServer from code with no need to access UI to get the boring task done!



Hope this helps! Happy categorizing!

[eof]

wałdis iljuczonok

Software architect, lead technologist and Visual Studio ALM & TFS evangelist, technical fellow. Focus on solution research, new technologies analysis and enterprise designing.

https://tech-fellow.net
riga.lv

comments powered by Disqus