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 593 posts 2389 karma points
    Jul 11, 2019 @ 15:08
    Bo Jacobsen
    1

    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 45 posts 175 karma points c-trib
    Jul 11, 2019 @ 15:44
    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 593 posts 2389 karma points
    Jul 11, 2019 @ 18:34
    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 45 posts 175 karma points c-trib
    Jul 11, 2019 @ 18:37
    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 593 posts 2389 karma points
    Jul 11, 2019 @ 18:44
    Bo Jacobsen
    0

    Hi Luke.

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

  • Bryna 73 posts 259 karma points
    Jul 11, 2019 @ 21:48
    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 593 posts 2389 karma points
    Jul 12, 2019 @ 05:55
    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 9 posts 66 karma points
    Jul 12, 2019 @ 08:21
    giuseppe
    103

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

    @Model.GetCultureFromDomains()
    

    without arguments.

  • Bo Jacobsen 593 posts 2389 karma points
    Jul 12, 2019 @ 08:58
    Bo Jacobsen
    1

    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 168 posts 588 karma points
    Jul 12, 2019 @ 10:05
    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 593 posts 2389 karma points
    Jul 12, 2019 @ 10:15
    Bo Jacobsen
    1

    Hi Dimitri.

    Model.Cultures should do it.

  • David Armitage 505 posts 2073 karma points
    Nov 13, 2020 @ 04:57
    David Armitage
    0

    Thanks. You saved me some time here.

  • Alexander Gräf 25 posts 131 karma points
    Jan 19, 2021 @ 20:20
    Alexander Gräf
    0

    None of that works anymore. UmbracoContext has become Current.UmbracoContext, and DomainUtilities.GetCultureFromDomains is now an internal method.

  • Dmitriy 168 posts 588 karma points
    May 20, 2020 @ 16:03
    Dmitriy
    1

    UPD

    Here is my extension method to get old-school CultureInfo object like it was in Umbraco 7.

    public static CultureInfo GetCulture(this IPublishedContent content)
    {
       return new CultureInfo(content.GetCultureFromDomains());
    }
    

    Let me know if you know the better way

  • Brett Spencer 88 posts 259 karma points
    Dec 24, 2020 @ 18:45
    Brett Spencer
    0

    Thank you Dmitriy, this allowed me to save time and keep the current architecture for our subnavigation:

    var defaultLanguage = umbracoModel.GetTwoLetterLanguageName();
    
        public static string GetTwoLetterLanguageName(this ContentModel umbracoModel)
        {
            IPublishedContent model = umbracoModel.Content;
            return model.GetTwoLetterLanguageName();
        }
    
        public static string GetTwoLetterLanguageName(this IPublishedContent model)
        {
            /*** Deprecated from v7
                //System.Globalization.CultureInfo culture = model.GetCulture();
                //return culture.TwoLetterISOLanguageName;
            ***/
            System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(model.GetCultureFromDomains());
            return culture.TwoLetterISOLanguageName;
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft