Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • edge33 6 posts 76 karma points
    Aug 28, 2018 @ 13:48
    edge33
    0

    Href to Localized Content

    Hi Everyone,

    I Currently have a multilingual website with the following root nodes:

    1. it - locale set to italian
    2. en - locale set to english

    I chose to keep this setting:

    <add key="umbracoHideTopLevelNodeFromPath" value="false" />
    

    in order to get the default root in italian, and access the english root by adding "en" to the site address, let say:

    • site.com - italian
    • site.com/en - english

    This, causes the issue when redirecting via anchor the the product page. How do I know what root to get?

    What I did so far is: register the route handler for both, localized and non localized url, and resolve the correct path:

    RouteTable.Routes.MapUmbracoRoute(
                "productIT",
                "product/{id}",
                new
                {
                    locale = "it",
                    controller = "ProductPage",
                    action = "GetProductById",
                    name = "product"
                },
                resolveRootByLocale);
    
            RouteTable.Routes.MapUmbracoRoute(
                "product",
                "{locale}/product/{id}",
                new
                {
                    controller = "ProductPage",
                    action = "GetProductById",
                    name = "product"
                },
                resolveRootByLocale);
    

    where 'resolveRootByLocale' is defined as follows:

    public class ProductNodeByLocaleAndNameRouteHandler : UmbracoVirtualNodeRouteHandler
    {
        protected override IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext)
        {
            return umbracoContext.ContentCache.GetByRoute("/" + requestContext.RouteData.Values["locale"] + "/" + requestContext.RouteData.Values["name"]);
        }
    }
    

    Now, when redirecting to the product page, I have to prefix in html the localized root of the site I intend to use, let say:

    <a href="/product/0001">product</a>
    

    or

    <a href="en/product/0001">product</a>
    

    I want to avoid prefexing the root locale. Do you guys have any clue on how to do this?

  • Sven Geusens 169 posts 881 karma points c-trib
    Sep 06, 2018 @ 11:06
    Sven Geusens
    1

    Hi and welcome to the Umbraco community!

    You can get the current culture 2 letter notation by doing Umbraco.CultureDictionary.Culture.TwoLetterISOLanguageName

    Wrapped into an extension method so that the it language returns no path

    public static class UmbracoHelperExtenstion
    {
        public static string CurrentLanguagePath(this UmbracoHelper umbracoHelper)
        {
            var currentLanguage = umbracoHelper.CultureDictionary.Culture.TwoLetterISOLanguageName.ToLower();
            switch (currentLanguage)
            {
                case "it":
                    return null;
                default:
                    return currentLanguage;
            }
        }
    }
    

    You can replace <a href="/product/0001">product</a> by <a href="@Umbraco.CurrentLanguagePath()/product/0001">product</a> And always have the product in the current language.

  • edge33 6 posts 76 karma points
    Sep 07, 2018 @ 12:32
    edge33
    0

    sweet! Thank you

  • Sven Geusens 169 posts 881 karma points c-trib
    Sep 07, 2018 @ 12:41
    Sven Geusens
    1

    No problem

    I think I found an even beter solution today.

    In your resolveRootByLocale you set the linked PublishedContent to the node based on the locale.

    If my assumption is correct, this is the general productpage relative to your language

    You can use this to generate the Full Url <a href="@(Model.Content.Url)/0001">product</a>

    Model.Content.Url generates the Url with a / at the end.

    If this works, you don't need the hacky extension method I supplied earlier. (Might not work because of of the umbracoHideTopLevelNodeFromPath, not sure)

Please Sign in or register to post replies

Write your reply to:

Draft