Copied to clipboard

Flag this post as spam?

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


  • Roberto Bianchi 137 posts 446 karma points c-trib
    Feb 19, 2016 @ 17:02
    Roberto Bianchi
    0

    Create a dynamic hreflang tag

    Hello,

    how it's possibile to create a dynamic hreflang in a multilingual website?

    I have this structure:

    English root

    • nodes in english

    Italian root

    • nodes in italian

    German root

    • nodes in german

    French root

    • nodes in french

    The name of the nodes are in the language of the root, because the name of the node generates the url. So it's important to mantain the language of the root node.

    How can I link with the hreflang tag the same page in the other languages?

  • Kin 30 posts 172 karma points
    Feb 19, 2016 @ 19:05
    Kin
    100

    FYI: You can change the URL of the node with a nifty built-in property of Umbraco called umbracoUrlName. If that field is set, it will use it instead of auto-generating one using the name of the node.

    (Legacy, but it still applies) https://our.umbraco.org/wiki/reference/umbraco-best-practices/umbracourlname/

  • Roberto Bianchi 137 posts 446 karma points c-trib
    Feb 22, 2016 @ 09:20
    Roberto Bianchi
    0

    Thank you so much!

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Feb 22, 2016 @ 09:26
    Ismail Mayat
    0

    Bob,

    I recently had to do this for v6 site i used https://our.umbraco.org/projects/backoffice-extensions/viziozrelationships/ you could as done in this package use relationships api. If you have created your other language sites via copying a language node and have checked relate on copy checkbox then in the relations table there will be a relationship and you can use the relations service to get related language instances of a page.

    Regards

    Ismail

  • Roberto Bianchi 137 posts 446 karma points c-trib
    Feb 26, 2016 @ 09:49
    Roberto Bianchi
    0

    Thank you so much Ismail, but I use Umbraco v7.0 :)

  • Jan Borup Coyle 7 posts 32 karma points
    Nov 01, 2016 @ 12:30
    Jan Borup Coyle
    1

    Hi

    Mr @Craig100 has made a great macro implementation here: https://our.umbraco.org/forum/extending-umbraco-and-using-the-api/76546-how-to-build-link-rel-alternate#comment-248053

    Best regards Jan

  • Roberto Bianchi 137 posts 446 karma points c-trib
    Nov 02, 2016 @ 08:50
    Roberto Bianchi
    0

    Hi and thank you for your reply.

    I tried the macro but it doesn't work for me.

    All the pages has only one lang tag, set as "x-default", also if I'd copied the pages from the same node...

  • Nathan Skidmore 63 posts 251 karma points c-trib
    Feb 17, 2017 @ 15:19
    Nathan Skidmore
    1

    If you have not setup relationships on your language variant nodes (i.e. you need to apply this to an existing multi-lingual website), relationships are not going to be the answer without some heavy content changes in the back-office.

    I managed to get to a solution for one of my sites with a Linq query. It does make a few assumptions. The first is that there is one root node, above the site root language nodes. The second is that alternative pages all have the same Node Name. The exception to this would be the root language node, which could be named UK, US, FR etc. Another assumption is that the general tree structure across different language sites is the same (levels).

    Here is the code, which is placed in the master template of the website. Hopefully someone else will find this useful.

    var currentNode = Model.Content;
    var rootNode = currentNode.AncestorOrSelf("docTypeAliasOfSiteRootNode");
    
    var alternatePages = rootNode.Descendants()
        .Where(x => x.DocumentTypeAlias == currentNode.DocumentTypeAlias &&
            x.Level == currentNode.Level &&
            x.Parent.DocumentTypeAlias == currentNode.Parent.DocumentTypeAlias &&
            (x.Parent.Name == currentNode.Parent.Name || x.Parent.DocumentTypeAlias == "docTypeOfRootLanguageNode") &&
            (x.Name == currentNode.Name || x.DocumentTypeAlias == "docTypeOfRootLanguageNode")).ToList();
    

    Then to populate the meta tags we would need something like this:

    @if (currentNode == rootNode)
    {
        <link rel="alternate" href="@currentNode.UrlAbsolute()" hreflang="x-default" />
        foreach (var languageNode in rootNode.Children().Where(x => x.DocumentTypeAlias == "docTypeOfRootLanguageNode"))
        {
            <link rel="alternate" href="@languageNode.UrlAbsolute()" hreflang="@languageNode.GetCulture().ToString()" />
        }
    }
    @if (alternatePages != null && alternatePages.Any())
    {
        if (alternatePages.Count == 1)
        {
            <link rel="alternate" href="@alternatePages.First().UrlAbsolute()" hreflang="x-default" />
        }
        else
        {
            foreach (var altPage in alternatePages)
            {
                <link rel="alternate" href="@altPage.UrlAbsolute()" hreflang="@altPage.GetCulture().ToString()" />
            }
        }
    }
    

    I hope someone else finds some use of this. I'm also open to suggestions on improving this code.

    Cheers,

    Nathan

  • Roberto Bianchi 137 posts 446 karma points c-trib
    Feb 17, 2017 @ 16:27
    Roberto Bianchi
    0

    Thank you for sharing your solution, Nathan.

    Unfortunately I can't use your code 'cause I have a root node for each language and I have different node names, because the node name create the url, and for SEO is better if the url is in the language of the page...

    Thank you the same :)

  • Nathan Skidmore 63 posts 251 karma points c-trib
    Feb 17, 2017 @ 16:55
    Nathan Skidmore
    0

    Hi Bob,

    The URL Name is a good point, however if you don't speak the language of the site you are supporting, it is very hard to navigate the content tree by naming the nodes in a different language. To reduce those administrative woes, you can use the property umbracoUrlName to override the URL with whatever name you want.

    If you don't have a root node, you would have to modify the linq query to handle your tree setup, but the principles would be mostly the same.

    Cheers,

    Nathan

  • Roberto Bianchi 137 posts 446 karma points c-trib
    Feb 17, 2017 @ 17:00
    Roberto Bianchi
    0

    Nice idea! Really useful! Thank you :)

  • Tom Lamphere 6 posts 75 karma points
    Dec 03, 2018 @ 19:33
    Tom Lamphere
    0

    Roberto- were you able to find a solution for this?

  • jakub hromada 8 posts 98 karma points
    Apr 11, 2019 @ 14:35
    jakub hromada
    0

    On my site I have two languages (French and English) and a root node for each language. My solution was to use Content Picker on my SEO tab that is on every page within my Umbraco installation. Content editor would then manually pick localized version of a page.

    Then on my master template I added the following:

    var ukAlternateLinkTag = "";
    var frenchAlternateLinkTag = "";
    
    if (CurrentPage.hreflang != null)
    {
        if (Culture == "fr-FR")
        {
            frenchAlternateLinkTag = "https://www.siteurl.fr" + CurrentPage.Url;
            ukAlternateLinkTag = CurrentPage.hreflang.Url;
        }
        else
        {
            frenchAlternateLinkTag = CurrentPage.hreflang.Url;
            ukAlternateLinkTag = "https://www.siteurl.co.uk" + CurrentPage.Url;
        }
    }
    
    @if (!string.IsNullOrWhiteSpace(frenchAlternateLinkTag) && !string.IsNullOrWhiteSpace(ukAlternateLinkTag))
    {
        <link rel="alternate" hreflang="en-US" href="@ukAlternateLinkTag" />
        <link rel="alternate" hreflang="fr-FR" href="@frenchAlternateLinkTag" />
    }
    

    Please note that the proper way doing this would be to use the Umbraco RelationService. However, to make this work, the content editor would need to create the language equivalent of a page by copying the page and and having "Relate to original" enabled.

Please Sign in or register to post replies

Write your reply to:

Draft