Copied to clipboard

Flag this post as spam?

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


  • mohd iliyaj 8 posts 77 karma points
    Nov 06, 2023 @ 10:55
    mohd iliyaj
    0

    Design the language dropdown using the Umbraco language varients

    Hello Everyone,

    I'm designing a umbraco project and I want one dropdown in navbar with different languages when user selects a language content should be changed to selected language, If user return to previous language then content should be revert to previous language. Can anyone help to resolve this issue as much as possible, Please don't share any document because I have gone through all the document but I can't find the solution. Please provide me some steps to resolve this problem.

    Thank you,

    Iliyaj

  • Huw Reddick 1770 posts 6157 karma points MVP c-trib
    Nov 06, 2023 @ 11:43
    Huw Reddick
    0

    this should work. It renders a dropdown.

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<IPublishedContent>
    @using System.Globalization;
    @using Umbraco.Cms.Core.Models
    @using UmbracoProject.Core.Models
    
    @{
        var currentPage = Model.Url();
        var cultureList = Model.Cultures.ToList();
        var languagePickerLinks = new List<CultureModel>();
    
        @foreach (var culture in cultureList) {
            var langName = CultureInfo.CreateSpecificCulture(culture.Value.Culture);
    
            var link = new CultureModel {
                Url = Model.Url(culture.Value.Culture),
                Culture = culture.Value.Culture,
                LangName = langName.NativeName.Split(" ")[0]
            };
    
            languagePickerLinks.Add(link);
        }
    
        if (cultureList.Count > 1) {
            <div class="lang-switch">
                @{
                    <select id="lang-switch"  asp-for="@currentPage" onchange="if (this.value) window.location.href=this.value">
                        @foreach (var culture in languagePickerLinks) {
                            <option value="@culture.Url">@culture.LangName</option>
                        }
                    </select>
                }
            </div>
        }
    }
    

    The CultureModel is

    public class CultureModel {
    
        public string? Url { get; set; }
        public string? Culture { get; set; }
        public string? LangName { get; set; }
    
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft