Copied to clipboard

Flag this post as spam?

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

  • David Gregory 73 posts 199 karma points
    May 10, 2019 @ 15:56
    David Gregory

    1:1 Language switcher Variants

    Hi guys

    I'm trying to get a language switcher to work on a 1:1 bilingual site on v8.

    So as an example I have this article with the urls:



    Variants and cultures all set up and working nicely I just need to have a button that will toggle the page from one language to the other.

    Looking at this post I have this test code:

        @foreach (var (culture, info) in Model.Cultures)
                <a href="/@info.UrlSegment">@info.Name</a>

    But this returns me the urls:



    As you can see UrlSegment only gives me the last part of the url. What am I missing to get the fuller path?


  • christian Boutet 10 posts 100 karma points
    May 10, 2019 @ 18:32
    christian Boutet

    Hi, here our language switcher in Umbraco 8. Hope it help!

        @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
            var page = UmbracoContext.PublishedRequest.PublishedContent;
            var currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
            var availableLanguages = Services.LocalizationService.GetAllLanguages().Where(l => page.Cultures.Keys.Contains(l.IsoCode));
            var currentlanguage = availableLanguages.GetLanguageFromIsoCode(currentCulture.Name);
        <div class="dropdown" data-controller="DropdownController">
            <button class="btn btn-link btn-xs dropdown-toggle" type="button" id="languageSelectorButton">
                <div class="btn-icon">
                    <div class="dropdown-toggle-btn-icon icon-chevron-right"></div>
            <ul class="dropdown-menu dropdown-menu-left">
                @foreach (var (culture, infos) in page.Cultures.Where(c => c.Key.ToLower() != currentCulture.Name.ToLower()))
                    var language = availableLanguages.GetLanguageFromIsoCode(culture);
                    <li><a class="btn btn-link-thin btn-xs text-right" href="@page.GetUrl(culture)">@(language)</a></li>
    extension method is:
            public static string GetLanguageFromIsoCode(this IEnumerable<ILanguage> languages, string isoCode) {
                return languages.FirstOrDefault(l => l.IsoCode.ToLower() == isoCode.ToLower())?.CultureInfo?.NativeName ?? isoCode;
  • David Gregory 73 posts 199 karma points
    May 11, 2019 @ 19:13
    David Gregory

    Ah, thanks.

    I didn't know you could pass in a culture to get url. So I can basically change my code to use @Model.GetUrl(culture) instead and it works.

    @foreach (var (culture, info) in Model.Cultures)
            <a href="@Model.GetUrl(culture)">@info.Name</a>
  • Sachin 19 posts 49 karma points
    Sep 12, 2019 @ 09:17

    Hi, I am new to Umbraco, can you please let me know where and how should I create extension method that you have mentioned in your above example: public static string GetLanguageFromIsoCode(this IEnumerable

    Regards, Sachin

  • David Gregory 73 posts 199 karma points
    May 13, 2019 @ 09:11
    David Gregory

    Further thinking on this.

    For my site which has 2 languages all I need to do for a 1:1 lang switch is use the Dictionary feature in Umbraco like this:

    enter image description here

    and then have a single line of code like this to display the switch.

    <a href="@Model.GetUrl(Umbraco.GetDictionaryValue("CultureParam"))">@Umbraco.GetDictionaryValue("SwitchLanguage")</a>

    Does anyone think this is a bad/good way of doing it?

  • Tom 34 posts 154 karma points
    Apr 01, 2020 @ 19:45

    Hello David, maybe you could check out this article about settings up a language variants switch in Umbraco 8:

    Hope it helps you out!

Please Sign in or register to post replies

Write your reply to: