Copied to clipboard

Flag this post as spam?

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


  • Bear 11 posts 61 karma points
    Sep 02, 2015 @ 14:56
    Bear
    0

    Get Culture of IPublishedContent v6.2.5

    Hi

    My site structure is like so;

    • ROOT (en-GB)
    • Some Page (inherit)
    • Some Page (inherit)
      • Some French Page (fr-FR)
      • Some Dutch Page (nl-NL)

    I set the "culture and hostname" on a per page basis (not on the root domain, because I can't)

    I want to retrieve a CultureInfo object for an individual node (IPublishedContent) like "Some French Page" and for it to return me all the good stuff like isocodes etc "fr-FR".

    How do I do this in v6.2.5?

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Sep 02, 2015 @ 16:00
    Tim Geyssens
    1

    You should be able to use the standard .net stuff like Thread.CurrentThread.CurrentCulture

  • Bear 11 posts 61 karma points
    Sep 03, 2015 @ 07:29
    Bear
    0

    Yep, if I'm on the page I can simply call;

    @Model.CurrentCulture
    

    but actually, I wanted to do this on the parent page and

       @foreach (IPublishedContent childPage in Model.Content.Children) {  
            <p>@childPage.CurrentCulture</p> // doesn't work
        }
    

    I've also seen posts that suggest

    @foreach (IPublishedContent childPage in Model.Content.Children)
    {
        var childContent = ApplicationContext.Current.Services.ContentService.GetById(childPage.Id);
        <p>@childContent.Language</p> // not implemented
    }
    

    In V7 theres an extension GetCulture() for IContent and IPublishedContent

    But can't find the right way to get the culture of a child page (IPublishedContent) directly in 6.2.5

  • Bear 11 posts 61 karma points
    Sep 03, 2015 @ 13:20
    Bear
    100

    So, I found

    Which helped me create a helper method;

    public static class LanguageHelper
        {
            public static ILanguage GetLanguage(this IPublishedContent node)
            {
                // guard; no node
                if (node == null) return null;
    
                // hit the db and try to find isoCode
                var connectionString = ConfigurationManager.ConnectionStrings["yourConnectionString"].ConnectionString;
                string isoCode;
                using (var connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    var command = connection.CreateCommand();
                    command.CommandText = "select ul.languageISOCode from umbracoDomains ud join umbracoLanguage ul on ud.domainDefaultLanguage = ul.id where ud.DomainRootStructureID = @rootNodeId";
                    command.Parameters.AddWithValue("@rootNodeId", node.Id);
                    isoCode = Convert.ToString(command.ExecuteScalar());
                }
    
                // guard; no iso code found
                if (string.IsNullOrEmpty(isoCode)) return null;
    
                // hit the LocalizationService to actually return the language
                return ApplicationContext.Current.Services.LocalizationService.GetLanguageByIsoCode(isoCode);
            }
        }
    

    Now I get the good stuff (provided you've set the culture and hostname explicitly on the node you want):-)

    @Model.Content.Children.FirstOrDefault().GetLanguage().CultureInfo.EnglishName
    

    Used sparingly with caching and suitable null checks, this is an OK solution.

  • 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