0

I'm trying to create a more friendly page URL in Kentico with the two letter culture code (en) instead of the full culture code (en-US). I modified the page URL pattern as seen in the image below. Is there a better way of doing this, should I create a custom macro expression?

The other thing I wanted to achieve was having the default culture not in the URL. Maybe I could also use the custom macro expression for it.

Current situation

A. van Hugten
  • 715
  • 5
  • 16

3 Answers3

1

I have solved the problem with a custom macro expression. This is used in the URL pattern in combination with the DocumentCulture. The macro expression takes the culture alias name which is in case of the default culture empty and else the two letter code, and returns this with a '/'.

The url pattern for a page:

{%Util.GetUrlCultureCode(DocumentCulture)%}{%NodeAliasPath%}

The macro expression I wrote for fixing this problem is as below.

public class CustomCultureMacroExpression : MacroMethodContainer
{
    [MacroMethod(typeof(List<string>), "Returns the url culture code of current document", 1)]
    [MacroMethodParam(0, "Culture", typeof(string), "Current culture.")]
    public static object GetUrlCultureCode(EvaluationContext context, params object[] parameters)
    {
        // Branches according to the number of the method's parameters
        switch (parameters.Length)
        {
            case 1:
                return GetCultureAliasOfCulture(parameters[0].ToString());
            case 2:
                // Weird bug causing macro expression in url pattern to have two parameters where the first parameter is null.
                return GetCultureAliasOfCulture(parameters[1].ToString());
            default:
                // No other overloads are supported
                return string.Empty;
        }
    }

    private static string GetCultureAliasOfCulture(string cultureCode)
    {
        var culture = CultureInfoProvider.GetCultureInfo(cultureCode);
        var culturealias = culture?.CultureAlias ?? string.Empty;

        return string.IsNullOrEmpty(culturealias) ? string.Empty : $"/{culturealias}";
    }
}
A. van Hugten
  • 715
  • 5
  • 16
0

You may try to update culture codes in Localization -> Cultures, however I'm not sure if it won't have any side effect

Roman Hutnyk
  • 1,549
  • 9
  • 14
0

If you use the Dynamic Routing module, you can hook into the GetCulture and GetPage global events to do what you are looking for.

For the GetCulture, you would check if the URL has a 2 letter culture code, if it does then use that as the culture.

You can also adjust the GetPage if needed, although the URL slugs generated should work properly, unless you want a fall back that on the GetPage.After, if there is no found page, you can try removing any culture from the URL and looking up the page by that.

I recommended using the NodeAliasPath for the remaining part of the UrlPattern for ease of use.

Trevor F
  • 1,429
  • 9
  • 6
  • Hi Trevor, thanks for answering. I am using dynamic routing (thanks for that). My problem was having linked widgets where i'm using the RelativePath property of a selected page. This uses the URL pattern and should be culture specific. I'm not sure what you exactly mean with the GetCulture and GetPage events. I have now created a custom macro expression for the url pattern. This also fixes having the default culture not in the url. I will post this as an answer in this question for providing information :) – A. van Hugten Jan 23 '20 at 08:51
  • No problem, the DynamicRouting has event hooks for GetCulture that you can make sure that the Page returned from it is the right culture, however if you have it wired up already that the Site's culture is handled before this, or that the page's URL slug already has the right pattern, then you're set. – Trevor F Jan 26 '20 at 18:42