What is the best way to impliment a language drop ...
# help-with-umbraco
j
Hello, i need to impliment multilangue but on the front end i need to change the language by clicking a button? What is the best approach for this?
c
Do you have your multilingual site already and just need the language switcher? (which is mentioned but not covered in the docs AFAICS)
j
AFAICS?
a
As far as i (know)? 😉
But you can use the ilanguageservice to get the installed languages
Or the domainservice to get the assigned domains for your current root
j
@Ambert can get the assigned domains but how do i get the urls for the language?
a
I assume you have setup a domain per language? For example, www.mysite.com/en?
I have a piece of code spmewhere
c
AFAICS = As Far As I Can See 😉
There's no doubt a better way, but this works for me as a partial. It renders a dropdown but shouldn't be too hard to convert to buttons:-
Copy code
@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<CultureLinkModel>();
    
    @foreach (var culture in cultureList) {
        var langName = CultureInfo.CreateSpecificCulture(culture.Value.Culture);
        
        var link = new CultureLinkModel {
            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 CultureLinkModel is just:-
Copy code
namespace UmbracoProject.Core.Models; 

public class CultureLinkModel {
    
    public string? Url { get; set; }
    public string? Culture { get; set; }
    public string? LangName { get; set; }

}
Hope that helps.
j
Thanks @Craig100
5 Views