Copied to clipboard

Flag this post as spam?

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


  • Mathias Valentin 60 posts 208 karma points
    Feb 23, 2015 @ 12:06
    Mathias Valentin
    0

    Getting the language property with Razor

    Hi all,

    How do I get the language id (chosen from culture/hostnames in the content tree) into the html lang attribute with Razor?

     <html lang=''>

     

    Thanks

  • Sören Deger 733 posts 2844 karma points c-trib
    Feb 23, 2015 @ 12:35
    Sören Deger
    0

    Hi Mathias,

    I can get the culture alias with this code:

    var language = umbraco.library.GetCurrentDomains(Model.Content.Id)[0].Language;
    string cult = language.CultureAlias.Substring(0, 2).ToUpper();


    Maybe this can help you?

     

    Best,
    Sören

  • Mathias Valentin 60 posts 208 karma points
    Feb 23, 2015 @ 12:58
    Mathias Valentin
    0

    Thanks, but I'm getting an error :/

    Object reference not set to an instance of an object.
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 23, 2015 @ 13:05
    Dennis Aaen
    0

    Hi Mathias,

    Try to see my comment in this: https://our.umbraco.org/forum/using/ui-questions/57860-Getting-the-content-of-html-lang-tag-in-Umbraco-razor, you can get the language property with jQuery.

    Hope this helps,

    /Dennis

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 23, 2015 @ 13:12
    Jan Skovgaard
    6

    Hi Mathias

    If I understand you correctly you want to populate the lang attribute with the chosen culture?

    If you want to render it then try simply writing lang="@Culture".

    Does that work for you?

    /Jan

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 23, 2015 @ 13:16
    Dennis Aaen
    0

    Hi Jan,

    Ah yeah I totally forgot this option :-) on how you can set the language property.

    /Dennis

  • Sören Deger 733 posts 2844 karma points c-trib
    Feb 23, 2015 @ 13:34
    Sören Deger
    0

    Hi Jan,

    great :-) This is very simple!

    Sören

  • Mathias Valentin 60 posts 208 karma points
    Feb 23, 2015 @ 13:38
    Mathias Valentin
    103

    Awesome! Now I feel kind of stupid, how did I miss @Culture :/ The complete solution for me was:

    <html lang="@Culture">


    @jan where can I find these properties? as they don't appear on the cheat-sheet. 

    Thanks everybody! 

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Mar 18, 2016 @ 11:05
    Chriztian Steinmeier
    2

    Hi Mathias,

    Just out of curiosity - why do you only pick the two initial letters ("da" instead of the complete "da-DK")?

    I know danish doesn't exist in any other versions, but e.g. English has more than a couple of different versions, so wouldn't you ideally just include them, if the particular site has a specific version?

    /Chriztian

  • Mathias Valentin 60 posts 208 karma points
    Mar 18, 2016 @ 12:56
    Mathias Valentin
    0

    Oh yes - curiosity is great and I see your point :) I'm however following the w3 specification on language tags and since this particular site isn't going to have multiply regional versions in the same language - it's using the shortest possible version "da", "en" etc.

    The golden rule when creating language tags is to keep the tag as short as possible. Avoid region, script or other subtags except where they add useful distinguishing information. For instance, use ja for Japanese and not ja-JP, unless there is a particular reason that you need to say that this is Japanese as spoken specifically in Japan.

    https://www.w3.org/International/geo/html-tech/tech-lang.html#ri20030112.224717800

    But I now realized that Umbraco has region-less languages - both Danish (da-DK) and Danish (da) are available in the language selection list. Dammit! This means the substring method is unnecessary. I'll be updating the answer above. Thanks for the curiosity Chriztian ;)

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 23, 2015 @ 13:41
    Jan Skovgaard
    0

    Hi Mathias

    Happy you figured it out :)

    Well, I actually just stumbled upon it on a project where I needed the same thing and it's seems to be a very native thing - I just googled Razor C# culture.

    Not everything is in the cheat-sheet but is just native C# stuff.

    /Jan

  • Mathias Valentin 60 posts 208 karma points
    Feb 23, 2015 @ 14:04
    Mathias Valentin
    0

    I see - Razor is still very new to me, so I didn't even think this could be a native thing :)

    Btw: I marked my own complete answer as Solved for others who might be having a simular question. I now realise that this gave my karma a good boost :( Think I misinterpreted the function since it was you who solved the puzzle :/ 

     

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Mar 18, 2016 @ 11:06
    Chriztian Steinmeier
    0

    (Don't worry about "solving" your own questions - you won't actually get that Karma-boost. It just looks like it :-)

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 23, 2015 @ 14:18
    Jan Skovgaard
    1

    Hi Mathias

    Don't worry about that - If people go straight to your answer they still get a good solution.

    Yeah that's the thing with Razor...basically it's a subset of C#...so much C# stuff will just work :)

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft