Copied to clipboard

Flag this post as spam?

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


  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    May 09, 2019 @ 07:50
    Frans de Jong
    0

    AncestorsOrSelf<T>() returns wrong culture

    In a site where the structure looks like this

    -Product overview --Product category ---Product

    In v7 if I needed a label from the productoverview to be shown for a property of a grandchild I could do something like this:

    public Product(IPublishedContent content) : base(content)
    {
       string currentCulture = Thread.CurrentThread.CurrentCulture.Name;
       ProductOverview productOverview = content.AncestorOrSelf<ProductOverview>();
       string label = productOverview.Label;
    }
    

    In V8 when I do the same in the culture en-US it returns the value of the default variant which is the nl-NL variant.

    I just can't find a way to get the culture right

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    May 09, 2019 @ 08:49
    Frans de Jong
    0

    Ok, for future reference.

    In V7 it was save to use the constructor of modelsbuilder to set a property. In V8 this is not ok when dealing with variant properties.

    When NuCache is reloaded it will hit the constructor with the backoffice language and when that doesn't exist it breaks NuCache and the views will show the default language.

  • Stephen 767 posts 2273 karma points c-trib
    May 09, 2019 @ 14:53
    Stephen
    1

    When you get content.Label, you get content.Value("label") ie the value of the "label" property, for the current, ambient culture.

    Therefore, when rendering a view, you get the value in the correct culture. If you render the same view with two different cultures, you get two different values.

    However, outside a view, you get... the value corresponding to whatever is the current culture, and if you capture that value, eg

    var label = content.Label;
    

    the label variable contains the value for that current culture. And will not change. So, as a rule of thumb: do not capture things that are contextual. If you want to capture something, here, capture productOverview and then get its Label property when needed.

    Another way to put it is this: a document is a "whole" with all its cultures, it is never "the english document". You cannot get "the german version of a document". Getting ancestors... gives you the ancestor documents. And the values of their properties depends on the current culture, not on the document itself.

    Is this making more sense?

Please Sign in or register to post replies

Write your reply to:

Draft