Copied to clipboard

Flag this post as spam?

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


  • Olov 27 posts 101 karma points
    Apr 04, 2019 @ 12:37
    Olov
    1

    Set culture in published content request

    Setting the Culture in PublishedRequest.Prepared shouldn't it give me IPublishedContent in that culture? I've set svas the default language but also enabled en-US and published the pages in both languages.

    public class PublishedRequestComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Components().Append<PublishedRequestComponent>();
        }
    }
    

    To keep it simple I've hard coded the culture here.

    public class PublishedRequestComponent : IComponent
    {
        public void Initialize()
        {
            PublishedRequest.Prepared += PublishedRequest_Prepared;
        }
    
        public void Terminate()
        {
        }
    
        private void PublishedRequest_Prepared(object sender, System.EventArgs e)
        {
            var request = sender as PublishedRequest;
    
            request.Culture = new System.Globalization.CultureInfo("en-US");
        }
    }
    

    Get the translated names of the pages...

    var names = Umbraco.ContentAtRoot()
        .FirstOrDefault(x => x.IsPublished())
        ?.Children
        .Select(x => x.Name);
    

    But all I get is the names in sv...

  • Olov 27 posts 101 karma points
    Apr 05, 2019 @ 11:24
    Olov
    0

    Ok, I've investigated it a bit further and it seems to work as expected if I render a page, but I'm trying to do this in an UmbracoApiController. Is it possible to set the "published request culture" there?

  • Olov 27 posts 101 karma points
    Apr 05, 2019 @ 15:06
    Olov
    0

    Tried to set the thread culture, no difference, the returned variants are sv

    var culture = new CultureInfo("en-US");
    Thread.CurrentThread.CurrentCulture = culture;
    Thread.CurrentThread.CurrentUICulture = culture;
    
    var names = Umbraco.ContentAtRoot()
        .FirstOrDefault(x => x.IsPublished())
        ?.Children
        .Select(x => x.Name);
    
  • Corné Strijkert 80 posts 456 karma points c-trib
    Apr 05, 2019 @ 17:47
    Corné Strijkert
    1

    Thats the expected behaviour in Umbraco 8.

    The injected Model on pages contains property values based on the current language (based on current domain). So, when you output @Model.Name, you can see that the name is displayed in en-US culture.

    But, your code to get the names of the roots children is not sensitive for cultures. You will get all children, even when there is no specific content for specific languages.

    var names = Umbraco.ContentAtRoot()
        .FirstOrDefault(x => x.IsPublished())
        ?.Children
        .Select(x => x.Name);
    

    The 'Name' property contains always the default language value. Instead you can use the following code:

    var currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture.Name;
    
    var names = Umbraco.ContentAtRoot()
           .FirstOrDefault(x => x.IsPublished())
           ?.Children
           .Select(x => x.GetCulture(currentCulture).Name);
    

    When you want to list only the children that are available in the current culture, you can use:

    var names = Umbraco.ContentAtRoot()
           .FirstOrDefault(x => x.IsPublished())
           ?.Children
           .Where(x.Cultures.ContainsKey(currentCulture))
           .Select(x => x.GetCulture(currentCulture).Name);
    
  • Olov 27 posts 101 karma points
    Apr 08, 2019 @ 07:24
    Olov
    0

    The code above works like a charm for getting the name in the specified culture, but it only solves the first part of what I'm trying to do. I also need to access properties defined by me in that culture, is it possible to get a complete Models Builder model in the culture? Or do I need to get them one by one using

    .Value("propName", "culture")

    (It works but a translated model would be prettier IMO)

            var items = Umbraco.ContentAtRoot()
                .FirstOrDefault(x => x.IsPublished())
                ?.Children
                .Where(x => x.Cultures.ContainsKey(currentCulture))
                .Select(x => new Item
                {
                    Id = x.Id,
                    Name = x.GetCulture(currentCulture).Name,
                    Heading = x.Value<string>(nameof(Standard.Heading), currentCulture)
                });
    
  • Dave de Moel 122 posts 574 karma points c-trib
    Jul 02, 2019 @ 13:30
    Dave de Moel
    0

    Have you ever found a solution for this? I am currently running into the same problem with an api we have. It needs to be multilingual but I don't seem to find a way to do that using the model builder models :(

  • Dave de Moel 122 posts 574 karma points c-trib
    Aug 20, 2019 @ 12:40
    Dave de Moel
    8

    After digging into the source of Umbraco, I found how they define which language to get the content for. They do this by creating a new VariantionContext.

    F.E. ``` public class ContentApiController : UmbracoApiController { private readonly IVariationContextAccessor _variationContextAccessor;

        public ContentApiController(IVariationContextAccessor variationContextAccessor)
        {
            _variationContextAccessor = variationContextAccessor;
        }
    
        public IHttpActionResult Get(int id, string culture)
        {
            _variationContextAccessor.VariationContext = new VariationContext(culture);
            var cnt = Umbraco.Content(id);
            return Ok(cnt.Name);
        }
    }
    

    This will return the data in the language for which the culture is passed.

  • OleP 67 posts 276 karma points
    Oct 14, 2019 @ 20:03
    OleP
    0

    This works really well!

    But isn't the point of the culture parameter of the .Children and .Descendents extention that you can get all children/descendents in a specific culture when also specificering the type?

    List<IPublishedContent> childrenOfType = item.Children<TextPage>("en-GB")
    
    List<IPublishedContent> descendentsOfType = item.Descendents<TextPage>("en-GB")
    

    At least this doesn't seem to work when doing async controller requests.

  • Chris Kim 48 posts 254 karma points c-trib
    Aug 27, 2020 @ 03:16
    Chris Kim
    2

    Wow, THIS!

    _variationContextAccessor.VariationContext = new VariationContext(culture);

    I was digging around how to properly set the culture on AJAX requests but gave up the other day.

    This seems to do it perfectly. Before, I was passing the culture to each property on partials returned from AJAX requests, but this became tedious and felt wrong when it wasn't necessary for normal requests. Setting the VariationContext in the AJAX controller seems to take care of it.

    EDIT: Now that I knew what to look for, I also found this on the official documentation: https://our.umbraco.com/documentation/Reference/Language-Variation/

Please Sign in or register to post replies

Write your reply to:

Draft