Copied to clipboard

Flag this post as spam?

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


  • k 256 posts 654 karma points
    Jan 19, 2020 @ 11:32
    k
    0

    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

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jan 20, 2020 @ 10:04
    Alex Skrypnyk
    0

    Hi Kusum

    Really great topic about multilanguage here - https://our.umbraco.com/forum/umbraco-8/95909-umbraco-v8-multilanguage

    Thanks,

    Alex

  • k 256 posts 654 karma points
    Jan 21, 2020 @ 05:29
    k
    0

    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

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jan 21, 2020 @ 12:07
    Alex Skrypnyk
    0

    Hi Kusum

    How were cultures organized in version 7?

    Alex

  • k 256 posts 654 karma points
    Jan 22, 2020 @ 10:23
    k
    0

    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

    enter image description here

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jan 22, 2020 @ 12:11
    Alex Skrypnyk
    0

    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.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jan 22, 2020 @ 13:01
    Dan Diplo
    100

    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!

  • k 256 posts 654 karma points
    Jan 24, 2020 @ 09:44
    k
    0

    Thanks for the reply Dan.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies