161 lines
4.7 KiB
C#
161 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Threading;
|
|
|
|
namespace Kreta.Framework
|
|
{
|
|
[Serializable]
|
|
public class LanguageContext
|
|
{
|
|
const int Hungarian = 1038;
|
|
const int Zulu = 1077;
|
|
const int Urdu = 1056;
|
|
|
|
[ThreadStatic]
|
|
static LanguageContext m_Current;
|
|
|
|
static LanguageContext m_DefaultLanguageContext;
|
|
|
|
static Dictionary<string, int> m_LanguageIndexes;
|
|
|
|
readonly int m_LCID;
|
|
CultureInfo m_RegionSettings;
|
|
|
|
public LanguageContext(int lcid)
|
|
{
|
|
m_LCID = lcid;
|
|
SetCulture(CultureInfo.GetCultureInfo(lcid == Zulu || lcid == Urdu ? Hungarian : lcid)); /* A urdu és zulu nyelvet kvázi magyarnak tekintjük. */
|
|
}
|
|
|
|
public LanguageContext() : this(LanguageContext.DefaultLanguageContext.LCID)
|
|
{
|
|
}
|
|
|
|
void SetCulture(CultureInfo cultureInfo)
|
|
{
|
|
if (cultureInfo.IsNeutralCulture)
|
|
{
|
|
SetCulture(CultureInfo.CreateSpecificCulture(cultureInfo.Name));
|
|
}
|
|
m_RegionSettings = cultureInfo;
|
|
}
|
|
|
|
static int GetLanguageIndex(CultureInfo cultureInfo)
|
|
{
|
|
string key = cultureInfo.Name.ToLower();
|
|
if (m_LanguageIndexes.TryGetValue(key, out int value))
|
|
{
|
|
return value;
|
|
}
|
|
|
|
if (!cultureInfo.IsNeutralCulture)
|
|
{
|
|
return GetLanguageIndex(cultureInfo.Parent);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void InitializeLanguageIndexes()
|
|
{
|
|
Dictionary<string, int> result = new Dictionary<string, int>();
|
|
// using( SDA.DataProvider.SDAConnection connection = SDAServer.Instance.CreateConnection() )
|
|
// {
|
|
// connection.Open();
|
|
// using( SDA.DataProvider.SDACommand command = connection.CreateCommand() )
|
|
// {
|
|
// command.CommandText = @"
|
|
//if exists (select 1 from sys.tables where name = 'T_SYSTEMPARAMETER')
|
|
//begin
|
|
// select C_VALUE from T_SYSTEMPARAMETER where C_NAME = 'LANGUAGES'
|
|
//end";
|
|
// string temp = command.ExecuteScalar() as string;
|
|
// if( temp != null )
|
|
// {
|
|
// string[] languages = temp.Split( ',', ';' );
|
|
// for( int index = 0; index < languages.Length; index++ )
|
|
// {
|
|
// result[languages[index].Trim().ToLower()] = index;
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
m_LanguageIndexes = result;
|
|
}
|
|
|
|
public static void Initialize(LanguageContext defaultLanguageContext)
|
|
{
|
|
DefaultLanguageContext = defaultLanguageContext;
|
|
InitializeLanguageIndexes();
|
|
}
|
|
|
|
public static LanguageContext DefaultLanguageContext
|
|
{
|
|
get
|
|
{
|
|
if (m_DefaultLanguageContext == null)
|
|
{
|
|
m_DefaultLanguageContext = new LanguageContext(Thread.CurrentThread.CurrentCulture.LCID);
|
|
}
|
|
return m_DefaultLanguageContext;
|
|
}
|
|
set
|
|
{
|
|
m_DefaultLanguageContext = value ?? throw new ArgumentNullException(nameof(value));
|
|
}
|
|
}
|
|
|
|
public static LanguageContext Current
|
|
{
|
|
get
|
|
{
|
|
if (m_Current != null)
|
|
{
|
|
return m_Current;
|
|
}
|
|
|
|
if (UserContext.Instance != null)
|
|
{
|
|
LanguageContext current = UserContext.Instance.LanguageContext;
|
|
if (current != null)
|
|
{
|
|
return current;
|
|
}
|
|
}
|
|
return DefaultLanguageContext;
|
|
}
|
|
set
|
|
{
|
|
m_Current = value;
|
|
}
|
|
}
|
|
|
|
public CultureInfo RegionSettings
|
|
{
|
|
get
|
|
{
|
|
return m_RegionSettings;
|
|
}
|
|
}
|
|
|
|
public int LCID
|
|
{
|
|
get
|
|
{
|
|
return m_LCID;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// A nyelv rendszerparaméterben definiált indexe.
|
|
/// </summary>
|
|
public int LanguageIndex
|
|
{
|
|
get
|
|
{
|
|
return GetLanguageIndex(m_RegionSettings);
|
|
}
|
|
}
|
|
}
|
|
}
|