Copied to clipboard

Flag this post as spam?

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


  • Kim Bantz Rasmussen 81 posts 310 karma points
    Jul 09, 2013 @ 16:02
    Kim Bantz Rasmussen
    0

    Get language in Razor?

    Hi,

    I'm working on a multilanguage website, and I'am wondering if it's possible to get the language code in Razor? I find this from 2009: 

    umbraco.library.GetCurrentDomains(nodeId)[0]

     

    http://our.umbraco.org/forum/developers/extending-umbraco/3884-get-the-hostname-language-settings

    Best regards and thanks in advance :)

    Kim

     

     

     

     

     

     

     

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jul 10, 2013 @ 16:52
    Jeavon Leopold
    0

    Hi Kim,

    I've just had a dig around the Umbraco source to see if there was anything new for this but it appears that currently using the umbraco.library is still the best approach.

    You can access the Domains class directly if you need any other methods (this is basically what the library method is doing), e.g:

    @using umbraco.cms.businesslogic.web;
    @{
     var firstDomainLanguage = Domain.GetDomainsById(Model.Content.AncestorOrSelf(1).Id)[0].Language;

     I did find a really nice looking DomainHelper in the Umbraco.Web.Routing namespace but all of the methods are currently internal so no use (yet).

    Thanks,

    Jeavon

  • Kim Bantz Rasmussen 81 posts 310 karma points
    Jul 11, 2013 @ 08:57
    Kim Bantz Rasmussen
    1

    Hi Jeavon,

    Thank you for your reply - but I couldn't get it to work :(

    There's no errors in the code, but I get the error on the frontend "Error loading MacroEngine script (file: Language.cshtml) "

    @inherits umbraco.MacroEngines.DynamicNodeContext 
    @using umbraco.cms.businesslogic.web;

    @{
    var firstDomainLanguage = Domain.GetDomainsById(Model.Content.AncestorOrSelf(1).Id.Language);
    }

    @firstDomainLanguage

    Can you see what I'am doing wrong? 

    Best regards 
    Kim 

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jul 11, 2013 @ 09:24
    Jeavon Leopold
    0

    Hi Kim, try this

    @inherits umbraco.MacroEngines.DynamicNodeContext 
    @using umbraco.cms.businesslogic.web;

    @{
    var firstDomainLanguage =Domain.GetDomainsById(Model.AncestorOrSelf(1).Id)[0].Language;
    }

    @firstDomainLanguage

    Let me know if that works?

    Jeavon

  • Kim Bantz Rasmussen 81 posts 310 karma points
    Jul 11, 2013 @ 09:35
    Kim Bantz Rasmussen
    0

    No, sorry - didn't work :(

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jul 11, 2013 @ 10:08
    Jeavon Leopold
    2

    Ah, got it

    @inherits umbraco.MacroEngines.DynamicNodeContext 
    @using umbraco.cms.businesslogic.web; 
    
    @{ 
        var firstDomainLanguage = Domain.GetDomainsById(Model.AncestorOrSelf(1).Id)[0].Language;
    } 
    
    @firstDomainLanguage.CultureAlias;

     

  • Kim Bantz Rasmussen 81 posts 310 karma points
    Jul 11, 2013 @ 10:16
    Kim Bantz Rasmussen
    1

    Absolutely fantastic!

    Thank you very much!

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jul 11, 2013 @ 10:19
    Jeavon Leopold
    0

    Great! Of course this will only get the language from the first domain assigned to your homepage.

  • Kim Bantz Rasmussen 81 posts 310 karma points
    Jul 11, 2013 @ 10:42
    Kim Bantz Rasmussen
    0

    Okay, I see that ... I guess it's easier to make a dropdown at each language start node for now. That's a shame.

    Thank you for your help 

  • Kim Bantz Rasmussen 81 posts 310 karma points
    Jul 11, 2013 @ 17:01
    Kim Bantz Rasmussen
    104

    A workaround is to use a dictionary item, like this:

     

    @{
    var language = @Dictionary["language"];
    }

    @if (@language == "da") {
    <div>Language is Danish</div>
    }
    else {
    <div>Language is English</div>
    }

    Best regards
    Kim 

  • Filip Witkowski 4 posts 24 karma points
    Dec 10, 2014 @ 18:15
    Filip Witkowski
    0

    Using below method, gives me error: Error loading Partial View script (file: ~/Views/MacroPartials/LanguageSwitcher.cshtml)

    @inherits umbraco.MacroEngines.DynamicNodeContext 
    @using umbraco.cms.businesslogic.web;

    @{
       
    var firstDomainLanguage =Domain.GetDomainsById(Model.AncestorOrSelf(1).Id)[0].Language;
    }

    @firstDomainLanguage.CultureAlias;
  • Filip Witkowski 4 posts 24 karma points
    Dec 10, 2014 @ 18:16
    Filip Witkowski
    0

    Using below method, gives me error: Error loading Partial View script (file: ~/Views/MacroPartials/LanguageSwitcher.cshtml)

    @inherits umbraco.MacroEngines.DynamicNodeContext 
    @using umbraco.cms.businesslogic.web;

    @{
       
    var firstDomainLanguage =Domain.GetDomainsById(Model.AncestorOrSelf(1).Id)[0].Language;
    }

    @firstDomainLanguage.CultureAlias;
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 10, 2014 @ 18:37
    Dennis Aaen
    0

    Hi Flip.

    To me it looks like you are using MVC razor and not the old DynamicNode razor which is also not recommeded to use any more.

    But if you have a multilingual websites in the same single installation of Umbraco I think that you could use this snippet of code to create a langauge switcher, in Razor for your frontend of your site. In the example below I am using the strongly typed version of Razor.

    @{
       
    var homePages = Umbraco.TypedContentAtRoot().Where(x => x.DocumentTypeAlias == "umbHomePage");
     
           
    <ul>
               
    @foreach(var page in homePages){
                   
    <li>
                       
    <a href="@page.Url">
                           
    @page.Name
                       
    </a>
                    </
    li>
               
    }
           
    </ul>
    }

    This will make a list of all your nodes using the document type "umbHomePage" and pull out the name of the node and a link to it. Remember to change the "umbHomePage" so it mach the alias of your document type for your hompage document type.

    Hope this helps,

    /Dennis

  • Calle Bjernekull 28 posts 103 karma points
    Apr 16, 2015 @ 11:06
    Calle Bjernekull
    0

    Hi, in 7.2.4 the GetCulture-extension is public, (http://issues.umbraco.org/issue/U4-3753), but it still doesn't solve the problem. You still need to have separate domains to make it work. All because it ignores wildcard-domains. Which I suppose is the case for the thread-starter. Have a look at  Umbraco.Web.Routing.DomainHelper.DomainForNode(int nodeId, Uri current)

    However, I found a pretty easy solution for this:

    public static CultureInfo GetCulture(IPublishedContent content)
    {
    List<IPublishedContent> ancestors = content.Ancestors().ToList();
    Domain[] domains = Domain.GetDomains(true).ToArray<Domain>();
    foreach(IPublishedContent cnt in ancestors)
    {
    Domain nodeDomain = domains.Where(d => d.RootNodeId == cnt.Id).FirstOrDefault();
    if(nodeDomain != null)
    return new CultureInfo(nodeDomain.Language.CultureAlias);
    }
    return new CultureInfo(domains.First().Language.CultureAlias);
    }
  • Brandon Osborne 38 posts 161 karma points c-trib
    Apr 12, 2016 @ 04:23
    Brandon Osborne
    0

    That's a great solution, however, content.Ancestors().ToList() should be content.AncestorsOrSelf().ToList(), in case users are on the homepage.

Please Sign in or register to post replies

Write your reply to:

Draft