From 76fb595a55d01c11d06cdb9a2043d7ddd6efd450 Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 18 May 2015 13:38:14 +1000 Subject: [PATCH] Fixes: U4-6363 Expose Dictionary item access on UmbracoHelper --- .../CultureDictionaryFactoryResolver.cs | 17 ++- .../Dictionary/ICultureDictionary.cs | 8 ++ .../Dictionary/UmbracoCultureDictionary.cs | 112 ++++++++++++++++-- src/Umbraco.Web/UmbracoHelper.cs | 26 ++-- .../RazorCore/UmbracoCultureDictionary.cs | 9 +- 5 files changed, 146 insertions(+), 26 deletions(-) diff --git a/src/Umbraco.Core/Dictionary/CultureDictionaryFactoryResolver.cs b/src/Umbraco.Core/Dictionary/CultureDictionaryFactoryResolver.cs index 80abfb0eaf..334c2fbe4b 100644 --- a/src/Umbraco.Core/Dictionary/CultureDictionaryFactoryResolver.cs +++ b/src/Umbraco.Core/Dictionary/CultureDictionaryFactoryResolver.cs @@ -1,3 +1,5 @@ +using System; +using System.ComponentModel; using Umbraco.Core.ObjectResolution; namespace Umbraco.Core.Dictionary @@ -12,15 +14,22 @@ namespace Umbraco.Core.Dictionary { } - /// - /// Can be used by developers at runtime to set their ICultureDictionaryFactory at app startup - /// - /// + [Obsolete("Use SetDictionaryFactory instead")] + [EditorBrowsable(EditorBrowsableState.Never)] public void SetContentStore(ICultureDictionaryFactory factory) { Value = factory; } + /// + /// Can be used by developers at runtime to set their ICultureDictionaryFactory at app startup + /// + /// + public void SetDictionaryFactory(ICultureDictionaryFactory factory) + { + Value = factory; + } + /// /// Returns the ICultureDictionaryFactory /// diff --git a/src/Umbraco.Core/Dictionary/ICultureDictionary.cs b/src/Umbraco.Core/Dictionary/ICultureDictionary.cs index 57a739dbc6..8617d7440e 100644 --- a/src/Umbraco.Core/Dictionary/ICultureDictionary.cs +++ b/src/Umbraco.Core/Dictionary/ICultureDictionary.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Globalization; namespace Umbraco.Core.Dictionary @@ -19,5 +20,12 @@ namespace Umbraco.Core.Dictionary /// Returns the current culture /// CultureInfo Culture { get; } + + /// + /// Returns the child dictionary entries for a given key + /// + /// + /// + IDictionary GetChildren(string key); } } \ No newline at end of file diff --git a/src/Umbraco.Web/Dictionary/UmbracoCultureDictionary.cs b/src/Umbraco.Web/Dictionary/UmbracoCultureDictionary.cs index e7ebb3cb6a..29954701ea 100644 --- a/src/Umbraco.Web/Dictionary/UmbracoCultureDictionary.cs +++ b/src/Umbraco.Web/Dictionary/UmbracoCultureDictionary.cs @@ -1,17 +1,61 @@ using System; +using System.Collections.Generic; using System.Dynamic; using System.Globalization; +using System.Linq; using System.Web; using Umbraco.Core.Logging; using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.language; +using Umbraco.Core; +using Umbraco.Core.Cache; +using Umbraco.Core.Models; +using Umbraco.Core.Services; namespace Umbraco.Web.Dictionary { + /// + /// A culture dictionary that uses the Umbraco ILocalizationService + /// public class DefaultCultureDictionary : Umbraco.Core.Dictionary.ICultureDictionary { - /// + private readonly ILocalizationService _localizationService; + private readonly ICacheProvider _requestCacheProvider; + private readonly CultureInfo _specificCulture = null; + + public DefaultCultureDictionary() + : this(ApplicationContext.Current.Services.LocalizationService, ApplicationContext.Current.ApplicationCache.RequestCache) + { + + } + + public DefaultCultureDictionary(ILocalizationService localizationService, ICacheProvider requestCacheProvider) + { + if (localizationService == null) throw new ArgumentNullException("localizationService"); + if (requestCacheProvider == null) throw new ArgumentNullException("requestCacheProvider"); + _localizationService = localizationService; + _requestCacheProvider = requestCacheProvider; + } + + public DefaultCultureDictionary(CultureInfo specificCulture) + : this(ApplicationContext.Current.Services.LocalizationService, ApplicationContext.Current.ApplicationCache.RequestCache) + { + if (specificCulture == null) throw new ArgumentNullException("specificCulture"); + _specificCulture = specificCulture; + } + + public DefaultCultureDictionary(CultureInfo specificCulture, ILocalizationService localizationService, ICacheProvider requestCacheProvider) + { + if (specificCulture == null) throw new ArgumentNullException("specificCulture"); + if (localizationService == null) throw new ArgumentNullException("localizationService"); + if (requestCacheProvider == null) throw new ArgumentNullException("requestCacheProvider"); + _localizationService = localizationService; + _requestCacheProvider = requestCacheProvider; + _specificCulture = specificCulture; + } + + /// /// Returns the dictionary value based on the key supplied /// /// @@ -20,15 +64,19 @@ namespace Umbraco.Web.Dictionary { get { - try - { - return new global::umbraco.cms.businesslogic.Dictionary.DictionaryItem(key).Value(Language.id); - } - catch (Exception e) - { - LogHelper.WarnWithException("Error returning dictionary item '" + key + "'", true, e); - return string.Empty; - } + var found = _localizationService.GetDictionaryItemByKey(key); + if (found == null) + { + return string.Empty; + } + + var byLang = found.Translations.FirstOrDefault(x => x.Language.Equals(Language)); + if (byLang == null) + { + return string.Empty; + } + + return byLang.Value; } } @@ -37,12 +85,50 @@ namespace Umbraco.Web.Dictionary /// public CultureInfo Culture { - get { return System.Threading.Thread.CurrentThread.CurrentUICulture; } + get { return _specificCulture ?? System.Threading.Thread.CurrentThread.CurrentUICulture; } } - private Language Language + /// + /// Returns the child dictionary entries for a given key + /// + /// + /// + public IDictionary GetChildren(string key) + { + var result = new Dictionary(); + + var found = _localizationService.GetDictionaryItemByKey(key); + if (found == null) + { + return result; + } + + var children = _localizationService.GetDictionaryItemChildren(found.Key); + if (children == null) + { + return result; + } + + foreach (var dictionaryItem in children) + { + var byLang = dictionaryItem.Translations.FirstOrDefault((x => x.Language.Equals(Language))); + if (byLang != null) + { + result.Add(dictionaryItem.ItemKey, byLang.Value); + } + } + + return result; + } + + private ILanguage Language { - get { return Language.GetByCultureCode(System.Threading.Thread.CurrentThread.CurrentUICulture.Name); } + get + { + //ensure it's stored/retrieved from request cache + return _requestCacheProvider.GetCacheItem(typeof (DefaultCultureDictionary).Name + "Culture", + () => _localizationService.GetLanguageByIsoCode(Culture.Name)); + } } } diff --git a/src/Umbraco.Web/UmbracoHelper.cs b/src/Umbraco.Web/UmbracoHelper.cs index 383897b2f2..d60630a58d 100644 --- a/src/Umbraco.Web/UmbracoHelper.cs +++ b/src/Umbraco.Web/UmbracoHelper.cs @@ -26,8 +26,7 @@ namespace Umbraco.Web private readonly UmbracoContext _umbracoContext; private readonly IPublishedContent _currentPage; private readonly ITypedPublishedContentQuery _typedQuery; - private readonly IDynamicPublishedContentQuery _dynamicQuery; - + private readonly IDynamicPublishedContentQuery _dynamicQuery; private readonly HtmlStringUtilities _stringUtilities = new HtmlStringUtilities(); private IUmbracoComponentRenderer _componentRenderer; @@ -362,14 +361,25 @@ namespace Umbraco.Web /// public string GetDictionaryValue(string key) { - if (_cultureDictionary == null) - { - var factory = CultureDictionaryFactoryResolver.Current.Factory; - _cultureDictionary = factory.CreateDictionary(); - } - return _cultureDictionary[key]; + return CultureDictionary[key]; } + /// + /// Returns the ICultureDictionary for access to dictionary items + /// + public ICultureDictionary CultureDictionary + { + get + { + if (_cultureDictionary == null) + { + var factory = CultureDictionaryFactoryResolver.Current.Factory; + _cultureDictionary = factory.CreateDictionary(); + } + return _cultureDictionary; + } + } + #endregion #region Membership diff --git a/src/umbraco.MacroEngines/RazorCore/UmbracoCultureDictionary.cs b/src/umbraco.MacroEngines/RazorCore/UmbracoCultureDictionary.cs index b228f0741e..8921925f9a 100644 --- a/src/umbraco.MacroEngines/RazorCore/UmbracoCultureDictionary.cs +++ b/src/umbraco.MacroEngines/RazorCore/UmbracoCultureDictionary.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Dynamic; using System.Globalization; using umbraco.cms.businesslogic; @@ -39,7 +40,13 @@ namespace umbraco.MacroEngines } } - public Language Language + [Obsolete("This returns an empty dictionary, it is not intended to be used by the legacy razor macros")] + public IDictionary GetChildren(string key) + { + return new Dictionary(); + } + + public Language Language { get { return Language.GetByCultureCode(System.Threading.Thread.CurrentThread.CurrentUICulture.Name); } }