From a190d9bc64f040f87e04f8234881cdd1e2b336a1 Mon Sep 17 00:00:00 2001 From: Louis DeJardin Date: Wed, 9 Jun 2010 15:21:50 -0700 Subject: [PATCH] Integrating some localizer support for encoding and pluralization Formatter internally encodes information passed as object[] args as appropriate Resulting LocalizedString marked as html-encoded to prevent double-encoding Extension method for <%:T.Plural("1 thing", "{0} things", n, ...)%> --HG-- branch : dev --- src/Orchard/Localization/LocalizedString.cs | 10 +++++++-- src/Orchard/Localization/Localizer.cs | 10 ++++++++- src/Orchard/Localization/Text.cs | 25 ++++++++++++++++++--- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/Orchard/Localization/LocalizedString.cs b/src/Orchard/Localization/LocalizedString.cs index 8197f4219..e5db95112 100644 --- a/src/Orchard/Localization/LocalizedString.cs +++ b/src/Orchard/Localization/LocalizedString.cs @@ -1,7 +1,8 @@ using System; +using System.Web; namespace Orchard.Localization { - public class LocalizedString : MarshalByRefObject { + public class LocalizedString : MarshalByRefObject, IHtmlString { private readonly string _localized; public LocalizedString(string localized) { @@ -20,6 +21,10 @@ namespace Orchard.Localization { return _localized; } + string IHtmlString.ToHtmlString() { + return _localized; + } + public override int GetHashCode() { var hashCode = 0; if (_localized != null) @@ -31,8 +36,9 @@ namespace Orchard.Localization { if (obj == null || obj.GetType() != GetType()) return false; - var that = (LocalizedString) obj; + var that = (LocalizedString)obj; return string.Equals(_localized, that._localized); } + } } diff --git a/src/Orchard/Localization/Localizer.cs b/src/Orchard/Localization/Localizer.cs index 48cba242c..0e2221263 100644 --- a/src/Orchard/Localization/Localizer.cs +++ b/src/Orchard/Localization/Localizer.cs @@ -1,3 +1,11 @@ +using System.Linq; + namespace Orchard.Localization { public delegate LocalizedString Localizer(string text, params object[] args); -} \ No newline at end of file + + public static class LocalizerExtensions { + public static LocalizedString Plural(this Localizer T, string textSingular, string textPlural, int count, params object[] args) { + return T(count == 1 ? textSingular : textPlural, new object[] { count }.Concat(args).ToArray()); + } + } +} diff --git a/src/Orchard/Localization/Text.cs b/src/Orchard/Localization/Text.cs index 8403d25c4..80bc672dc 100644 --- a/src/Orchard/Localization/Text.cs +++ b/src/Orchard/Localization/Text.cs @@ -1,3 +1,6 @@ +using System; +using System.Globalization; +using System.Linq; using System.Web; using Orchard.Localization.Services; using Orchard.Logging; @@ -23,9 +26,25 @@ namespace Orchard.Localization { string currentCulture = _cultureManager.GetCurrentCulture(HttpContext.Current); var localizedFormat = _resourceManager.GetLocalizedString(_scope, textHint, currentCulture); - return args.Length < 1 - ? new LocalizedString(localizedFormat) - : new LocalizedString(string.Format(localizedFormat, args)); + return args.Length == 0 + ? new LocalizedString(localizedFormat) + : string.Format(GetFormatProvider(currentCulture), localizedFormat, args.Select(Encode).ToArray()); + } + + private static IFormatProvider GetFormatProvider(string currentCulture) { + try { + return CultureInfo.GetCultureInfoByIetfLanguageTag(currentCulture); + } + catch { + return null; + } + } + + static object Encode(object arg) { + if (arg is IFormattable || arg is IHtmlString) { + return arg; + } + return HttpUtility.HtmlEncode(arg); } } } \ No newline at end of file