From 99598ec0609b3676aee40301b587344867e7ddd3 Mon Sep 17 00:00:00 2001 From: Stephan Date: Tue, 3 Mar 2015 13:03:11 +0100 Subject: [PATCH] U4-3753 - add a way to get the rendering culture of a content --- src/Umbraco.Web/Models/ContentExtensions.cs | 42 +++++++++++++++++++ src/Umbraco.Web/PublishedContentExtensions.cs | 36 ++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/Umbraco.Web/Models/ContentExtensions.cs diff --git a/src/Umbraco.Web/Models/ContentExtensions.cs b/src/Umbraco.Web/Models/ContentExtensions.cs new file mode 100644 index 0000000000..85f1a3033d --- /dev/null +++ b/src/Umbraco.Web/Models/ContentExtensions.cs @@ -0,0 +1,42 @@ +using System; +using System.Globalization; +using System.Linq; +using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Web.Routing; + +namespace Umbraco.Web.Models +{ + public static class ContentExtensions + { + /// + /// Gets the culture that would be selected to render a specified content, + /// within the context of a specified current request. + /// + /// The content. + /// The request Uri. + /// The culture that would be selected to render the content. + public static CultureInfo GetCulture(this IContent content, Uri current = null) + { + var route = UmbracoContext.Current.ContentCache.GetRouteById(content.Id); // cached + var pos = route.IndexOf('/'); + + var domainHelper = new DomainHelper(ApplicationContext.Current.Services.DomainService); + + var domain = pos == 0 + ? null + : domainHelper.DomainForNode(int.Parse(route.Substring(0, pos)), current).UmbracoDomain; + + if (domain == null) + { + var defaultLanguage = ApplicationContext.Current.Services.LocalizationService.GetAllLanguages().FirstOrDefault(); + return defaultLanguage == null ? CultureInfo.CurrentUICulture : new CultureInfo(defaultLanguage.IsoCode); + } + + var wcDomain = DomainHelper.FindWildcardDomainInPath(ApplicationContext.Current.Services.DomainService.GetAll(true), content.Path, domain.RootContent.Id); + return wcDomain == null + ? new CultureInfo(domain.Language.IsoCode) + : new CultureInfo(wcDomain.Language.IsoCode); + } + } +} diff --git a/src/Umbraco.Web/PublishedContentExtensions.cs b/src/Umbraco.Web/PublishedContentExtensions.cs index d7a19288fc..923edf50c2 100644 --- a/src/Umbraco.Web/PublishedContentExtensions.cs +++ b/src/Umbraco.Web/PublishedContentExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Data; +using System.Globalization; using System.Linq; using System.Web; using Examine.LuceneEngine.SearchCriteria; @@ -8,6 +9,7 @@ using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; using Umbraco.Web.Models; using Umbraco.Core; +using Umbraco.Web.Routing; using ContentType = umbraco.cms.businesslogic.ContentType; namespace Umbraco.Web @@ -1882,5 +1884,39 @@ namespace Umbraco.Web } #endregion + + #region Culture + + /// + /// Gets the culture that would be selected to render a specified content, + /// within the context of a specified current request. + /// + /// The content. + /// The request Uri. + /// The culture that would be selected to render the content. + public static CultureInfo GetCulture(this IPublishedContent content, Uri current = null) + { + var route = UmbracoContext.Current.ContentCache.GetRouteById(content.Id); // cached + var pos = route.IndexOf('/'); + + var domainHelper = new DomainHelper(ApplicationContext.Current.Services.DomainService); + + var domain = pos == 0 + ? null + : domainHelper.DomainForNode(int.Parse(route.Substring(0, pos)), current).UmbracoDomain; + + if (domain == null) + { + var defaultLanguage = ApplicationContext.Current.Services.LocalizationService.GetAllLanguages().FirstOrDefault(); + return defaultLanguage == null ? CultureInfo.CurrentUICulture : new CultureInfo(defaultLanguage.IsoCode); + } + + var wcDomain = DomainHelper.FindWildcardDomainInPath(ApplicationContext.Current.Services.DomainService.GetAll(true), content.Path, domain.RootContent.Id); + return wcDomain == null + ? new CultureInfo(domain.Language.IsoCode) + : new CultureInfo(wcDomain.Language.IsoCode); + } + + #endregion } }