Files
Umbraco-CMS/src/Umbraco.Core/Services/LocalizedTextServiceExtensions.cs

105 lines
4.1 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
2017-05-30 15:46:25 +02:00
using Umbraco.Core.Composing;
using Umbraco.Core.Dictionary;
2014-12-17 15:19:03 +11:00
namespace Umbraco.Core.Services
{
/// <summary>
/// Extension methods for ILocalizedTextService
/// </summary>
public static class LocalizedTextServiceExtensions
{
public static string Localize(this ILocalizedTextService manager, string area, string key)
2016-03-15 16:58:13 +01:00
{
var fullKey = string.Join("/", area, key);
return manager.Localize(fullKey, Thread.CurrentThread.CurrentUICulture);
2016-03-15 16:58:13 +01:00
}
/// <summary>
/// Localize using the current thread culture
/// </summary>
/// <param name="manager"></param>
/// <param name="key"></param>
/// <param name="tokens"></param>
/// <returns></returns>
public static string Localize(this ILocalizedTextService manager, string key, string[] tokens)
{
return manager.Localize(key, Thread.CurrentThread.CurrentUICulture, tokens);
}
/// <summary>
/// Localize using the current thread culture
/// </summary>
/// <param name="manager"></param>
/// <param name="key"></param>
/// <param name="tokens"></param>
/// <returns></returns>
public static string Localize(this ILocalizedTextService manager, string key, IDictionary<string, string> tokens = null)
{
return manager.Localize(key, Thread.CurrentThread.CurrentUICulture, tokens);
}
2014-12-17 15:19:03 +11:00
/// <summary>
/// Localize a key without any variables
/// </summary>
/// <param name="manager"></param>
/// <param name="key"></param>
/// <param name="culture"></param>
/// <param name="tokens"></param>
2014-12-17 15:19:03 +11:00
/// <returns></returns>
public static string Localize(this ILocalizedTextService manager, string key, CultureInfo culture, string[] tokens)
2014-12-17 15:19:03 +11:00
{
return manager.Localize(key, culture, ConvertToDictionaryVars(tokens));
2014-12-17 15:19:03 +11:00
}
/// <summary>
/// Convert an array of strings to a dictionary of indicies -> values
/// </summary>
/// <param name="variables"></param>
/// <returns></returns>
internal static IDictionary<string, string> 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);
}
private static ICultureDictionary _cultureDictionary;
2016-01-06 12:24:09 +01:00
/// <summary>
/// TODO: We need to refactor how we work with ICultureDictionary - this is supposed to be the 'fast' way to
/// do readonly access to the Dictionary without using the ILocalizationService. See TODO Notes in `DefaultCultureDictionary`
/// Also NOTE that the ICultureDictionary is based on the ILocalizationService not the ILocalizedTextService (which is used
/// only for the localization files - not the dictionary)
/// </summary>
/// <param name="manager"></param>
/// <param name="text"></param>
/// <returns></returns>
internal static string UmbracoDictionaryTranslate(this ILocalizedTextService manager, string text)
{
var cultureDictionary = CultureDictionary;
return UmbracoDictionaryTranslate(text, cultureDictionary);
}
private static string UmbracoDictionaryTranslate(string text, ICultureDictionary cultureDictionary)
{
if (text == null)
return null;
if (text.StartsWith("#") == false)
return text;
text = text.Substring(1);
return cultureDictionary[text].IfNullOrWhiteSpace(text);
}
2017-07-20 11:21:28 +02:00
private static ICultureDictionary CultureDictionary
=> _cultureDictionary ?? (_cultureDictionary = Current.CultureDictionaryFactory.CreateDictionary());
2014-12-17 15:19:03 +11:00
}
}