Copied to clipboard

Flag this post as spam?

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


  • Rocio Kostritsyn 1 post 71 karma points
    Aug 03, 2022 @ 20:03
    Rocio Kostritsyn
    0

    Switch from one Multilanguage profile from another... How?

    Hi,

    I apologize in advance if this might seem a silly question. Let's even though I an Umbraco oldie, I am a newbie to the latest version after 8. I am trying to implement a test case to present to my boss to use the Multilanguage alternative for our Website. after getting around a few document types, and templates, and pages, I froze at the idea of how to would the user switch from one language environment to the other. We currently have a dup website for each language and we use a button. So I was wondering if I were to use the button, how would I tell Umbraco now the user wants to go to the Spanish version of this English page.

    Thank you in advance and apologies for the stupidity.

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Aug 04, 2022 @ 05:20
    Huw Reddick
    1

    Just set the culture/uiculture to the language they select. I'll post example when I'm at my computer

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Aug 04, 2022 @ 11:48
    Huw Reddick
    2

    Hi Rocio, as promised, some examples. Currently I set the Thread culture info to the language they have selected, although not sure that is strictly required, but it helps with internal code stuff when you need the culture. You may be able to accomplish the same just with a session variable or a querystring parameter.

    At the top of my master template, I check for a session variable (this is set when they select a different language from a dropdown of available languages) and set the Thread culture.

    if (Context.Session.Keys.Contains("Culture"))
    {
        var cultureString = Context.Session.GetString("Culture").Replace("\"", "");
        Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureString );
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureString );
    }
    

    At the bottom of my master template I then call a javascript function, passing it the CurrentUICulture language code SetCulture('@Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName');

    SetCulture function, checks the language code and sets the correct url

        //switch templates based on the current TwoLetterISOLanguageName
    SetCulture = function(lang) {
        //check if using correct url template
        var url = location.href;
        if (/(\/\w{2}\/)/.test(url)) {
            if (lang === "en") {
                location.href = url.replace(/(\/\w{2}\/)/gm, "");
            } else {
                location.href = url.replace(/(\/\w{2}\/)/gm, "/"+ lang + "/");
            }
    
        } else {
            var newurl = new URL(url);
            var urlstr = "/" + lang + newurl.pathname;
            location.href = urlstr;
        }
    }
    

    In my header I have a ViewComponent which displays the various countries and currencies that can be selected, but basically when they select a flag, a javascript function calls a surfacecontroller which sets the culture and session variable to the select language.

        $(document).on('click', '.flag', function (e) {
    
            ChangeCulture($(this).data("id"));
        });
    
    ChangeCulture = function (lang) { 
        $.ajax({
            url: "/umbraco/surface/clientsurface/changecurrentculture",
            type: "GET",
            dataType: "html",
            async: true,
            data: {"language":lang }
            , success: function (data) {
                if (data) {
                    SetCulture(lang);
                    $("#drp_currency").val(data);
                } else {
                    console.log('error: no data returned');
                }
            }, error: function (xhr) {
                console.log('Error: ', xhr.status + ' (' + xhr.statusText + ')');
            }
        });
        return "";
    }    
        }
    

    SurfaceCntroller method

        public JsonResult ChangeCurrentCulture(string language)
        {
            if (language == "en-GB")
            {
                language = "en";
            }
            var newCulture = new CultureInfo(language, false);
            _httpContext.HttpContext.Session.SetString("Culture",JsonConvert.SerializeObject(newCulture));
    
            return Json("success");
        }
    

    Hope this helps.

Please Sign in or register to post replies

Write your reply to:

Draft