Copied to clipboard

Flag this post as spam?

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


  • Sjors 1 post 71 karma points
    Aug 24, 2017 @ 12:24
    Sjors
    0

    Get culture in GetUrl function called by a Control

    Hello all,

    In my backend i have a root node called products. This node contains categories. These categories are multilanguage and are show on the product overview page. This page is either part of a "nl" or a "en" tree.

    A made this piece of code to give the categories the correct url to use:

    public string GetUrl(UmbracoContext umbracoContext, int id, Uri current, UrlProviderMode mode)
        {
            // Get the conten to get a new url
            var content = umbracoContext.ContentCache.GetById(id);
    
            // Check if the content is has corrent document type
            if (content != null && content.DocumentTypeAlias == "category" && content.Parent != null)
            {
                // Get culture info
                CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
    
                // Get all the parents
                IEnumerable<IPublishedContent> productOverviewPages = umbracoContext.ContentCache.GetByXPath("//productOverviewPage");
                if (productOverviewPages.Count() > 0)
                {
                    // Get the parent in the correct culture
                    IPublishedContent productOverviewPage = productOverviewPages.FirstOrDefault(p => p.GetCulture().ToString() == currentCulture.ToString());
    
                    if (productOverviewPage != null)
                    {
                        // Create a new url for the item
                        return productOverviewPage.Url + "/" + content.UrlName;
                    }
                }
            }
    
            return null;
        }
    

    The frontend shows the correct url and the links work. My problem is in the backend with the content pickers. When use one, it alway adds a url with "en" in it: http://localhost:49541/en/assortment/lisianthus for example. Even when I select a category from a page in the Dutch (nl) tree.

    This is because the backend is in English, and therefore the current culture is "en". And because the request comes from a control, i can't get the current page. Wat I want to do is get the culture of the page on wich the control is called. That way I can return a url with the correct culture.

    So my question is, is there a way to get the page that is currently open in my backend, or is there a way to trace the call back from the control back to the page, so that I can get the correct Culture. Or is there a other way of doing this.

    I hope i'm being clear enough.

Please Sign in or register to post replies

Write your reply to:

Draft