diff --git a/src/Umbraco.Core/Cache/DefaultRepositoryCachePolicy.cs b/src/Umbraco.Core/Cache/DefaultRepositoryCachePolicy.cs index fda57cefee..a5777a5775 100644 --- a/src/Umbraco.Core/Cache/DefaultRepositoryCachePolicy.cs +++ b/src/Umbraco.Core/Cache/DefaultRepositoryCachePolicy.cs @@ -35,8 +35,6 @@ namespace Umbraco.Core.Cache public void CreateOrUpdate(TEntity entity, Action persistMethod) { - var cacheKey = GetCacheIdKey(entity.Id); - try { persistMethod(entity); @@ -44,7 +42,12 @@ namespace Umbraco.Core.Cache //set the disposal action SetCacheAction(() => { - Cache.InsertCacheItem(cacheKey, () => entity); + //just to be safe, we cannot cache an item without an identity + if (entity.HasIdentity) + { + Cache.InsertCacheItem(GetCacheIdKey(entity.Id), () => entity); + } + //If there's a GetAllCacheAllowZeroCount cache, ensure it is cleared Cache.ClearCacheItem(GetCacheTypeKey()); }); @@ -57,7 +60,7 @@ namespace Umbraco.Core.Cache { //if an exception is thrown we need to remove the entry from cache, this is ONLY a work around because of the way // that we cache entities: http://issues.umbraco.org/issue/U4-4259 - Cache.ClearCacheItem(cacheKey); + Cache.ClearCacheItem(GetCacheIdKey(entity.Id)); //If there's a GetAllCacheAllowZeroCount cache, ensure it is cleared Cache.ClearCacheItem(GetCacheTypeKey()); @@ -188,7 +191,14 @@ namespace Umbraco.Core.Cache /// protected virtual void SetCacheAction(string cacheKey, TEntity entity) { - SetCacheAction(() => Cache.InsertCacheItem(cacheKey, () => entity)); + SetCacheAction(() => + { + //just to be safe, we cannot cache an item without an identity + if (entity.HasIdentity) + { + Cache.InsertCacheItem(cacheKey, () => entity); + } + }); } /// @@ -214,7 +224,11 @@ namespace Umbraco.Core.Cache foreach (var entity in entityCollection.WhereNotNull()) { var localCopy = entity; - Cache.InsertCacheItem(GetCacheIdKey(entity.Id), () => localCopy); + //just to be safe, we cannot cache an item without an identity + if (localCopy.HasIdentity) + { + Cache.InsertCacheItem(GetCacheIdKey(entity.Id), () => localCopy); + } } } }); diff --git a/src/Umbraco.Core/Services/LocalizedTextService.cs b/src/Umbraco.Core/Services/LocalizedTextService.cs index 886fa47cbf..84ff3b745b 100644 --- a/src/Umbraco.Core/Services/LocalizedTextService.cs +++ b/src/Umbraco.Core/Services/LocalizedTextService.cs @@ -112,19 +112,15 @@ namespace Umbraco.Core.Services } //convert all areas + keys to a single key with a '/' - var areas = xmlSource[culture].Value.XPathSelectElements("//area"); - foreach (var area in areas) + result = GetStoredTranslations(xmlSource, culture); + + //merge with the english file in case there's keys in there that don't exist in the local file + var englishCulture = new CultureInfo("en-US"); + if (culture.Equals(englishCulture) == false) { - var keys = area.XPathSelectElements("./key"); - foreach (var key in keys) - { - var dictionaryKey = string.Format("{0}/{1}", (string) area.Attribute("alias"), (string) key.Attribute("alias")); - //there could be duplicates if the language file isn't formatted nicely - which is probably the case for quite a few lang files - if (result.ContainsKey(dictionaryKey) == false) - { - result.Add(dictionaryKey, key.Value); - } - } + var englishResults = GetStoredTranslations(xmlSource, englishCulture); + foreach (var englishResult in englishResults.Where(englishResult => result.ContainsKey(englishResult.Key) == false)) + result.Add(englishResult.Key, englishResult.Value); } } else @@ -153,6 +149,25 @@ namespace Umbraco.Core.Services return result; } + private Dictionary GetStoredTranslations(IDictionary> xmlSource, CultureInfo cult) + { + var result = new Dictionary(); + var areas = xmlSource[cult].Value.XPathSelectElements("//area"); + foreach (var area in areas) + { + var keys = area.XPathSelectElements("./key"); + foreach (var key in keys) + { + var dictionaryKey = string.Format("{0}/{1}", (string)area.Attribute("alias"), + (string)key.Attribute("alias")); + //there could be duplicates if the language file isn't formatted nicely - which is probably the case for quite a few lang files + if (result.ContainsKey(dictionaryKey) == false) + result.Add(dictionaryKey, key.Value); + } + } + return result; + } + /// /// Returns a list of all currently supported cultures /// diff --git a/src/Umbraco.Web/Editors/BackOfficeController.cs b/src/Umbraco.Web/Editors/BackOfficeController.cs index 412e09ec52..4ffe57a924 100644 --- a/src/Umbraco.Web/Editors/BackOfficeController.cs +++ b/src/Umbraco.Web/Editors/BackOfficeController.cs @@ -5,6 +5,7 @@ using System.Globalization; using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using System.Web; using System.Web.Configuration; @@ -100,7 +101,8 @@ namespace Umbraco.Web.Editors var cultureInfo = string.IsNullOrWhiteSpace(culture) //if the user is logged in, get their culture, otherwise default to 'en' ? Security.IsAuthenticated() - ? Security.CurrentUser.GetUserCulture(Services.TextService) + //current culture is set at the very beginning of each request + ? Thread.CurrentThread.CurrentCulture : CultureInfo.GetCultureInfo("en") : CultureInfo.GetCultureInfo(culture);