Post

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:

1
2
3
4
5
6
[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:

1
2
3
4
5
namespace MyProject
{
    [LocalizedCategory]
    public class SomeCategory : Category { }
}

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

1
$"/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):

1
2
3
4
5
6
7
8
9
10
11
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]

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.