Copied to clipboard

Flag this post as spam?

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


  • Henrik Vincent 122 posts 616 karma points
    Apr 11, 2019 @ 09:54
    Henrik Vincent
    0

    Showing active language in languagepicker

    Hi guys

    Doing a languagepicker for a multilingual site, and it's almost working as wanted.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @if(Model.Root().HasProperty("countryLanguage") && Model.Root().HasValue("countryLanguage")){
    
    <div class="languagepicker roundborders large">
    @foreach (var (culture, infos) in Model.Cultures)
    {
        var root = Model.Root();
        var language = root.Value("countryLanguage", culture);
        var flag = root.Value<IPublishedContent>("countryFlag", culture);
    
    <a href="@Model.GetUrl(culture)"><li><img src="@flag.Url"/>@language</li></a>
    }
    </div>
    }
    

    My only problem, is that I would like it to show the active language as the first language.

    At them moment, not matter which language layer I'm on, it always shows English first.

    Any suggestions on how to achieve this?

    Best,

    Henrik

  • Corné Strijkert 80 posts 456 karma points c-trib
    Apr 11, 2019 @ 10:11
    Corné Strijkert
    100

    Try this:

    <div class="languagepicker roundborders large">
        @{
            var root = Model.Root();
            var currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
            var currentFlag = root.Value<IPublishedContent>("countryFlag", currentCulture.Name);
        }
    
        <a href="@Model.GetUrl(currentCulture.Name)">
            <li>
                <img src="@currentFlag.Url" />@root.Value("countryLanguage", currentCulture.Name)
            </li>
        </a>
    
        @foreach (var (culture, infos) in Model.Cultures.Where(x => x.Key.ToLower() != currentCulture.Name.ToLower()))
        {
            var language = root.Value("countryLanguage", culture);
            var flag = root.Value<IPublishedContent>("countryFlag", culture);
    
            <a href="@Model.GetUrl(culture)"><li><img src="@flag.Url" />@language</li></a>
        }
    </div>
    

    The above code splits the current language from the other languages by adding a Where() statement to the foreach:

    Model.Cultures.Where(x => x.Key.ToLower() != currentCulture.Name.ToLower())
    

    Also I think it's not necessary to set the language names on your document type. You can use the culture info or use the Umbraco localization services to retrieve the language display name.

  • Henrik Vincent 122 posts 616 karma points
    Apr 11, 2019 @ 10:54
    Henrik Vincent
    0

    Thanks a bunch Corné!

    It's working now, besides the flag for the current language.

    You added <img src="@Model.Url"/> as the URL for the current language, which gets the page URL, rather than the image URL.

    Got any idea on how to retrieve the image URL?

    Henrik

  • Corné Strijkert 80 posts 456 karma points c-trib
    Apr 11, 2019 @ 11:04
    Corné Strijkert
    0

    Ah, my bad!

    I have edited the code example above.

    I think it's a good idea to refactor the code a bit further, but it shows how you can achieve that the current language is always the first one in the list.

  • Henrik Vincent 122 posts 616 karma points
    Apr 11, 2019 @ 11:27
    Henrik Vincent
    1

    Perfect!

    Works like a charm.

    Thanks a bunch for your help :)

    Have a great day

    Henrik

Please Sign in or register to post replies

Write your reply to:

Draft