Fixes: U4-6363 Expose Dictionary item access on UmbracoHelper
This commit is contained in:
@@ -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
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Can be used by developers at runtime to set their ICultureDictionaryFactory at app startup
|
||||
/// </summary>
|
||||
/// <param name="factory"></param>
|
||||
[Obsolete("Use SetDictionaryFactory instead")]
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public void SetContentStore(ICultureDictionaryFactory factory)
|
||||
{
|
||||
Value = factory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Can be used by developers at runtime to set their ICultureDictionaryFactory at app startup
|
||||
/// </summary>
|
||||
/// <param name="factory"></param>
|
||||
public void SetDictionaryFactory(ICultureDictionaryFactory factory)
|
||||
{
|
||||
Value = factory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the ICultureDictionaryFactory
|
||||
/// </summary>
|
||||
|
||||
@@ -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
|
||||
/// </summary>
|
||||
CultureInfo Culture { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the child dictionary entries for a given key
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
IDictionary<string, string> GetChildren(string key);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// A culture dictionary that uses the Umbraco ILocalizationService
|
||||
/// </summary>
|
||||
public class DefaultCultureDictionary : Umbraco.Core.Dictionary.ICultureDictionary
|
||||
{
|
||||
/// <summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the dictionary value based on the key supplied
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
@@ -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<DefaultCultureDictionary>("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
|
||||
/// </summary>
|
||||
public CultureInfo Culture
|
||||
{
|
||||
get { return System.Threading.Thread.CurrentThread.CurrentUICulture; }
|
||||
get { return _specificCulture ?? System.Threading.Thread.CurrentThread.CurrentUICulture; }
|
||||
}
|
||||
|
||||
private Language Language
|
||||
/// <summary>
|
||||
/// Returns the child dictionary entries for a given key
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public IDictionary<string, string> GetChildren(string key)
|
||||
{
|
||||
var result = new Dictionary<string, string>();
|
||||
|
||||
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<ILanguage>(typeof (DefaultCultureDictionary).Name + "Culture",
|
||||
() => _localizationService.GetLanguageByIsoCode(Culture.Name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
/// <returns></returns>
|
||||
public string GetDictionaryValue(string key)
|
||||
{
|
||||
if (_cultureDictionary == null)
|
||||
{
|
||||
var factory = CultureDictionaryFactoryResolver.Current.Factory;
|
||||
_cultureDictionary = factory.CreateDictionary();
|
||||
}
|
||||
return _cultureDictionary[key];
|
||||
return CultureDictionary[key];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the ICultureDictionary for access to dictionary items
|
||||
/// </summary>
|
||||
public ICultureDictionary CultureDictionary
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_cultureDictionary == null)
|
||||
{
|
||||
var factory = CultureDictionaryFactoryResolver.Current.Factory;
|
||||
_cultureDictionary = factory.CreateDictionary();
|
||||
}
|
||||
return _cultureDictionary;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Membership
|
||||
|
||||
@@ -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<string, string> GetChildren(string key)
|
||||
{
|
||||
return new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
public Language Language
|
||||
{
|
||||
get { return Language.GetByCultureCode(System.Threading.Thread.CurrentThread.CurrentUICulture.Name); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user