How to find the language / culture of an IPublishedContent object
Hi all,
Sorry for the basic question but I cannot seem to figure it out myself. How do I find the CultureInfo of an IPublishedContent object?
If there is not language property, how do you query against the Culture / Language of items? (I am talking about querying through the Umbraco helper, not through Lucene)
The documentation doesn't really mention it so I'm guessing that I am doing it wrong and that I should be looking elsewhere (?)
The solution I'm looking for cannot depend on the structure of the site tree (i.e. I don't want to assume that all IPublishedContent items below a certain item all have the same culture). In other words, solutions that would assume that everything under certain root element share the same culture don't work.
UmbracoContext.Current.PublishedContentRequest.PublishedContent
.AncestorsOrSelf(1) //This doesn't work for me
.Children(...)
I was really looking for a solution like :
UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
var items = helper.TypedContentAtRoot()
.Children("myContentType")
.Where(/* Some magical language predicate */)
The binary operator Subtract is not defined for the types 'System.Func`2[Umbraco.Web.Models.DynamicPublishedContent,System.Object]' and 'System.Func`2[Umbraco.Web.Models.DynamicPublishedContent,System.Object]'
So the "-" seems to be causing problems. I tried to escape it with .Where("Culture == \"en-US\"") but it doesn't work.
I also tried .Where("Culture.StartsWith(\"en\")") and it returns nothing even though I have items with the en-US culture. I'm not sure what i'm doing wrong here.
Instead of passing a string predicate, would you know how to express the same thing in a Func<IPublishedContent, bool> predicate?
Thanks again for your help, but no, it still throws the same exception when I go with
var items = helper.ContentAtRoot()
.Children("myContentType")
.Where("Culture == en-US");
The exception I get is :
InvalidOperationException
The binary operator Subtract is not defined for the types 'System.Func`2[Umbraco.Web.Models.DynamicPublishedContent,System.Object]' and 'System.Func`2[Umbraco.Web.Models.DynamicPublishedContent,System.Object]'.
To be honest, at this point, I have spent too much time trying to figure it out so I will probably settle and go with an ugly hack (like looking for /en/ in the URL or something equivalent).
I am surprised though that there isn't an easy way to extract the culture info from the IPublishedContent type.
I didn't even think about looking in the service before asking the questions. It does make the property easily accessible when fetching content that way.
My workaround to the unresolved issue is to implement 2 extension methods to assit with getting a contents language using the legacy Domain API and traversiing the content tree:
public static class LocalizationExtensions { public static ILanguage GetLanguageByContent(this ILocalizationService localizationService, IContent content) {
if (content == null) return null;
// There isn't a DomainService in Umbraco yet so we need to use the legacy Domain API
var domain = Domain.GetDomain("*" + content.Id); // dummy domain name : example: *1234
return (domain == null)
? localizationService.GetLanguageByContent(content.Parent()) // recursive
: localizationService.GetLanguageByCultureCode(domain.Language.CultureAlias); }
public static CultureInfo GetCultureInfo(this IPublishedContent content)
{
var services = UmbracoContext.Current.Application.Services;
var lang = services.LocalizationService.GetLanguageByContent(services.ContentService.GetById(content.Id));
return lang == null ? CultureInfo.CurrentCulture : lang.CultureInfo;
} }
When I want to get an published contents language, I can call this method:
Model.GetCultureInfo(); // where Model is an IPublishedContent
How to find the language / culture of an IPublishedContent object
Hi all,
Sorry for the basic question but I cannot seem to figure it out myself. How do I find the CultureInfo of an IPublishedContent object?
If there is not language property, how do you query against the Culture / Language of items? (I am talking about querying through the Umbraco helper, not through Lucene)
The documentation doesn't really mention it so I'm guessing that I am doing it wrong and that I should be looking elsewhere (?)
The solution I'm looking for cannot depend on the structure of the site tree (i.e. I don't want to assume that all IPublishedContent items below a certain item all have the same culture). In other words, solutions that would assume that everything under certain root element share the same culture don't work.
I was really looking for a solution like :
How should I go about this?
Thanks!
Hi Hugo,
In Razor you can get the culture by this:
This outputs the culture that signed to the website in Culture and Hostnames .
So perhaps you could do something like this:
Hope this can help you,
/Dennis
Hi Dennis and thanks a lot for your help.
I'm not quite able to make it work though.
throws an
InvalidOperationException
becauseSo the "-" seems to be causing problems. I tried to escape it with
.Where("Culture == \"en-US\"")
but it doesn't work.I also tried
.Where("Culture.StartsWith(\"en\")")
and it returns nothing even though I have items with the en-US culture. I'm not sure what i'm doing wrong here.Instead of passing a string predicate, would you know how to express the same thing in a
Func<IPublishedContent, bool>
predicate?Thanks again!
Hi Hugo,
I can see that you are in a stronly typed context, what if you are using dynamic instead of the stronly typed. So the code look like this.
Does this makes any difference or did you still get the error message.
Hope this helps,
/Dennis
Hi Dennis,
Thanks again for your help, but no, it still throws the same exception when I go with
The exception I get is :
To be honest, at this point, I have spent too much time trying to figure it out so I will probably settle and go with an ugly hack (like looking for /en/ in the URL or something equivalent).
I am surprised though that there isn't an easy way to extract the culture info from the
IPublishedContent
type.Thanks for giving me a hand looking into this!
Try this.
Yup, this is awesome, thanks Daniel!
I didn't even think about looking in the service before asking the questions. It does make the property easily accessible when fetching content that way.
Thanks!
Sorry to say that my solution is not valid at this time. Please read this issue: http://issues.umbraco.org/issue/U4-3753
My workaround to the unresolved issue is to implement 2 extension methods to assit with getting a contents language using the legacy Domain API and traversiing the content tree:
When I want to get an published contents language, I can call this method:
In Umbraco 7.2.5 you can use the .GetCulture() extension method on IPublishedContent:
FYI, that extension method lives in the Umbraco.Web namespace.
is working on a reply...