Copied to clipboard

Flag this post as spam?

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


  • Roberto Bianchi 137 posts 446 karma points c-trib
    Dec 09, 2016 @ 17:24
    Roberto Bianchi
    0

    Change culture and hostname in razor

    Hello, how can I change culture and hostname of an Umbraco node using razor?

    I tried using this code:

    @using System.Globalization
    
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR");
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR");
    

    But, for example, the dictionary items of this node are in english, instead of french.

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Dec 10, 2016 @ 00:22
    Marc Goodson
    1

    Hi Bob

    Yes you can set the Culture and UICulture for the 'WebPageRenderingBase' class used by your razor view. Umbraco Dictionary works off of the UICulture.

    You could set this in your razor view by just having:

    @{
    Culture = UICulture = "fr-FR";
    }
    <h2>@Umbraco.GetDictionaryValue("hello")</h2>
    

    and this would output

    Bonjour

    regards

    Marc

  • Roberto Bianchi 137 posts 446 karma points c-trib
    Dec 12, 2016 @ 08:24
    Roberto Bianchi
    0

    Hello marc and thank you for your response.

    I tried your code but the dictionary item still remains in english...

  • Roberto Bianchi 137 posts 446 karma points c-trib
    Dec 12, 2016 @ 15:23
    Roberto Bianchi
    0

    Sorry Marc, the page remained in english because of the cache.

    Now I receive a compiler error CS0103:

    The name 'Culture' does not exist in the current context.

    And I still use this namespace:

    @using System.Globalization
    

    ... strange ...

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Dec 12, 2016 @ 18:39
    Marc Goodson
    1

    Hi Bob

    veryy strange... my whole razor file is just:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "Master.cshtml";
    }
    @{ 
        Culture = UICulture = "fr-FR";
    }
    <h2>@Umbraco.GetDictionaryValue("test")</h2>
    

    what is the model for your view ?

  • Roberto Bianchi 137 posts 446 karma points c-trib
    Dec 13, 2016 @ 08:23
    Roberto Bianchi
    0

    I use this code on an helper method.

    @using System.Globalization
    
    @helper change()
    {
        var cookieLingua = "en-US";
    
        if(Request.Cookies["lingua"]!=null)
        {
            cookieLingua = Request.Cookies["lingua"].Value;
        }
    
        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookieLingua);
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookieLingua);
    }
    

    And now it works... I had the dictionary item in the language that I take from the cookie.

    If I add your code, I receive the previous error.

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Dec 13, 2016 @ 13:15
    Marc Goodson
    100

    Hi Bob

    Yes, my suggestion will only work in a Razor View, as it is the context of the View that enables you to set the underlying culture in this way.

    You could handle changing the language, by putting something like this in /Views/_ViewStart.cshtml and you'd trigger a languag change by requesting ?lang=Fr-fr on a url / set and read from your cookie here:

        @using System.Globalization
        @using System.Web.Services.Protocols
        @{
            Layout = null;
            var lang = "en-GB";
            if (!Request["lang"].IsEmpty())
            {
                lang = Request["lang"];
                // check if valid culture
                if (CultureInfo.GetCultures(CultureTypes.SpecificCultures).Any(f=>f.TwoLetterISOLanguageName == lang || f.Name.ToLower() == lang.ToLower()))
                {
    // set cookie...
                    Culture = UICulture = lang;
                }
    
            }
    else {
            // is cookie set
    // get lang from cookie
    // set culture
    Culture = UICulture = lang;
    
    
    }
    
        }
    

    if that makes sense ?

  • Roberto Bianchi 137 posts 446 karma points c-trib
    Dec 13, 2016 @ 13:43
    Roberto Bianchi
    0

    Yup, this is another interesting solution :) thank you

Please Sign in or register to post replies

Write your reply to:

Draft