Copied to clipboard

Flag this post as spam?

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


  • Bo Jacobsen 240 posts 1128 karma points
    1 week ago
    Bo Jacobsen
    0

    How to get current culture from IpublishedContent in umbraco 8

    Hi all.

    Just upgraded to Umbraco 8.1.0 from 8.0.2 and have to change our code to work with the breaking changes.

    Before we could get the current PublishedCultureInfo by the IPublishedContent interface like this.

    using Umbraco.Web;
    IPublishedContent.GetCulture()
    

    How would we get it now?

    Do we have to check current.Thread.CultureInfo or is there a simplier way?

  • Luke Hook 32 posts 135 karma points
    1 week ago
    Luke Hook
    0

    Hi there,

    Looking at some of the recent changes in 8.1 there have been a number of methods removed from IPublishedContent that break a number of things.

    Check out this link

    Paying attention to Stephens comment further down that gives a summary of changes.

    An extract from which is here.

    Culture: content.Cultures now returns a collection of string representing the available cultures, and content.GetCulture(...) is gone, the proper way to get the culture date is content.CultureDate(string culture = null)

    Not sure if that fully answers your question or not but should hopefully helps. IMO this changes should have been documented better in the release notes. I've seen number of posts the last couple of days regarding these changes and the information only exists in those comments quite for down the string of the change request

  • Bo Jacobsen 240 posts 1128 karma points
    1 week ago
    Bo Jacobsen
    0

    Hi Luke.

    IPublishedContent.CultureDate(string culture = null) returning a DateTime and not a PublishedCultureInfo.

    If you do not set a culture, then the function try to find it this way. And maybe thats the new way to get the current Culture instead of IPublishedContent.GetCulture()

    VariationContextAccessor?.VariationContext?.Culture
    
  • Luke Hook 32 posts 135 karma points
    1 week ago
    Luke Hook
    0

    Hi Bo,

    Sorry yes there didn't appear to be a new way of getting the current Culture. I was just highlighting where this information was surrounding it's removal in the release notes. Seems strange to remove it completely without a solid alternative

  • Bo Jacobsen 240 posts 1128 karma points
    1 week ago
    Bo Jacobsen
    0

    Hi Luke.

    Your link gave me a better insight in what's going on, so that was very helpful.

  • Bryna 66 posts 227 karma points
    1 week ago
    Bryna
    0

    I think the Properties now take culture as an argument.

    Model.Url("es")
    Model.Value("customProp","es")
    

    It is a bit more intuitive provided one isn't set in their ways:). I think that is a fairly solid alternative.

  • Bo Jacobsen 240 posts 1128 karma points
    1 week ago
    Bo Jacobsen
    0

    Hi Bryna.

    Model.Value("customProp","es") was also possible before. But Model.Url("es") and Model.Name("es") is new.

    The weird thing is if you dont set a culture it will take the current culture. But there aint a method to get the current culture and that is what i need.

  • giuseppe 8 posts 61 karma points
    1 week ago
    giuseppe
    101

    Hi Bo, I use this snippet to get the current culture

    @Model.GetCultureFromDomains()
    

    without arguments.

  • Bo Jacobsen 240 posts 1128 karma points
    1 week ago
    Bo Jacobsen
    0

    Hi Giuseppe.

    That seems to work.

    /// <summary>
            /// Gets the culture assigned to a document by domains, in the context of a current Uri.
            /// </summary>
            /// <param name="content">The document.</param>
            /// <param name="current">An optional current Uri.</param>
            /// <returns>The culture assigned to the document by domains.</returns>
            /// <remarks>
            /// <para>In 1:1 multilingual setup, a document contains several cultures (there is not
            /// one document per culture), and domains, withing the context of a current Uri, assign
            /// a culture to that document.</para>
            /// </remarks>
            public static string GetCultureFromDomains(this IPublishedContent content, Uri current = null)
            {
                var umbracoContext = UmbracoContext;
    
                if (umbracoContext == null)
                    throw new InvalidOperationException("A current UmbracoContext is required.");
    
                return DomainUtilities.GetCultureFromDomains(content.Id, content.Path, current, umbracoContext, SiteDomainHelper);
            }
    

    Before your answer i made a method that also seems to work, but i rather use the IPublishedContent.GetCultureFromDomains()

    public string GetCulture()
    {
        return Umbraco.Core.Composing.Current.VariationContextAccessor?.VariationContext?.Culture ?? "";
    }
    
  • Dmitriy 135 posts 490 karma points
    1 week ago
    Dmitriy
    0

    Thank you, I was looking for that too.

    By the way, may be you know the approach to get all published cultures for current page?

  • Bo Jacobsen 240 posts 1128 karma points
    1 week ago
    Bo Jacobsen
    0

    Hi Dimitri.

    Model.Cultures should do it.

Please Sign in or register to post replies

Write your reply to:

Draft