Copied to clipboard

Flag this post as spam?

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


  • Martin Griffiths 826 posts 1269 karma points c-trib
    Oct 08, 2015 @ 09:48
    Martin Griffiths
    0

    Get Language code for a page with Umbraco Public methods

    I'm finding it very difficult to find any non-deprecated API that will provide me with the language code (inherited) for a given pageId.

    Cannot find anything in IPublishedContent content or through any of the helpers.

    I managed to find .Language in the ContentService but despite being apparently implemented in v6 its marked as deprecated!

    The LocalizationService just seems to be a get/setAll in the system rather than whats applied to the nodes themselves.

    Can anyone shed any light? Should I just use a vanilla c# method via HttpContext?

    Thanks

    Martin

  • Martin Griffiths 826 posts 1269 karma points c-trib
    Oct 08, 2015 @ 14:49
    Martin Griffiths
    100

    Hello all Umbracians.

    For anyone fighting to find an answer to this, I realised Language is now tied to Domain, as its part and parcel of the same area in the back office.

    So i've used the new DomainService to get at the code.

    Here's an example for those interested, you may need to do more iteration than I have...

            public static string GetLanguage(int pageId)
        {
            try
            {
                // set default
                var languageCode = "en-GB";
    
                // try getting domain mappings for current page
                IEnumerable<IDomain> currentPageDomains = ApplicationContext.Current.Services.DomainService.GetAssignedDomains(pageId, false);
    
                if (currentPageDomains.Count() > 0)
                {
                    // get code from first entry as multiple entries will always have the same language code
                    languageCode = currentPageDomains.FirstOrDefault().LanguageIsoCode;
                }
                else
                {
                    // if no domain mapping for currentPage, navigate to root node and get language code
                    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                    var currentPage = umbracoHelper.TypedContent(pageId);
                    var rootPageId = currentPage.AncestorOrSelf(1).Id;
    
                    IEnumerable<IDomain> rootPageDomains = ApplicationContext.Current.Services.DomainService.GetAssignedDomains(rootPageId, false);
    
                    languageCode = rootPageDomains.FirstOrDefault().LanguageIsoCode;
                }
    
                return languageCode;
            }
    
            catch (Exception)
            {
                return "en-GB";
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft