From da25970f79e3f0d08f6739abbb7adefed1a56827 Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 17 Dec 2014 15:19:03 +1100 Subject: [PATCH] wip - working on U4-5966 --- .../Services/ILocalizedTextService.cs | 26 ++ .../Services/LocalizedTextService.cs | 169 ++++++++++++ .../LocalizedTextServiceExtensions.cs | 24 ++ src/Umbraco.Core/Services/ServiceContext.cs | 23 +- src/Umbraco.Core/Umbraco.Core.csproj | 3 + src/Umbraco.Tests/MockTests.cs | 6 +- .../Services/LocalizedTextServiceTests.cs | 240 ++++++++++++++++++ src/Umbraco.Tests/Umbraco.Tests.csproj | 1 + src/umbraco.businesslogic/ui.cs | 195 +++++++++----- 9 files changed, 626 insertions(+), 61 deletions(-) create mode 100644 src/Umbraco.Core/Services/ILocalizedTextService.cs create mode 100644 src/Umbraco.Core/Services/LocalizedTextService.cs create mode 100644 src/Umbraco.Core/Services/LocalizedTextServiceExtensions.cs create mode 100644 src/Umbraco.Tests/Services/LocalizedTextServiceTests.cs diff --git a/src/Umbraco.Core/Services/ILocalizedTextService.cs b/src/Umbraco.Core/Services/ILocalizedTextService.cs new file mode 100644 index 0000000000..dd6f9de883 --- /dev/null +++ b/src/Umbraco.Core/Services/ILocalizedTextService.cs @@ -0,0 +1,26 @@ +using System.Globalization; + +namespace Umbraco.Core.Services +{ + /// + /// The entry point to localize any key in the text storage source for a given culture + /// + /// + /// This class is created to be as simple as possible so that it can be replaced very easily, + /// all other methods are extension methods that simply call the one underlying method in this class + /// + public interface ILocalizedTextService + { + /// + /// Localize a key with variables + /// + /// + /// + /// This can be null + /// + string Localize(string key, CultureInfo culture, + + //TODO: Potentially this should be a dictionary to simplify things a little? + object variables); + } +} diff --git a/src/Umbraco.Core/Services/LocalizedTextService.cs b/src/Umbraco.Core/Services/LocalizedTextService.cs new file mode 100644 index 0000000000..d9a445b482 --- /dev/null +++ b/src/Umbraco.Core/Services/LocalizedTextService.cs @@ -0,0 +1,169 @@ +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); + 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; + private readonly IDictionary> _xmlSource; + + /// + /// Initializes with a file sources instance + /// + /// + public LocalizedTextService(LocalizedTextServiceFileSources fileSources) + { + _xmlSource = fileSources.GetXmlSources(); + } + + /// + /// Initializes with an XML source + /// + /// + public LocalizedTextService(IDictionary> source) + { + if (source == null) throw new ArgumentNullException("source"); + _xmlSource = source; + } + + /// + /// Initializes with a source of a dictionary of culture -> areas -> sub dictionary of keys/values + /// + /// + public LocalizedTextService(IDictionary>> source) + { + if (source == null) throw new ArgumentNullException("source"); + _dictionarySource = source; + } + + public string Localize(string key, CultureInfo culture, object variables) + { + Mandate.ParameterNotNull(culture, "culture"); + + //This is what the legacy ui service did + if (string.IsNullOrEmpty(key)) + return string.Empty; + + var keyParts = key.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries); + var area = keyParts.Length > 1 ? keyParts[0] : null; + var alias = keyParts.Length > 1 ? keyParts[1] : keyParts[0]; + + if (_xmlSource != null) + { + return GetFromXmlSource(culture, area, alias); + } + else + { + return GetFromDictionarySource(culture, area, alias); + } + } + + private string GetFromDictionarySource(CultureInfo culture, string area, string key) + { + if (_dictionarySource.ContainsKey(culture) == false) + { + throw new NullReferenceException("The culture specified " + culture + " was not found in any configured sources for this service"); + } + + var cultureSource = _dictionarySource[culture]; + + string found; + if (area.IsNullOrWhiteSpace()) + { + found = cultureSource + .SelectMany(x => x.Value) + .Where(keyvals => keyvals.Key.InvariantEquals(key)) + .Select(x => x.Value) + .FirstOrDefault(); + } + else + { + found = cultureSource + .Where(areas => areas.Key.InvariantEquals(area)) + .SelectMany(a => a.Value) + .Where(keyvals => keyvals.Key.InvariantEquals(key)) + .Select(x => x.Value) + .FirstOrDefault(); + } + + //NOTE: Based on how legacy works, the default text does not contain the area, just the key + return found ?? "[" + key + "]"; + } + + private string GetFromXmlSource(CultureInfo culture, string area, string key) + { + if (_xmlSource.ContainsKey(culture) == false) + { + throw new NullReferenceException("The culture specified " + culture + " was not found in any configured sources for this service"); + } + + var cultureSource = _xmlSource[culture].Value; + + var xpath = area.IsNullOrWhiteSpace() + ? string.Format("//key [@alias = '{0}']", key) + : string.Format("//area [@alias = '{0}']/key [@alias = '{1}']", area, key); + + var found = cultureSource.XPathSelectElement(xpath); + + return found == null + //NOTE: Based on how legacy works, the default text does not contain the area, just the key + ? "[" + key + "]" + : found.Value; + } + + + + } +} diff --git a/src/Umbraco.Core/Services/LocalizedTextServiceExtensions.cs b/src/Umbraco.Core/Services/LocalizedTextServiceExtensions.cs new file mode 100644 index 0000000000..82c5e9e643 --- /dev/null +++ b/src/Umbraco.Core/Services/LocalizedTextServiceExtensions.cs @@ -0,0 +1,24 @@ +using System.Globalization; + +namespace Umbraco.Core.Services +{ + + /// + /// Extension methods for ILocalizedTextService + /// + public static class LocalizedTextServiceExtensions + { + /// + /// Localize a key without any variables + /// + /// + /// + /// + /// + public static string Localize(this ILocalizedTextService manager, string key, CultureInfo culture) + { + return manager.Localize(key, culture, null); + } + + } +} diff --git a/src/Umbraco.Core/Services/ServiceContext.cs b/src/Umbraco.Core/Services/ServiceContext.cs index 79280b371f..6dabf8b0b6 100644 --- a/src/Umbraco.Core/Services/ServiceContext.cs +++ b/src/Umbraco.Core/Services/ServiceContext.cs @@ -1,4 +1,6 @@ using System; +using System.IO; +using Umbraco.Core.IO; using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.UnitOfWork; using Umbraco.Core.Publishing; @@ -12,6 +14,7 @@ namespace Umbraco.Core.Services /// public class ServiceContext { + private Lazy _localizedTextService; private Lazy _tagService; private Lazy _contentService; private Lazy _userService; @@ -52,6 +55,7 @@ namespace Umbraco.Core.Services /// /// /// + /// public ServiceContext( IContentService contentService, IMediaService mediaService, @@ -69,8 +73,10 @@ namespace Umbraco.Core.Services ISectionService sectionService, IApplicationTreeService treeService, ITagService tagService, - INotificationService notificationService) + INotificationService notificationService, + ILocalizedTextService localizedTextService) { + _localizedTextService = new Lazy(() => localizedTextService); _tagService = new Lazy(() => tagService); _contentService = new Lazy(() => contentService); _mediaService = new Lazy(() => mediaService); @@ -119,6 +125,12 @@ namespace Umbraco.Core.Services var provider = dbUnitOfWorkProvider; var fileProvider = fileUnitOfWorkProvider; + + + if (_localizedTextService == null) + _localizedTextService = new Lazy(() => new LocalizedTextService( + new LocalizedTextServiceFileSources(cache.RuntimeCache, new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Umbraco + "/config/lang/"))))); + if (_notificationService == null) _notificationService = new Lazy(() => new NotificationService(provider, _userService.Value, _contentService.Value)); @@ -174,11 +186,20 @@ namespace Umbraco.Core.Services if (_tagService == null) _tagService = new Lazy(() => new TagService(provider, repositoryFactory.Value)); + if (_memberGroupService == null) _memberGroupService = new Lazy(() => new MemberGroupService(provider, repositoryFactory.Value)); } + /// + /// Gets the + /// + public ILocalizedTextService TextService + { + get { return _localizedTextService.Value; } + } + /// /// Gets the /// diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index da47e24a80..52c5260bed 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -316,6 +316,9 @@ + + + diff --git a/src/Umbraco.Tests/MockTests.cs b/src/Umbraco.Tests/MockTests.cs index 30fedf2465..95c324fd49 100644 --- a/src/Umbraco.Tests/MockTests.cs +++ b/src/Umbraco.Tests/MockTests.cs @@ -57,7 +57,8 @@ namespace Umbraco.Tests new Mock().Object, new Mock().Object, new Mock().Object, - new Mock().Object); + new Mock().Object, + Mock.Of()); Assert.Pass(); } @@ -103,7 +104,8 @@ namespace Umbraco.Tests new Mock().Object, new Mock().Object, new Mock().Object, - new Mock().Object), + new Mock().Object, + Mock.Of()), CacheHelper.CreateDisabledCacheHelper()); Assert.Pass(); diff --git a/src/Umbraco.Tests/Services/LocalizedTextServiceTests.cs b/src/Umbraco.Tests/Services/LocalizedTextServiceTests.cs new file mode 100644 index 0000000000..38f0f1a1ce --- /dev/null +++ b/src/Umbraco.Tests/Services/LocalizedTextServiceTests.cs @@ -0,0 +1,240 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using NUnit.Framework; +using Umbraco.Core.Services; + +namespace Umbraco.Tests.Services +{ + [TestFixture] + public class LocalizedTextServiceTests + { + + [Test] + public void Using_Dictionary_Returns_Text_With_Area() + { + var culture = CultureInfo.GetCultureInfo("en-US"); + var txtService = new LocalizedTextService( + new Dictionary>> + { + { + culture, new Dictionary> + { + { + "testArea", new Dictionary + { + {"testKey", "testValue"} + } + } + } + } + }); + + var result = txtService.Localize("testArea/testKey", culture); + + Assert.AreEqual("testValue", result); + } + + [Test] + public void Using_Dictionary_Returns_Text_Without_Area() + { + var culture = CultureInfo.GetCultureInfo("en-US"); + var txtService = new LocalizedTextService( + new Dictionary>> + { + { + culture, new Dictionary> + { + { + "testArea", new Dictionary + { + {"testKey", "testValue"} + } + } + } + } + }); + + var result = txtService.Localize("testKey", culture); + + Assert.AreEqual("testValue", result); + } + + [Test] + public void Using_Dictionary_Returns_Default_Text_When_Not_Found_With_Area() + { + var culture = CultureInfo.GetCultureInfo("en-US"); + var txtService = new LocalizedTextService( + new Dictionary>> + { + { + culture, new Dictionary> + { + { + "testArea", new Dictionary + { + {"testKey", "testValue"} + } + } + } + } + }); + + var result = txtService.Localize("testArea/doNotFind", culture); + + //NOTE: Based on how legacy works, the default text does not contain the area, just the key + Assert.AreEqual("[doNotFind]", result); + } + + [Test] + public void Using_Dictionary_Returns_Default_Text_When_Not_Found_Without_Area() + { + var culture = CultureInfo.GetCultureInfo("en-US"); + var txtService = new LocalizedTextService( + new Dictionary>> + { + { + culture, new Dictionary> + { + { + "testArea", new Dictionary + { + {"testKey", "testValue"} + } + } + } + } + }); + + var result = txtService.Localize("doNotFind", culture); + + Assert.AreEqual("[doNotFind]", result); + } + + [Test] + public void Using_XDocument_Returns_Text_With_Area() + { + var culture = CultureInfo.GetCultureInfo("en-US"); + var txtService = new LocalizedTextService( + new Dictionary> + { + { + culture, new Lazy(() => new XDocument( + new XElement("area", new XAttribute("alias", "testArea"), + new XElement("key", new XAttribute("alias", "testKey"), + "testValue")))) + } + }); + + var result = txtService.Localize("testArea/testKey", culture); + + Assert.AreEqual("testValue", result); + } + + [Test] + public void Using_XDocument_Returns_Text_Without_Area() + { + var culture = CultureInfo.GetCultureInfo("en-US"); + var txtService = new LocalizedTextService( + new Dictionary> + { + { + culture, new Lazy(() => new XDocument( + new XElement("area", new XAttribute("alias", "testArea"), + new XElement("key", new XAttribute("alias", "testKey"), + "testValue")))) + } + }); + + var result = txtService.Localize("testKey", culture); + + Assert.AreEqual("testValue", result); + } + + [Test] + public void Using_XDocument_Returns_Default_Text_When_Not_Found_With_Area() + { + var culture = CultureInfo.GetCultureInfo("en-US"); + var txtService = new LocalizedTextService( + new Dictionary> + { + { + culture, new Lazy(() => new XDocument( + new XElement("area", new XAttribute("alias", "testArea"), + new XElement("key", new XAttribute("alias", "testKey"), + "testValue")))) + } + }); + + var result = txtService.Localize("testArea/doNotFind", culture); + + //NOTE: Based on how legacy works, the default text does not contain the area, just the key + Assert.AreEqual("[doNotFind]", result); + } + + [Test] + public void Using_XDocument_Returns_Default_Text_When_Not_Found_Without_Area() + { + var culture = CultureInfo.GetCultureInfo("en-US"); + var txtService = new LocalizedTextService( + new Dictionary> + { + { + culture, new Lazy(() => new XDocument( + new XElement("area", new XAttribute("alias", "testArea"), + new XElement("key", new XAttribute("alias", "testKey"), + "testValue")))) + } + }); + + var result = txtService.Localize("doNotFind", culture); + + Assert.AreEqual("[doNotFind]", result); + } + + [Test] + public void Using_Dictionary_Throws_When_No_Culture_Found() + { + var culture = CultureInfo.GetCultureInfo("en-US"); + var txtService = new LocalizedTextService( + new Dictionary>> + { + { + culture, new Dictionary> + { + { + "testArea", new Dictionary + { + {"testKey", "testValue"} + } + } + } + } + }); + + Assert.Throws(() => txtService.Localize("testArea/testKey", CultureInfo.GetCultureInfo("en-AU"))); + } + + [Test] + public void Using_XDocument_Throws_When_No_Culture_Found() + { + var culture = CultureInfo.GetCultureInfo("en-US"); + var txtService = new LocalizedTextService( + new Dictionary> + { + { + culture, new Lazy(() => new XDocument( + new XElement("area", new XAttribute("alias", "testArea"), + new XElement("key", new XAttribute("alias", "testKey"), + "testValue")))) + } + }); + + Assert.Throws(() => txtService.Localize("testArea/testKey", CultureInfo.GetCultureInfo("en-AU"))); + } + } +} diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 074bb19d71..cf664b243d 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -320,6 +320,7 @@ + diff --git a/src/umbraco.businesslogic/ui.cs b/src/umbraco.businesslogic/ui.cs index 9c03bebc53..509df667aa 100644 --- a/src/umbraco.businesslogic/ui.cs +++ b/src/umbraco.businesslogic/ui.cs @@ -1,4 +1,6 @@ using System; +using System.ComponentModel; +using System.Globalization; using System.IO; using System.Text.RegularExpressions; using System.Threading; @@ -10,6 +12,7 @@ using Umbraco.Core.IO; using Umbraco.Core.Logging; using Umbraco.Core.Models.Membership; using umbraco.BasePages; +using Umbraco.Core.Services; using User = umbraco.BusinessLogic.User; namespace umbraco @@ -17,27 +20,25 @@ namespace umbraco //TODO: Make the User overloads obsolete, then publicize the IUser object - //TODO: Convert all of this over to Niels K's localization framework and put into Core proj. - /// /// The ui class handles the multilingual text in the umbraco back-end. /// Provides access to language settings and language files used in the umbraco back-end. /// + [Obsolete("Use the ITextManager instead which is available on most umbraco base classes and also on the ApplicationContext")] public class ui { private static readonly string UmbracoDefaultUiLanguage = GlobalSettings.DefaultUILanguage; private static readonly string UmbracoPath = SystemDirectories.Umbraco; - /// - /// Gets the current Culture for the logged-in users - /// - /// The user. - /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("Get the current culture/language from the currently logged in IUser, the IUser object is available on most Umbraco base classes and on the UmbracoContext")] public static string Culture(User u) { return Culture(u.Language); } - + + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("Get the current culture/language from the currently logged in IUser, the IUser object is available on most Umbraco base classes and on the UmbracoContext")] internal static string Culture(IUser u) { return Culture(u.Language); @@ -56,18 +57,16 @@ namespace umbraco } } - /// - /// Check if th user is logged in, if they are, return their language specified in the database. - /// If they aren't logged in, check the current thread culture and return it, however if that is - /// null, then return the default Umbraco culture. - /// - /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("Get the current culture/language from the currently logged in IUser, the IUser object is available on most Umbraco base classes and on the UmbracoContext")] private static string GetLanguage() { var user = UmbracoEnsuredPage.CurrentUser; return GetLanguage(user); } + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("Get the current culture/language from the currently logged in IUser, the IUser object is available on most Umbraco base classes and on the UmbracoContext")] private static string GetLanguage(User u) { if (u != null) @@ -77,6 +76,8 @@ namespace umbraco return GetLanguage(""); } + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("Get the current culture/language from the currently logged in IUser, the IUser object is available on most Umbraco base classes and on the UmbracoContext")] private static string GetLanguage(IUser u) { if (u != null) @@ -86,6 +87,8 @@ namespace umbraco return GetLanguage(""); } + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("Get the current culture/language from the currently logged in IUser, the IUser object is available on most Umbraco base classes and on the UmbracoContext")] private static string GetLanguage(string userLanguage) { if (userLanguage.IsNullOrWhiteSpace() == false) @@ -106,12 +109,16 @@ namespace umbraco /// public static string Text(string Key, User u) { - return GetText(string.Empty, Key, null, GetLanguage(u)); + return ApplicationContext.Current.Services.TextService.Localize(Key, CultureInfo.GetCultureInfo(GetLanguage(u))); + + //return GetText(string.Empty, Key, null, GetLanguage(u)); } internal static string Text(string key, IUser u) { - return GetText(string.Empty, key, null, GetLanguage(u)); + return ApplicationContext.Current.Services.TextService.Localize(key, CultureInfo.GetCultureInfo(GetLanguage(u))); + + //return GetText(string.Empty, key, null, GetLanguage(u)); } /// @@ -121,7 +128,9 @@ namespace umbraco /// public static string Text(string Key) { - return GetText(Key); + return ApplicationContext.Current.Services.TextService.Localize(Key, CultureInfo.GetCultureInfo(GetLanguage())); + + //return GetText(Key); } /// @@ -133,12 +142,20 @@ namespace umbraco /// public static string Text(string Area, string Key, User u) { - return GetText(Area, Key, null, GetLanguage(u)); + return ApplicationContext.Current.Services.TextService.Localize( + string.Format("{0}/{1}", Area, Key), + CultureInfo.GetCultureInfo(GetLanguage(u))); + + //return GetText(Area, Key, null, GetLanguage(u)); } public static string Text(string area, string key, IUser u) { - return GetText(area, key, null, GetLanguage(u)); + return ApplicationContext.Current.Services.TextService.Localize( + string.Format("{0}/{1}", area, key), + CultureInfo.GetCultureInfo(GetLanguage(u))); + + //return GetText(area, key, null, GetLanguage(u)); } /// @@ -149,7 +166,11 @@ namespace umbraco /// public static string Text(string Area, string Key) { - return GetText(Area, Key, GetLanguage()); + return ApplicationContext.Current.Services.TextService.Localize( + string.Format("{0}/{1}", Area, Key), + CultureInfo.GetCultureInfo(GetLanguage())); + + //return GetText(Area, Key, GetLanguage()); } /// @@ -162,17 +183,32 @@ namespace umbraco /// public static string Text(string Area, string Key, string[] Variables, User u) { - return GetText(Area, Key, Variables, GetLanguage(u)); + return ApplicationContext.Current.Services.TextService.Localize( + string.Format("{0}/{1}", Area, Key), + CultureInfo.GetCultureInfo(GetLanguage(u)), + ConvertToObjectVars(Variables)); + + //return GetText(Area, Key, Variables, GetLanguage(u)); } internal static string Text(string area, string key, string[] variables) { - return GetText(area, key, variables, GetLanguage((IUser)null)); + return ApplicationContext.Current.Services.TextService.Localize( + string.Format("{0}/{1}", area, key), + CultureInfo.GetCultureInfo(GetLanguage()), + ConvertToObjectVars(variables)); + + //return GetText(area, key, variables, GetLanguage((IUser)null)); } internal static string Text(string area, string key, string[] variables, IUser u) { - return GetText(area, key, variables, GetLanguage(u)); + return ApplicationContext.Current.Services.TextService.Localize( + string.Format("{0}/{1}", area, key), + CultureInfo.GetCultureInfo(GetLanguage(u)), + ConvertToObjectVars(variables)); + + //return GetText(area, key, variables, GetLanguage(u)); } /// @@ -185,17 +221,32 @@ namespace umbraco /// public static string Text(string Area, string Key, string Variable, User u) { - return GetText(Area, Key, new[] { Variable }, GetLanguage(u)); + return ApplicationContext.Current.Services.TextService.Localize( + string.Format("{0}/{1}", Area, Key), + CultureInfo.GetCultureInfo(GetLanguage(u)), + ConvertToObjectVars(new[] { Variable })); + + //return GetText(Area, Key, new[] { Variable }, GetLanguage(u)); } internal static string Text(string area, string key, string variable) { - return GetText(area, key, new[] { variable }, GetLanguage((IUser)null)); + return ApplicationContext.Current.Services.TextService.Localize( + string.Format("{0}/{1}", area, key), + CultureInfo.GetCultureInfo(GetLanguage()), + ConvertToObjectVars(new[] { variable })); + + //return GetText(area, key, new[] { variable }, GetLanguage((IUser)null)); } internal static string Text(string area, string key, string variable, IUser u) { - return GetText(area, key, new[] { variable }, GetLanguage(u)); + return ApplicationContext.Current.Services.TextService.Localize( + string.Format("{0}/{1}", area, key), + CultureInfo.GetCultureInfo(GetLanguage(u)), + ConvertToObjectVars(new[] { variable })); + + //return GetText(area, key, new[] { variable }, GetLanguage(u)); } /// @@ -205,7 +256,9 @@ namespace umbraco /// public static string GetText(string key) { - return GetText(string.Empty, key, null, GetLanguage()); + return ApplicationContext.Current.Services.TextService.Localize(key, CultureInfo.GetCultureInfo(GetLanguage())); + + //return GetText(string.Empty, key, null, GetLanguage()); } /// @@ -216,7 +269,11 @@ namespace umbraco /// public static string GetText(string area, string key) { - return GetText(area, key, null, GetLanguage()); + return ApplicationContext.Current.Services.TextService.Localize( + string.Format("{0}/{1}", area, key), + CultureInfo.GetCultureInfo(GetLanguage())); + + //return GetText(area, key, null, GetLanguage()); } /// @@ -228,7 +285,12 @@ namespace umbraco /// public static string GetText(string area, string key, string[] variables) { - return GetText(area, key, variables, GetLanguage()); + return ApplicationContext.Current.Services.TextService.Localize( + string.Format("{0}/{1}", area, key), + CultureInfo.GetCultureInfo(GetLanguage()), + ConvertToObjectVars(variables)); + + //return GetText(area, key, variables, GetLanguage()); } /// @@ -240,7 +302,12 @@ namespace umbraco /// public static string GetText(string area, string key, string variable) { - return GetText(area, key, new[] { variable }, GetLanguage()); + return ApplicationContext.Current.Services.TextService.Localize( + string.Format("{0}/{1}", area, key), + CultureInfo.GetCultureInfo(GetLanguage()), + ConvertToObjectVars(new[] { variable })); + + //return GetText(area, key, new[] { variable }, GetLanguage()); } /// @@ -254,41 +321,47 @@ namespace umbraco /// This is the underlying call for all Text/GetText method calls public static string GetText(string area, string key, string[] variables, string language) { - if (string.IsNullOrEmpty(key)) - return string.Empty; - if (string.IsNullOrEmpty(language)) - language = Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName; + return ApplicationContext.Current.Services.TextService.Localize( + string.Format("{0}/{1}", area, key), + CultureInfo.GetCultureInfo(GetLanguage()), + ConvertToObjectVars(variables)); - var langFile = getLanguageFile(language); - if (langFile != null) - { - XmlNode node; - if (string.IsNullOrEmpty(area)) - { - node = langFile.SelectSingleNode(string.Format("//key [@alias = '{0}']", key)); - } - else - { - node = langFile.SelectSingleNode(string.Format("//area [@alias = '{0}']/key [@alias = '{1}']", area, key)); - } + //if (string.IsNullOrEmpty(key)) + // return string.Empty; - if (node != null) - { - if (variables != null && variables.Length > 0) - { - return GetStringWithVars(node, variables); - } - return xmlHelper.GetNodeValue(node); - } - } - return "[" + key + "]"; + //if (string.IsNullOrEmpty(language)) + // language = Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName; + + //var langFile = getLanguageFile(language); + + //if (langFile != null) + //{ + // XmlNode node; + // if (string.IsNullOrEmpty(area)) + // { + // node = langFile.SelectSingleNode(string.Format("//key [@alias = '{0}']", key)); + // } + // else + // { + // node = langFile.SelectSingleNode(string.Format("//area [@alias = '{0}']/key [@alias = '{1}']", area, key)); + // } + + // if (node != null) + // { + // if (variables != null && variables.Length > 0) + // { + // return GetStringWithVars(node, variables); + // } + // return xmlHelper.GetNodeValue(node); + // } + //} + //return "[" + key + "]"; } - private static string GetStringWithVars(XmlNode node, string[] variables) + private static string GetStringWithVars(string stringWithVars, string[] variables) { - var stringWithVars = xmlHelper.GetNodeValue(node); var vars = Regex.Matches(stringWithVars, @"\%(\d)\%", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); foreach (Match var in vars) @@ -341,5 +414,11 @@ namespace umbraco } + + internal static object ConvertToObjectVars(string[] variables) + { + throw new NotImplementedException(); + } + } } \ No newline at end of file