2014-12-18 12:45:04 +11:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Linq;
|
2015-05-13 17:33:59 +10:00
|
|
|
|
using System.Threading;
|
2017-05-30 15:46:25 +02:00
|
|
|
|
using Umbraco.Core.Composing;
|
2016-01-06 11:22:15 +01:00
|
|
|
|
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
|
|
|
|
|
|
{
|
2018-07-10 12:43:07 +10:00
|
|
|
|
public static string Localize(this ILocalizedTextService manager, string area, string key)
|
2016-03-15 16:58:13 +01:00
|
|
|
|
{
|
2018-07-10 12:43:07 +10:00
|
|
|
|
var fullKey = string.Join("/", area, key);
|
|
|
|
|
|
return manager.Localize(fullKey, Thread.CurrentThread.CurrentUICulture);
|
2016-03-15 16:58:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-05-13 17:33:59 +10: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>
|
2014-12-18 12:45:04 +11:00
|
|
|
|
/// <param name="tokens"></param>
|
2014-12-17 15:19:03 +11:00
|
|
|
|
/// <returns></returns>
|
2014-12-18 12:45:04 +11:00
|
|
|
|
public static string Localize(this ILocalizedTextService manager, string key, CultureInfo culture, string[] tokens)
|
2014-12-17 15:19:03 +11:00
|
|
|
|
{
|
2014-12-18 12:45:04 +11:00
|
|
|
|
return manager.Localize(key, culture, ConvertToDictionaryVars(tokens));
|
2014-12-17 15:19:03 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-12-18 12:45:04 +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);
|
|
|
|
|
|
}
|
2016-01-06 11:22:15 +01:00
|
|
|
|
|
|
|
|
|
|
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>
|
2016-01-06 11:22:15 +01:00
|
|
|
|
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
|
2016-08-24 12:34:28 +02:00
|
|
|
|
=> _cultureDictionary ?? (_cultureDictionary = Current.CultureDictionaryFactory.CreateDictionary());
|
2014-12-17 15:19:03 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|