From 5b9a98ad6ae9e63322c26f7b162204e34f7fcb54 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 18 Dec 2014 12:45:04 +1100 Subject: [PATCH] Simplifies the ILocalizedTextService to not have an extension method for null tokens, adds another extension method to accept an array of tokens, adds null checks. --- .../Services/ILocalizedTextService.cs | 2 +- .../Services/LocalizedTextService.cs | 46 +---------------- .../LocalizedTextServiceExtensions.cs | 22 ++++++-- .../LocalizedTextServiceFileSources.cs | 51 +++++++++++++++++++ src/Umbraco.Core/Umbraco.Core.csproj | 3 +- src/umbraco.businesslogic/ui.cs | 23 +++++---- 6 files changed, 87 insertions(+), 60 deletions(-) create mode 100644 src/Umbraco.Core/Services/LocalizedTextServiceFileSources.cs diff --git a/src/Umbraco.Core/Services/ILocalizedTextService.cs b/src/Umbraco.Core/Services/ILocalizedTextService.cs index 47cf9a1cb0..de95f24efa 100644 --- a/src/Umbraco.Core/Services/ILocalizedTextService.cs +++ b/src/Umbraco.Core/Services/ILocalizedTextService.cs @@ -20,7 +20,7 @@ namespace Umbraco.Core.Services /// /// This can be null /// - string Localize(string key, CultureInfo culture, IDictionary tokens); + string Localize(string key, CultureInfo culture, IDictionary tokens = null); /// /// Returns all key/values in storage for the given culture diff --git a/src/Umbraco.Core/Services/LocalizedTextService.cs b/src/Umbraco.Core/Services/LocalizedTextService.cs index 22d57928f9..f5c2cc033a 100644 --- a/src/Umbraco.Core/Services/LocalizedTextService.cs +++ b/src/Umbraco.Core/Services/LocalizedTextService.cs @@ -1,59 +1,15 @@ using System; using System.Collections.Generic; using System.Globalization; -using System.IO; using System.Linq; using System.Xml; using System.Xml.Linq; using System.Xml.XPath; -using Umbraco.Core.Cache; namespace Umbraco.Core.Services { //TODO: Convert all of this over to Niels K's localization framework one day - /// - /// Exposes the XDocument sources from files for the default localization text service and ensure caching is taken care of - /// - public class LocalizedTextServiceFileSources - { - private readonly IRuntimeCacheProvider _cache; - private readonly DirectoryInfo _fileSourceFolder; - - public LocalizedTextServiceFileSources(IRuntimeCacheProvider cache, DirectoryInfo fileSourceFolder) - { - if (cache == null) throw new ArgumentNullException("cache"); - if (fileSourceFolder == null) throw new ArgumentNullException("fileSourceFolder"); - _cache = cache; - _fileSourceFolder = fileSourceFolder; - } - - /// - /// returns all xml sources for all culture files found in the folder - /// - /// - public IDictionary> GetXmlSources() - { - var result = new Dictionary>(); - foreach (var fileInfo in _fileSourceFolder.GetFiles("*.xml")) - { - var localCopy = fileInfo; - var filename = Path.GetFileNameWithoutExtension(localCopy.FullName).Replace("_", "-"); - var culture = CultureInfo.GetCultureInfo(filename); - //get the lazy value from cache - result.Add(culture, new Lazy(() => _cache.GetCacheItem( - string.Format("{0}-{1}", typeof (LocalizedTextServiceFileSources).Name, culture.TwoLetterISOLanguageName), () => - { - using (var fs = localCopy.OpenRead()) - { - return XDocument.Load(fs); - } - }, isSliding: true, timeout: TimeSpan.FromMinutes(10), dependentFiles: new[] {localCopy.FullName}))); - } - return result; - } - } - public class LocalizedTextService : ILocalizedTextService { private readonly IDictionary>> _dictionarySource; @@ -88,7 +44,7 @@ namespace Umbraco.Core.Services _dictionarySource = source; } - public string Localize(string key, CultureInfo culture, IDictionary tokens) + public string Localize(string key, CultureInfo culture, IDictionary tokens = null) { Mandate.ParameterNotNull(culture, "culture"); diff --git a/src/Umbraco.Core/Services/LocalizedTextServiceExtensions.cs b/src/Umbraco.Core/Services/LocalizedTextServiceExtensions.cs index 82c5e9e643..6ed20b2a45 100644 --- a/src/Umbraco.Core/Services/LocalizedTextServiceExtensions.cs +++ b/src/Umbraco.Core/Services/LocalizedTextServiceExtensions.cs @@ -1,4 +1,6 @@ -using System.Globalization; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; namespace Umbraco.Core.Services { @@ -14,11 +16,25 @@ namespace Umbraco.Core.Services /// /// /// + /// /// - public static string Localize(this ILocalizedTextService manager, string key, CultureInfo culture) + public static string Localize(this ILocalizedTextService manager, string key, CultureInfo culture, string[] tokens) { - return manager.Localize(key, culture, null); + return manager.Localize(key, culture, ConvertToDictionaryVars(tokens)); } + /// + /// Convert an array of strings to a dictionary of indicies -> values + /// + /// + /// + internal static IDictionary ConvertToDictionaryVars(string[] variables) + { + if (variables == null) return null; + if (variables.Any() == false) return null; + + return variables.Select((s, i) => new { index = i.ToString(CultureInfo.InvariantCulture), value = s }) + .ToDictionary(keyvals => keyvals.index, keyvals => keyvals.value); + } } } diff --git a/src/Umbraco.Core/Services/LocalizedTextServiceFileSources.cs b/src/Umbraco.Core/Services/LocalizedTextServiceFileSources.cs new file mode 100644 index 0000000000..147a3cda31 --- /dev/null +++ b/src/Umbraco.Core/Services/LocalizedTextServiceFileSources.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Xml.Linq; +using Umbraco.Core.Cache; + +namespace Umbraco.Core.Services +{ + /// + /// Exposes the XDocument sources from files for the default localization text service and ensure caching is taken care of + /// + public class LocalizedTextServiceFileSources + { + private readonly IRuntimeCacheProvider _cache; + private readonly DirectoryInfo _fileSourceFolder; + + public LocalizedTextServiceFileSources(IRuntimeCacheProvider cache, DirectoryInfo fileSourceFolder) + { + if (cache == null) throw new ArgumentNullException("cache"); + if (fileSourceFolder == null) throw new ArgumentNullException("fileSourceFolder"); + _cache = cache; + _fileSourceFolder = fileSourceFolder; + } + + /// + /// returns all xml sources for all culture files found in the folder + /// + /// + public IDictionary> GetXmlSources() + { + var result = new Dictionary>(); + foreach (var fileInfo in _fileSourceFolder.GetFiles("*.xml")) + { + var localCopy = fileInfo; + var filename = Path.GetFileNameWithoutExtension(localCopy.FullName).Replace("_", "-"); + var culture = CultureInfo.GetCultureInfo(filename); + //get the lazy value from cache + result.Add(culture, new Lazy(() => _cache.GetCacheItem( + string.Format("{0}-{1}", typeof (LocalizedTextServiceFileSources).Name, culture.TwoLetterISOLanguageName), () => + { + using (var fs = localCopy.OpenRead()) + { + return XDocument.Load(fs); + } + }, isSliding: true, timeout: TimeSpan.FromMinutes(10), dependentFiles: new[] {localCopy.FullName}))); + } + return result; + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 52c5260bed..7bd9b9ee1a 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -318,7 +318,6 @@ - @@ -1073,6 +1072,8 @@ + + diff --git a/src/umbraco.businesslogic/ui.cs b/src/umbraco.businesslogic/ui.cs index 24dc9933ad..e6bfbb2435 100644 --- a/src/umbraco.businesslogic/ui.cs +++ b/src/umbraco.businesslogic/ui.cs @@ -159,7 +159,7 @@ namespace umbraco return ApplicationContext.Current.Services.TextService.Localize( string.Format("{0}/{1}", Area, Key), GetCultureFromUserLanguage(GetLanguage(u)), - ConvertToObjectVars(Variables)); + ConvertToDictionaryVars(Variables)); } internal static string Text(string area, string key, string[] variables) @@ -167,7 +167,7 @@ namespace umbraco return ApplicationContext.Current.Services.TextService.Localize( string.Format("{0}/{1}", area, key), GetCultureFromUserLanguage(GetLanguage()), - ConvertToObjectVars(variables)); + ConvertToDictionaryVars(variables)); } internal static string Text(string area, string key, string[] variables, IUser u) @@ -175,7 +175,7 @@ namespace umbraco return ApplicationContext.Current.Services.TextService.Localize( string.Format("{0}/{1}", area, key), GetCultureFromUserLanguage(GetLanguage(u)), - ConvertToObjectVars(variables)); + ConvertToDictionaryVars(variables)); } /// @@ -191,7 +191,7 @@ namespace umbraco return ApplicationContext.Current.Services.TextService.Localize( string.Format("{0}/{1}", Area, Key), GetCultureFromUserLanguage(GetLanguage(u)), - ConvertToObjectVars(new[] { Variable })); + ConvertToDictionaryVars(new[] { Variable })); } internal static string Text(string area, string key, string variable) @@ -199,7 +199,7 @@ namespace umbraco return ApplicationContext.Current.Services.TextService.Localize( string.Format("{0}/{1}", area, key), GetCultureFromUserLanguage(GetLanguage()), - ConvertToObjectVars(new[] { variable })); + ConvertToDictionaryVars(new[] { variable })); } internal static string Text(string area, string key, string variable, IUser u) @@ -207,7 +207,7 @@ namespace umbraco return ApplicationContext.Current.Services.TextService.Localize( string.Format("{0}/{1}", area, key), GetCultureFromUserLanguage(GetLanguage(u)), - ConvertToObjectVars(new[] { variable })); + ConvertToDictionaryVars(new[] { variable })); } /// @@ -245,7 +245,7 @@ namespace umbraco return ApplicationContext.Current.Services.TextService.Localize( string.Format("{0}/{1}", area, key), GetCultureFromUserLanguage(GetLanguage()), - ConvertToObjectVars(variables)); + ConvertToDictionaryVars(variables)); } /// @@ -260,7 +260,7 @@ namespace umbraco return ApplicationContext.Current.Services.TextService.Localize( string.Format("{0}/{1}", area, key), GetCultureFromUserLanguage(GetLanguage()), - ConvertToObjectVars(new[] { variable })); + ConvertToDictionaryVars(new[] { variable })); } /// @@ -277,7 +277,7 @@ namespace umbraco return ApplicationContext.Current.Services.TextService.Localize( string.Format("{0}/{1}", area, key), GetCultureFromUserLanguage(GetLanguage()), - ConvertToObjectVars(variables)); + ConvertToDictionaryVars(variables)); } /// @@ -327,8 +327,11 @@ namespace umbraco /// /// /// - internal static IDictionary ConvertToObjectVars(string[] variables) + internal static IDictionary ConvertToDictionaryVars(string[] variables) { + if (variables == null) return null; + if (variables.Any() == false) return null; + return variables.Select((s, i) => new {index = i.ToString(CultureInfo.InvariantCulture), value = s}) .ToDictionary(keyvals => keyvals.index, keyvals => keyvals.value); }