Copied to clipboard

Flag this post as spam?

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


  • Darren Daley 7 posts 87 karma points notactivated
    Jan 16, 2019 @ 11:08
    Darren Daley
    0

    TryFindContent Not Finding Multi Language Pages

    I'm extending the TryFindContent to detect the current language of a user and then based off that redirect them to the appropriate language page.

    The issue I'm having is that when I try and direct the user to anything else besides the default english version of the site it doesn't work.

    For example when a user tries to go to www.website.com/about-us (english version) it works fine

    But if a user tries to go to www.website.com/pt-br/about (portoguese version) it returns a 404 error

    I've pinned it down to this line which returns null when trying to point to another language version of the site

    contentRequest.PublishedContent = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute(route, true);
    

    but for context here is the full code

    public class MultilingualContentFinder : IContentFinder
    {
        private readonly string homeURL = "Home";
        private readonly string brazilURL = "website.com";
        private readonly string portLangPath = "/pt-br";
    
        private readonly string englishLang = "en";
        private readonly string portugueseLang = "pt";
    
        private Dictionary<string, int> homeDictionary = new Dictionary<string, int>();
    
        public bool TryFindContent(PublishedContentRequest contentRequest)
        {
            try
            {
                var url = contentRequest.Uri.AbsoluteUri;
                var path = contentRequest.Uri.GetAbsolutePathDecoded();
    
                var homeNodes = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByXPath("root/home", null).ToList();
    
                foreach (var homeNode in homeNodes)
                {
                    if(homeDictionary.Where(n => n.Key == homeNode.Name).Count() == 0) {
                        homeDictionary.Add(homeNode.Name, homeNode.Id);
                    }
                }
    
                int nodeId = homeDictionary[homeURL];
                contentRequest.Culture = new CultureInfo("en-UK");
    
                if (url.Contains(brazilURL))
                {
                    nodeId = homeDictionary[brazilURL];
                    var languages = HttpContext.Current.Request.UserLanguages;
    
                    if (languages.Length > 0)
                    {
                        var language = "pt"; //languages[0].Split('-')[0];
    
                        if (language == portugueseLang && !url.Contains("pt-br"))
                        {
                            path = portLangPath + path;
                            url = portLangPath + url;
                            contentRequest.SetRedirect(url);
                            contentRequest.Culture = new CultureInfo("pt-BR");
                        }
                        else if (language == englishLang && url.Contains("pt-br"))
                        {
                            path = path.Replace(portLangPath, "");
                            contentRequest.SetRedirect(path);
                            nodeId = homeDictionary[brazilURL];
                        }
                    }
                }
    
                var route = url.Contains(brazilURL) ? nodeId + path : path;
                contentRequest.PublishedContent = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute(route, true);
    
            }
            catch (Exception ex) {
                Umbraco.Core.Logging.LogHelper.Error<MultilingualContentFinder>("MultilingualContentFinder exception", ex);
            }
    
            return contentRequest.PublishedContent != null;
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft