Copied to clipboard

Flag this post as spam?

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


  • Emil Schoelzer Schnohr 24 posts 144 karma points
    Aug 13, 2019 @ 09:51
    Emil Schoelzer Schnohr
    0

    UmbracoApiController not using correct culture (item.Name always using default language)

    I am trying to make a basic page search function, where I have made a webapi controller (UmbracoApiController) to make an AJAX call for, that returns a list of pages which name matches the search value.

    Problem is that the webapi doesn't seem to know what culture is being used. So i pass in the culture as a string along with the root node id, so I can easily start searching through descendants whos name matches the input value.

    public List<SearchResult> Search(string value = "", int? rootId = null, string culture = "en-US")
    {
        // When debugging I can see this sets the culture to en-US as expected
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
        Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
    
        List<SearchResult> results = new List<SearchResult>();
    
        var root = (rootId != null ? Umbraco.Content(rootId) : Umbraco.ContentAtRoot().First().Children.First());          
    
        // root.Descendants correctly loops through the published items for the specified culture, which is "en-US"
        foreach (var item in root.Descendants(culture).Where(x => x.Name.ToLower().Contains(value.ToLower()) && x.IsVisible() && !string.IsNullOrEmpty(x.GetTemplateAlias())).Take(7))
        {
            SearchResult model = new SearchResult();
            model.PageName = item.Name; // Name is always using the default culture "da-DK", but I want something like item.Name(culture)
    
            model.Url = item.Url(culture); // I can easily get URL for the culture
            model.Parent = "";
    
            if (item.Level > 2)
            {
                model.Parent = item.Parent.Name; // Again, this name is also using the default culture "da-DK" but the culture is set to "en-US"
            }
    
            results.Add(model);
        }
    
        return results;
    }
    

    That it doesn't automatically use the right culture is fine, I just need to know if there is a way I can get the culture-specific item.Name just like I can say "item.Url(culture)" and "item.Value("propertyAlias", culture)".

  • Tom Madden 253 posts 455 karma points MVP 4x c-trib
    Aug 13, 2019 @ 12:53
    Tom Madden
    0

    Which version of Umbraco 8 are you using? This changed a lot between 8.0 and 8.1

  • Emil Schoelzer Schnohr 24 posts 144 karma points
    Aug 13, 2019 @ 13:44
    Emil Schoelzer Schnohr
    0

    I'm on version 8.1.1

  • Tom Madden 253 posts 455 karma points MVP 4x c-trib
    Aug 13, 2019 @ 15:25
    Tom Madden
    100

    You can say item.Name("en-US") to specify a culture , you need to include using Umbraco.Core as it's an extension method which allow passing in a culture string.

    regards t

  • Emil Schoelzer Schnohr 24 posts 144 karma points
    Aug 14, 2019 @ 06:36
    Emil Schoelzer Schnohr
    0

    Awesome, thanks!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies