Copied to clipboard

Flag this post as spam?

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


  • Patrick van Kemenade 101 posts 339 karma points
    Apr 18, 2021 @ 12:35
    Patrick van Kemenade
    0

    Getting IPublishedContent within UmbracoApiController for the correct culture

    When I'm retrieving data within a API method, I have trouble getting the content for the correct culture.

    It seems to completly ignore the url from which the API function is run and instead uses the default culture.

    When trying to set the culture myself, or getting the correct language variant I get stuck.

    string culture = "nl";
    
    var ticketsFeed = GetRootElement(); // this works
    
    // HACK TO GET THE RIGHT CULTURE, seems it doesn't work
    System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
    
    var umbracoFeedItems = ticketsFeed.Children(culture) // looks like culture is ignored
        .Where(n => n is FeedItemtickets)
        .Select(n => (FeedItemtickets)n);
    
    var test = umbracoFeedItems.First();
    
    var test2 = test.HasCulture("nl"); // true, yes I have it I'm just not giving it to you
    
    var test3 = test.GetProperty("Summary"); // Gives English text instead of Dutch
    
    var test4 = test.Summary // Also the English text instead of Dutch
    
    // no overload available like: test4.GetProperty("Summary", culture);
    

    Anybody any idea on how to fix this ?

  • Patrick van Kemenade 101 posts 339 karma points
    Apr 18, 2021 @ 13:18
    Patrick van Kemenade
    0

    To answer my own question:

    var existingPublishedItem = umbracoFeedItems.Where(n => !String.IsNullOrWhiteSpace(n.UniqueId) && n.UniqueId == rawItem.ProductId).First();
    
    var existingItem = contentService.GetById(existingPublishedItem.Id); // via injection I got a reference to the concentService
    
    string summary = existingItem.GetValue<string>(FeedItemTickets.GetModelPropertyType(x => x.Summary).Alias, culture);
    

    Wish I could have use existingPublishedItem.Summary instead of this semi-strongly-typed solution in this multilingual scenario.

  • Johan Dahlström 33 posts 146 karma points
    Apr 18, 2021 @ 20:05
    Johan Dahlström
    1

    How about injecting IVariationContextAccessor and setting the culture there before you fetch any content? E.g.

    _variationContextAccessor.VariationContext = new VariationContext("nl");
    // get content here
    
  • Patrick van Kemenade 101 posts 339 karma points
    Apr 19, 2021 @ 10:46
    Patrick van Kemenade
    0

    Thanks Johan, that's looks like a much better solution then what I came up with.

Please Sign in or register to post replies

Write your reply to:

Draft