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).
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");
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.
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); }
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:
http://our.umbraco.org/forum/developers/extending-umbraco/3884-get-the-hostname-language-settings
Best regards and thanks in advance :)
Kim
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:
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
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) "
Can you see what I'am doing wrong?
Best regards
Kim
Hi Kim, try this
Let me know if that works?
Jeavon
No, sorry - didn't work :(
Ah, got it
Absolutely fantastic!
Thank you very much!
Great! Of course this will only get the language from the first domain assigned to your homepage.
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
A workaround is to use a dictionary item, like this:
Best regards
Kim
Using below method, gives me error: Error loading Partial View script (file: ~/Views/MacroPartials/LanguageSwitcher.cshtml)
Using below method, gives me error: Error loading Partial View script (file: ~/Views/MacroPartials/LanguageSwitcher.cshtml)
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.
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
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:
That's a great solution, however, content.Ancestors().ToList() should be content.AncestorsOrSelf().ToList(), in case users are on the homepage.
is working on a reply...