184 lines
6.1 KiB
C#
184 lines
6.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Globalization;
|
|
|
|
namespace Kreta.Framework.Localization
|
|
{
|
|
/// <summary>
|
|
/// Objektumok nyelvesítését koordináló osztály.
|
|
/// </summary>
|
|
public sealed class Localizer
|
|
{
|
|
private static Hashtable m_RegisteredLocalizers = Hashtable.Synchronized(new Hashtable());
|
|
private static Hashtable m_LocalizerCache = Hashtable.Synchronized(new Hashtable());
|
|
private static ILocalizer m_DefaultLocalizer = new GenericLocalizer();
|
|
|
|
/// <summary>
|
|
/// Az osztály statikus konstruktora.
|
|
/// </summary>
|
|
static Localizer()
|
|
{
|
|
SetLocalizer(typeof(Exception), new ExceptionLocalizer());
|
|
SetLocalizer(typeof(Enum), new EnumLocalizer());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Az osztály alapértelmezett konstruktora.
|
|
/// </summary>
|
|
/// <remarks>Az osztály statikus, példányosítását nem szándékozzuk.</remarks>
|
|
private Localizer()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Visszaadja a típushoz regisztrált nyelvesítő objektumot.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Ha a típushoz nem tartozik explicit regisztrált nyelvesítő, akkor az
|
|
/// ősosztály nyelvesítőjét fogja visszaadni (rekurzívan). Ha ilyen sincs,
|
|
/// akkor az alapértelmezett nyelvesítővel fog visszatérni.
|
|
/// </remarks>
|
|
/// <param name="type">A típus</param>
|
|
/// <returns>A típus nyelvesítője</returns>
|
|
private static ILocalizer FindRegisteredLocalizer(Type type)
|
|
{
|
|
ILocalizer result = m_RegisteredLocalizers[type] as ILocalizer;
|
|
if (result != null)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
if (type != typeof(object))
|
|
{
|
|
return FindRegisteredLocalizer(type.BaseType);
|
|
}
|
|
|
|
return m_DefaultLocalizer;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Az alapértelmezett nyelvesítő objektum, értéke sosem lehet null.
|
|
/// </summary>
|
|
public static ILocalizer DefaultLocalizer
|
|
{
|
|
get
|
|
{
|
|
return m_DefaultLocalizer;
|
|
}
|
|
set
|
|
{
|
|
m_DefaultLocalizer = value ?? throw new ArgumentNullException(nameof(value));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Visszaadja a típushoz tartozó nyelvesítőt.
|
|
/// </summary>
|
|
/// <param name="type">A típus</param>
|
|
/// <returns>A típus nyelvesítője</returns>
|
|
public static ILocalizer GetLocalizer(Type type)
|
|
{
|
|
ILocalizer result = m_LocalizerCache[type] as ILocalizer;
|
|
|
|
if (result != null)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
result = FindRegisteredLocalizer(type);
|
|
m_LocalizerCache[type] = result;
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Beállítja egy típushoz a hozzá tartozó nyelvesítő objektumot.
|
|
/// </summary>
|
|
/// <param name="type">A típus</param>
|
|
/// <param name="localizer">A típus nyelvesítője</param>
|
|
public static void SetLocalizer(Type type, ILocalizer localizer)
|
|
{
|
|
if (type == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(type));
|
|
}
|
|
|
|
m_RegisteredLocalizers[type] = localizer;
|
|
m_LocalizerCache.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Nyelvesíti a megadott objektumot.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A nyelvként a <see cref="System.Globalization.CultureInfo.CurrentUICulture"/>
|
|
/// értéke lesz használva.
|
|
/// </remarks>
|
|
/// <param name="value">A nyelvesítendő objektum</param>
|
|
/// <returns>Az objektum nyelvesítve</returns>
|
|
public static string Localize(object value)
|
|
{
|
|
return Localize(value, LanguageContext.Current.RegionSettings);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Nyelvesíti a megadott objektumot.
|
|
/// </summary>
|
|
/// <param name="value">A nyelvesítendő objektum</param>
|
|
/// <param name="cultureName">A kultúra neve</param>
|
|
/// <returns>Az objektum nyelvesítve</returns>
|
|
public static string Localize(object value, string cultureName)
|
|
{
|
|
return Localize(value, CultureInfo.GetCultureInfo(cultureName));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Nyelvesíti a megadott objektumot.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// Ha az objektum megvalósítja az <see cref="ILocalizable"/> fületet, akkor azt használja;
|
|
/// egyébként megkeresi a megfelelő - előzőleg beregisztrált <see cref="ILocalizer"/> -
|
|
/// objektumos, és azon keresztül történik a nyelvesítés.
|
|
/// </para>
|
|
/// </remarks>
|
|
/// <param name="value">A nyelvesítendő objektum</param>
|
|
/// <param name="cultureInfo">A nyelv</param>
|
|
/// <returns>Az objektum nyelvesítve</returns>
|
|
/// <exception cref="ArgumentNullException">Ha valamelyik paraméter értéke null.</exception>
|
|
public static string Localize(object value, CultureInfo cultureInfo)
|
|
{
|
|
if (value == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(value));
|
|
}
|
|
|
|
if (cultureInfo == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(cultureInfo));
|
|
}
|
|
|
|
ILocalizable localizable = value as ILocalizable;
|
|
|
|
string result;
|
|
if (localizable != null)
|
|
{
|
|
result = localizable.Localize(cultureInfo);
|
|
if (result != null)
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
|
|
ILocalizer localizer = GetLocalizer(value.GetType());
|
|
|
|
result = localizer.Localize(value, cultureInfo);
|
|
|
|
if (result != null || localizer == m_DefaultLocalizer)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
return m_DefaultLocalizer.Localize(value, cultureInfo);
|
|
}
|
|
}
|
|
}
|