using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Threading; namespace Umbraco.Core.Services { /// /// Extension methods for ILocalizedTextService /// public static class LocalizedTextServiceExtensions { /// /// Localize using the current thread culture /// /// /// /// /// public static string Localize(this ILocalizedTextService manager, string key, string[] tokens) { return manager.Localize(key, Thread.CurrentThread.CurrentUICulture, tokens); } /// /// Localize using the current thread culture /// /// /// /// /// public static string Localize(this ILocalizedTextService manager, string key, IDictionary tokens = null) { return manager.Localize(key, Thread.CurrentThread.CurrentUICulture, tokens); } /// /// Localize a key without any variables /// /// /// /// /// /// public static string Localize(this ILocalizedTextService manager, string key, CultureInfo culture, string[] tokens) { 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); } } }