Copied to clipboard

Flag this post as spam?

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


  • Levente Kosa 136 posts 352 karma points
    Dec 29, 2020 @ 12:50
    Levente Kosa
    0

    Strange behavior with children node url when website is multilingual

    Hi, I'm using v8.9.1 and I just noticed something very strange.

    I have a multilingual site with the following Culture and Hostnames settings on the Home page. enter image description here

    I'm listing Project children pages and the url is correct at first, e.g. http://localhost:52202/projects/ivy-tech-community-college/ but when I go to other languages, like http://localhost:52202/de and clicking on the same list, the url is not http://localhost:52202/de/projekt-archiv/ivy-tech-community-college/ but the same before, so http://localhost:52202/projects/ivy-tech-community-college/. Same problem with other languages.

    If I save any content in the back-office, and refresh the page when I'm on the German site, the url is http://localhost:52202/de/projekt-archiv/ivy-tech-community-college/ on the page, and now switch to other languages, e.g. back to English, the url remains German.

    The links are good when I visit the project children pages individually. enter image description here

    I'm listing children like this:

    var projects = Model.Root().Descendants().Where(x => x.IsDocumentType("projects")).FirstOrDefault(); 
    
    @foreach(var project in projects.Where(x => x.IsVisible())){
        <a href="@project.Url">@project.Name</a>
    }
    

    Any idea or suggestion? Is it a bug maybe?

    Additionally:

    I read this article (https://24days.in/umbraco-cms/2020/multilingual-websites-in-umbraco-8/) and in section "Generating HrefLang Tags" I added the code to generate hreflang tags:

    @foreach (var culture in Model.Cultures)
    {
        string url = Model.Url(culture: culture.Key, mode: UrlMode.Absolute);
    
        if (!string.IsNullOrEmpty(url) && url != "#")
        {
            <link rel="alternate" href="@Model.Url(culture: culture.Key, mode: UrlMode.Absolute)" hreflang="@culture.Key" />
        }
    }
    

    The tags have generated on every pages except on these Project children pages (e.g. http://localhost:52202/projects/ivy-tech-community-college/) when it gave me an error: enter image description here enter image description here

  • Bo Jacobsen 605 posts 2403 karma points
    Dec 29, 2020 @ 19:08
    Bo Jacobsen
    0

    Hi.

    I think Model.Cultures is a IReadOnlyDictionary<string, PublishedCultureInfo> So it would be culture.Value.Culture

    like

    @foreach (var culture in Model.Cultures)
    {
        string url = Model.Url(culture: culture.Value.Culture, mode: UrlMode.Absolute);
    
        if (!string.IsNullOrEmpty(url) && url != "#")
        {
            <link rel="alternate" href="@Model.Url(culture: culture.Value.Culture, mode: UrlMode.Absolute)" hreflang="@culture.Value.Culture" />
        }
    }
    
  • Levente Kosa 136 posts 352 karma points
    Dec 29, 2020 @ 19:40
    Levente Kosa
    0

    Thanks Bo, but it gives the same error. Actually this part of the code is not my main problem, but the this one:

    var projects = Model.Root().Descendants().Where(x => x.IsDocumentType("projects")).FirstOrDefault(); 
    
    @foreach(var project in projects.Where(x => x.IsVisible())){
        <a href="@project.Url">@project.Name</a>
    }
    

    I don't know why the url is the same when I change language.

  • Bo Jacobsen 605 posts 2403 karma points
    Dec 29, 2020 @ 19:57
    Bo Jacobsen
    0

    You will always get the url of your main language. So you need to specify the culture like project.Url(culture: "fr-FR") but if you are using FirstOrDefault() you only get returned one project or null.

    If you need the current culture then you can use Model.GetCultureFromDomains(), but what i think you need is this:

    @foreach (var project in Model.Root().DescendantsOfType("projects").Where(x => x.IsVisible()))
    {
        <a href="@project.Url(culture: "fr-FR")">@project.Name("fr-FR")</a>
    }
    

    or

    @foreach (var project in Model.Root().DescendantsOfType("projects", "fr-FR").Where(x => x.IsVisible()))
    {
        <a href="@project.Url(culture: "fr-FR")">@project.Name("fr-FR")</a>
    }
    
  • Bo Jacobsen 605 posts 2403 karma points
    Dec 29, 2020 @ 20:10
    Bo Jacobsen
    100
    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        Layout = null;
    
        var currentCulture = Model.GetCultureFromDomains();
        var projects = Model.Root().DescendantsOfType("projects", currentCulture).Where(x => x.IsVisible());
    }
    
    @foreach (var project in projects)
    {
        <a href="@project.Url(culture: currentCulture)">@project.Name(currentCulture)</a>
    }
    
  • Levente Kosa 136 posts 352 karma points
    Dec 29, 2020 @ 21:06
    Levente Kosa
    0

    Thank you, just what I was looking for!

Please Sign in or register to post replies

Write your reply to:

Draft