init
This commit is contained in:
commit
e124a47765
19374 changed files with 9806149 additions and 0 deletions
69
Framework/Localization/EnumLocalizer.cs
Normal file
69
Framework/Localization/EnumLocalizer.cs
Normal file
|
@ -0,0 +1,69 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Kreta.Framework.Caching;
|
||||
|
||||
namespace Kreta.Framework.Localization
|
||||
{
|
||||
/// <summary>
|
||||
/// Felsorolások nyelvesítésére szolgáló <see cref="ILocalizer"/> megvalósítás.
|
||||
/// </summary>
|
||||
internal sealed class EnumLocalizer : ILocalizer
|
||||
{
|
||||
private static object SyncObject = new object();
|
||||
|
||||
private static readonly Dictionary<Enum, FriendlyNameAttribute> friendlyNameAttributeCache = new Dictionary<Enum, FriendlyNameAttribute>();
|
||||
|
||||
/// <summary>
|
||||
/// Az osztály alapértelmezett konstruktora.
|
||||
/// </summary>
|
||||
public EnumLocalizer()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lásd <see cref="ILocalizer.Localize"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">Lásd <see cref="ILocalizer.Localize"/></param>
|
||||
/// <param name="cultureInfo">Lásd <see cref="ILocalizer.Localize"/></param>
|
||||
/// <returns>Lásd <see cref="ILocalizer.Localize"/></returns>
|
||||
string ILocalizer.Localize(object value, CultureInfo cultureInfo)
|
||||
{
|
||||
return Localize((Enum)value, cultureInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lásd <see cref="ILocalizer.Localize"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A nyelvesítés a felsorolás mezőire tett <see cref="FriendlyNameAttribute"/> attribútum
|
||||
/// alapján történik. A felsorolás mezője ilyet nem tartalmaz, akkor egyszerűen a <see cref="Enum.ToString()"/>
|
||||
/// metódus lesz használva.
|
||||
/// </remarks>
|
||||
/// <param name="value">Lásd <see cref="ILocalizer.Localize"/></param>
|
||||
/// <param name="cultureInfo">Lásd <see cref="ILocalizer.Localize"/></param>
|
||||
/// <returns>Lásd <see cref="ILocalizer.Localize"/></returns>
|
||||
public string Localize(Enum value, CultureInfo cultureInfo)
|
||||
{
|
||||
FriendlyNameAttribute attribute;
|
||||
lock (SyncObject)
|
||||
{
|
||||
if (!friendlyNameAttributeCache.ContainsKey(value))
|
||||
{
|
||||
System.Reflection.FieldInfo fieldinfo = value.GetType().GetField(value.ToString());
|
||||
attribute = (FriendlyNameAttribute)fieldinfo.GetCustomAttributes(typeof(FriendlyNameAttribute), false).FirstOrDefault(); // no inheritance
|
||||
friendlyNameAttributeCache[value] = attribute;
|
||||
}
|
||||
else
|
||||
{
|
||||
attribute = friendlyNameAttributeCache[value];
|
||||
}
|
||||
}
|
||||
|
||||
return attribute != null
|
||||
? SDAServer.Instance.CacheManager.AquireCache<StringResourcesCache>().Get(attribute.StringResourceId, cultureInfo.LCID, attribute.DisplayText)
|
||||
: value.ToString();
|
||||
}
|
||||
}
|
||||
}
|
48
Framework/Localization/ExceptionLocalizer.cs
Normal file
48
Framework/Localization/ExceptionLocalizer.cs
Normal file
|
@ -0,0 +1,48 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Kreta.Framework.Caching;
|
||||
|
||||
namespace Kreta.Framework.Localization
|
||||
{
|
||||
/// <summary>
|
||||
/// Kivételek nyelvesítésére szolgáló <see cref="ILocalizer"/> megvalósítás.
|
||||
/// </summary>
|
||||
internal class ExceptionLocalizer : ILocalizer
|
||||
{
|
||||
private static object SyncObject = new object();
|
||||
// <<Monostate>>
|
||||
private static readonly Dictionary<Type, FriendlyNameAttribute> friendlyNameAttributeCache = new Dictionary<Type, FriendlyNameAttribute>();
|
||||
|
||||
string ILocalizer.Localize(object value, CultureInfo cultureInfo)
|
||||
{
|
||||
return Localize((Exception)value, cultureInfo);
|
||||
}
|
||||
|
||||
public virtual string Localize(Exception exception, CultureInfo cultureInfo)
|
||||
{
|
||||
FriendlyNameAttribute attribute;
|
||||
lock (SyncObject)
|
||||
{
|
||||
var exceptionType = exception.GetType();
|
||||
if (!friendlyNameAttributeCache.TryGetValue(exceptionType, out attribute))
|
||||
{
|
||||
attribute = (FriendlyNameAttribute)exceptionType.GetCustomAttributes(typeof(FriendlyNameAttribute), true).FirstOrDefault(); //inherited but allowed only one
|
||||
friendlyNameAttributeCache[exceptionType] = attribute;
|
||||
}
|
||||
}
|
||||
|
||||
if (attribute != null)
|
||||
{
|
||||
if (attribute.StringResourceId == -1)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return SDAServer.Instance.CacheManager.AquireCache<StringResourcesCache>().Get(attribute.StringResourceId, cultureInfo.LCID, attribute.DisplayText);
|
||||
}
|
||||
return exception.Message;
|
||||
}
|
||||
}
|
||||
}
|
34
Framework/Localization/FriendlyNameAttribute.cs
Normal file
34
Framework/Localization/FriendlyNameAttribute.cs
Normal file
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
|
||||
namespace Kreta.Framework.Localization
|
||||
{
|
||||
/// <summary>
|
||||
/// Kultúrafüggő barátságos nevet definiáló attribútum.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
|
||||
public sealed class FriendlyNameAttribute : Attribute
|
||||
{
|
||||
public int StringResourceId
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public string DisplayText
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Az attribútum konstruktora.
|
||||
/// </summary>
|
||||
/// <param name="cultureName">A kultúra neve</param>
|
||||
/// <param name="value">A barátságos név</param>
|
||||
public FriendlyNameAttribute(int stringResourceId, string displayText)
|
||||
{
|
||||
StringResourceId = stringResourceId;
|
||||
DisplayText = displayText;
|
||||
}
|
||||
}
|
||||
}
|
49
Framework/Localization/GenericLocalizer.cs
Normal file
49
Framework/Localization/GenericLocalizer.cs
Normal file
|
@ -0,0 +1,49 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Kreta.Framework.Localization
|
||||
{
|
||||
/// <summary>
|
||||
/// Általános <see cref="ILocalizer"/> megvalósítás.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Az általános megvalósítás egyenértékű az objektumon végrehajtott <see cref="object.ToString()"/> hívással,
|
||||
/// azzal a különbséggel, hogy figyelembe veszi, ha az objektum megvalósítja az <see cref="IFormattable"/>
|
||||
/// felületet.
|
||||
/// </remarks>
|
||||
internal sealed class GenericLocalizer : ILocalizer
|
||||
{
|
||||
/// <summary>
|
||||
/// Az osztály alapértelmezett konstruktora.
|
||||
/// </summary>
|
||||
public GenericLocalizer()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lásd <see cref="ILocalizer.Localize"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">Lásd <see cref="ILocalizer.Localize"/></param>
|
||||
/// <param name="cultureInfo">Lásd <see cref="ILocalizer.Localize"/></param>
|
||||
/// <returns>Lásd <see cref="ILocalizer.Localize"/></returns>
|
||||
public string Localize(object value, CultureInfo cultureInfo)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (value is IFormattable formattable)
|
||||
{
|
||||
if (cultureInfo.IsNeutralCulture)
|
||||
{
|
||||
return formattable.ToString(null, CultureInfo.CreateSpecificCulture(cultureInfo.Name));
|
||||
}
|
||||
|
||||
return formattable.ToString(null, cultureInfo);
|
||||
}
|
||||
|
||||
return value.ToString();
|
||||
}
|
||||
}
|
||||
}
|
17
Framework/Localization/ILocalizable.cs
Normal file
17
Framework/Localization/ILocalizable.cs
Normal file
|
@ -0,0 +1,17 @@
|
|||
using System.Globalization;
|
||||
|
||||
namespace Kreta.Framework.Localization
|
||||
{
|
||||
/// <summary>
|
||||
/// Önmagukat nyelvesíteni képes osztályok felülete. Lásd <see cref="Localizer"/>.
|
||||
/// </summary>
|
||||
public interface ILocalizable
|
||||
{
|
||||
/// <summary>
|
||||
/// Nyelvesíti az objektumot.
|
||||
/// </summary>
|
||||
/// <param name="cultureInfo">A nyelv</param>
|
||||
/// <returns>Az objektum nyelvesítve</returns>
|
||||
string Localize(CultureInfo cultureInfo);
|
||||
}
|
||||
}
|
20
Framework/Localization/ILocalizer.cs
Normal file
20
Framework/Localization/ILocalizer.cs
Normal file
|
@ -0,0 +1,20 @@
|
|||
using System.Globalization;
|
||||
|
||||
namespace Kreta.Framework.Localization
|
||||
{
|
||||
/// <summary>
|
||||
/// A nyelvesítő osztályok felülete. Lásd <see cref="Localizer"/>.
|
||||
/// </summary>
|
||||
public interface ILocalizer
|
||||
{
|
||||
// <<Strategy>>
|
||||
|
||||
/// <summary>
|
||||
/// Nyelvesíti a megadott objektumot.
|
||||
/// </summary>
|
||||
/// <param name="value">A nyelvesítendő objektum</param>
|
||||
/// <param name="cultureInfo">A nyelv</param>
|
||||
/// <returns>Az objektum nyelvesítve</returns>
|
||||
string Localize(object value, CultureInfo cultureInfo);
|
||||
}
|
||||
}
|
184
Framework/Localization/Localizer.cs
Normal file
184
Framework/Localization/Localizer.cs
Normal file
|
@ -0,0 +1,184 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue