Here's how I do it in 8. I've created a Helper class to create a map of the current page and then all the language variants for it. It's a bit complex but hope you can gain something from the raw code:
First I have a custom class to hold the values:
public class CultureUrl
{
public string Culture { get; set; }
public string Url { get; set; }
public string Name => this.Culture.Substring(3, 2).ToUpper();
public bool Current { get; set; }
}
Then a helper class to perform the mapping:
public static class LanguageHelper
{
/// <summary>
/// Generates the list of languages for the page dropdown. If there is a parallel page using same culture then this is used, otherwise it falls back to the root URL.
/// </summary>
/// <param name="currentPage">The currently viewed page</param>
/// <param name="currentCulture">The current culture</param>
/// <returns>A list of languages and cultures</returns>
public static List<CultureUrl> GetCurrentLanguageList(IPublishedContent currentPage, string currentCulture, AppCaches caches)
{
var home = currentPage.HomePage();
var rootCultures = GetRootCultures(home, caches);
var pageCultures = currentPage.Cultures.Select(x => x.Value);
var mapped = new List<CultureUrl>(5);
foreach (var root in rootCultures)
{
var pageMatch = pageCultures.FirstOrDefault(c => c.Culture == root.Culture);
CultureUrl mappedPage = null;
if (pageMatch != null)
{
string url = currentPage.Url(pageMatch.Culture);
if (url != "#")
{
mappedPage = new CultureUrl()
{
Culture = pageMatch.Culture,
Url = url
};
}
}
if (mappedPage == null)
{
mappedPage = new CultureUrl()
{
Culture = root.Culture,
Url = home.Url(root.Culture)
};
}
mappedPage.Current = mappedPage.Culture.InvariantEquals(currentCulture);
mapped.Add(mappedPage);
}
return mapped;
}
private static IEnumerable<PublishedCultureInfo> GetRootCultures(IPublishedContent currentPage, AppCaches caches)
{
return caches.RuntimeCache.GetUmbracoCacheItem("RootCultures", () => currentPage.Cultures.Select(x => x.Value).ToList());
}
}
You can then call the helper in a view like this, passing in some context:
var languages = LanguageHelper.GetCurrentLanguageList(Model, Culture, AppCaches);
You can then build your list of language variant links like this:
@foreach (var item in languages)
{
<a href="@item.Url" title="@item.Culture">@item.Name</a>
}
Hopefully you can adapt the concepts to your own ends!
Language switch
Hello,
I want to be able to switch from french to english to its corresponding page.
Ex : https://www.mysite.com/fr/mon-article to switch to https://www.mysite.com/en/my-article
Is it possible in Umbraco 8 ?
Thanks for helping.
Kusum
Hi Kusum
Really great topic about multilanguage here - https://our.umbraco.com/forum/umbraco-8/95909-umbraco-v8-multilanguage
Thanks,
Alex
Thank you Alex.
I have another question please.
We have upgraded a website from version 7 to version 8.
I already had my french version and english version.
How do I apply culture without losing my content.
Can you please advise.
Thanks,
Kusum
Hi Kusum
How were cultures organized in version 7?
Alex
I am not sure I understand your question.
But in umbraco7 version, we created the FR version folder and then the EN version folder as below
Ah, clear, Truly said I don't know what is the easiest way to transfer this structure to v8 language variants.
It's a good question.
Here's how I do it in 8. I've created a Helper class to create a map of the current page and then all the language variants for it. It's a bit complex but hope you can gain something from the raw code:
First I have a custom class to hold the values:
Then a helper class to perform the mapping:
You can then call the helper in a view like this, passing in some context:
You can then build your list of language variant links like this:
Hopefully you can adapt the concepts to your own ends!
Thanks for the reply Dan.
is working on a reply...