Copied to clipboard

Flag this post as spam?

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


  • Lars Lj 13 posts 32 karma points
    Nov 05, 2014 @ 11:49
    Lars Lj
    0

    Getting the content of <html lang> tag in Umbraco razor

    Hi. How can I get the content of the tag <html lang="en"> dynamically? I want to have a tag like this

    var languageCulture = "en" in case of the html lang tag equals "en" and so on! What razor can I use to get that content?

    Any help highly appreaciated :-)ยจ

    //Lars

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 05, 2014 @ 11:57
    Alex Skrypnyk
    0

    Hi Lars,

    You can use jQuery for that. You can't use razor for get that, Razor is for setting that markup.

    Thanks

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 05, 2014 @ 12:03
    Dennis Aaen
    0

    Hi Lars and welcome to our,

    As Alex says you can use jQuery to get the value of the langague attribute on the html tag.

    var languageCulture = $('html').attr('lang'); 
    alert
    (
    languageCulture);

    Hope this helps,

    /Dennis

  • Lars Lj 13 posts 32 karma points
    Nov 06, 2014 @ 08:28
    Lars Lj
    0

    Hi Guys

    I thought that I could get in Razor. So thank you for your help :-)

    //Lars

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 06, 2014 @ 09:01
    Jan Skovgaard
    0

    Hi Lars

    May I ask what your scenario for wanting to fetch the value is? What do you need to achieve by looking at the lang attribute? Is it some script behaviour, CSS or something that involves the server?

    The proposed solution is good if you want to manipulate the DOM but if you need to render something serverside-wise you need another approach.

    Just curious :)

    /Jan

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Nov 06, 2014 @ 09:29
    Chriztian Steinmeier
    1

    Hi all,

    Ideally, you have a MasterLayout.cshtml file that actually writes that attribute (by looking up the current culture) if your site is multilingual. So whichever method you're using there should be applicable in any other Razor view.

    I'm an XSLT guy, so I'm doing this a little bit different, but I'm sure there is a way in C# to get the current culture's language code.

    /Chriztian

  • Lars Lj 13 posts 32 karma points
    Nov 06, 2014 @ 09:42
    Lars Lj
    0

    What a fantastic community with so many helpful people :-)

    I have an Umbraco solution where I need to have the site in 2 languages. The pure translation part with Umbraco Dictonary Items is easy. But I have to hide some parts of the page when the language shifts. As Dennis pointed out one would do this with JavaScript. However it could be fine to use it in Razor as well like this

    RAZORSCRIPT:

    var myLanguageVariable = get the current language

    if(myLanguageVariable == "en") {... show this part of the page.}

    if(myLanguageVariable == "de") {.. don't show this part of the page }

    --------------

    // Lars

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 06, 2014 @ 11:05
    Jan Skovgaard
    0

    Hi Lars

    I'd say that if you need to hide something based on language then you should go for a serverside solution rather than a client side solution.

    As Chriztian also points out it really should be set in the master template based on the site culture.

    I'm not too experienced dealing with it in Razor either but perhaps you can find some inspiration in this post http://stackoverflow.com/questions/19495980/get-culture-display-name-in-its-language or simply googling a bit more about to get the culture name using Razor :)

    /Jan

  • Lars Lj 13 posts 32 karma points
    Nov 10, 2014 @ 15:43
    Lars Lj
    0

    Thanks, Jan. It works with JQuery

    //Lars

Please Sign in or register to post replies

Write your reply to:

Draft