Copied to clipboard

Flag this post as spam?

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


  • Hugo Migneron 32 posts 105 karma points
    Feb 16, 2015 @ 21:16
    Hugo Migneron
    2

    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 */)
    

    How should I go about this?

    Thanks!

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

    Hi Hugo,

    In Razor you can get the culture by this:

    @Culture

    This outputs the culture that signed to the website in Culture and Hostnames .

    So perhaps you could do something like this:

    UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);

    var items = helper.TypedContentAtRoot().Children("myContentType").Where("Culture == en-US")

    Hope this can help you,

    /Dennis

  • Hugo Migneron 32 posts 105 karma points
    Feb 17, 2015 @ 00:31
    Hugo Migneron
    0

    Hi Dennis and thanks a lot for your help.

    I'm not quite able to make it work though.

    helper.TypedContentAtRoot().Children("myContentType").Where("Culture == en-US")
    

    throws an InvalidOperationException because

    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!

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

    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.

    UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);

    var items = helper.ContentAtRoot().Children("myContentType").Where("Culture == en-US")

    Does this makes any difference or did you still get the error message.

    Hope this helps,

    /Dennis

  • Hugo Migneron 32 posts 105 karma points
    Feb 17, 2015 @ 14:47
    Hugo Migneron
    0

    Hi Dennis,

    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.

    Thanks for giving me a hand looking into this!

  • Daniel Bardi 927 posts 2562 karma points
    Feb 24, 2015 @ 21:11
    Daniel Bardi
    102

    Try this.

    var language = UmbracoContext.Current.Application.Services.ContentService.GetById(content.Id).Language;
  • Hugo Migneron 32 posts 105 karma points
    Feb 24, 2015 @ 21:18
    Hugo Migneron
    0

    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!

  • Daniel Bardi 927 posts 2562 karma points
    Feb 25, 2015 @ 00:28
    Daniel Bardi
    0

    Sorry to say that my solution is not valid at this time.  Please read this issue: http://issues.umbraco.org/issue/U4-3753

  • Daniel Bardi 927 posts 2562 karma points
    Feb 25, 2015 @ 02:44
    Daniel Bardi
    2

    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
  • John Ligtenberg 53 posts 214 karma points
    May 23, 2015 @ 23:36
    John Ligtenberg
    3

    In Umbraco 7.2.5 you can use the .GetCulture() extension method on IPublishedContent:

    var segmentRoot = Umbraco.TypedContent(segmentId);
    CultureInfo cultureInfo = segmentRoot.GetCulture();
  • Brett Spencer 88 posts 259 karma points
    Sep 25, 2018 @ 15:49
    Brett Spencer
    0

    FYI, that extension method lives in the Umbraco.Web namespace.

Please Sign in or register to post replies

Write your reply to:

Draft