Same page language switching in Umbraco

Land on the same page in different language in Umbraco using Relations

When we build multi-language website, we usually create two root elements which represent different language websites.

To switch the language you just need to add a link to other root elements which represent other languages of the website. With this approach, when you switch language you will end up at homepage of the language regardless which page you were viewing.

Most of the time, clients do not complain about this, but..

Since it is the same website but in different languages it is most likely that both language websites are going to have pretty similar if not totally the same structure. Because of this you can expect from client to ask for language switch which lands on the same page in different language.

I'll try to explain the case on the following example of page URLs:

  • http://en.mywebsite.com/about-us/contact
  • http://rs.mywebsite.com/about-us/contact

You can see that urls of different language websites are pretty much the same in structure except for the sub-domain. This is when you can start using relations provided by Umbraco.

Umbraco Relation

You probably spotted the checkbox "Relate copied items to original" when you want to copy some content. I assume nobody really cared about this if they did not use relations, because whether you select this option or not, it does not take any effect on the website.

What it really does is that it creates relation in separate Umbraco table where it keeps pointer for a new node from which it originated from.

Note

You can view all website relations created when document copied in Developer/Relations/Relate Document On Copy

This can be used to obtain URL of one page in a different language easily and really fast since relations act as a sort of index table. Umbraco already has API for accessing relations. In earlier versions this API was not so well developed, but starting from Umbraco 6 it pretty stable and useful especially in cases like this when you have multi-language websites.

The following method is an example how to fetch related content inside different root (language) document.

        public static IPublishedContent GetRelatedContent(this IPublishedContent Content, IPublishedContent TargetWebsite)
        {
            IPublishedContent related = null;
            UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
            var allRelations = umbraco.cms.businesslogic.relation.Relation.GetRelations(Content.Id);
            if (allRelations != null && allRelations.Any())
            {
                var relations = allRelations.Where(r => !r.Child.IsTrashed && !r.Parent.IsTrashed).ToArray();
                if (Content.AncestorOrSelf(1).Id != TargetWebsite.Id)
                {
                    foreach (var rel in relations)
                    {
                        var child = helper.TypedContent(rel.Child.Id);
                        if (child != null && child.AncestorOrSelf(1).Id == TargetWebsite.Id)
                        {
                            related = child;
                        }
                    }

                    if (related == null)
                    {
                        foreach (var rel in relations)
                        {
                            var parent = helper.TypedContent(rel.Parent.Id);
                            if (parent != null && parent.AncestorOrSelf(1).Id == TargetWebsite.Id)
                            {
                                related = parent;
                            }
                        }
                    }
                }
                else
                {
                    related = helper.TypedContent(relations.First().Parent.Id);
                }
            }
            if (related == null)
            {
                related = TargetWebsite;
            }
            return related;
        }
    

If the document is not found in target website, root document (language homepage) URL will be used, so in any case you will end up on a selected language website, the only difference is that you will land to a same page in different language or, if not found on the homepage on selected language.

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

.NET

read more

JavaScript

read more

SQL/T-SQL

read more

PowerShell

read more

Comments for this article