You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
namespace Mesnac.Action.Base
{
public class LanguageService
{
private Dictionary<int, string> _languageDic { get; set; }
#region 单例模式
private static LanguageService _this;
public static LanguageService Instance
{
get
{
if (null == _this)
_this = new LanguageService();
return _this;
}
}
private LanguageService()
{
this._languageDic = new Dictionary<int, string>();
}
#endregion
#region 基本信息
private string _projectWizardName = string.Empty;
public string ProjectWizardName
{
get
{
return _projectWizardName;
}
set
{
if (_projectWizardName != value)
{
this._languageDic.Clear();
_projectWizardName = value;
}
}
}
private readonly string ActionConfigPath = "Data\\EventConfig\\";
private string GetConfigFilePath(string path)
{
string appPath = System.Windows.Forms.Application.StartupPath;
return Path.Combine(appPath, ActionConfigPath, path);
}
#endregion
#region IniLanguageDic
private void IniLanguageDic(FileInfo fi)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(fi.FullName);
foreach (XmlNode serviceNode in xmlDocument)
{
if (!serviceNode.Name.Equals("Language", StringComparison.CurrentCultureIgnoreCase))
{
continue;
}
foreach (XmlNode runtimeNode in serviceNode.ChildNodes)
{
if (runtimeNode.Attributes != null
&& runtimeNode.Attributes.Count > 1
&& runtimeNode.Attributes["key"] != null
&& runtimeNode.Attributes["key"].Value != null
&& runtimeNode.Attributes["value"] != null
&& runtimeNode.Attributes["value"].Value != null)
{
int key = 0;
if (int.TryParse(runtimeNode.Attributes["key"].Value.ToString(), out key))
{
if (!this._languageDic.ContainsKey(key))
{
this._languageDic.Add(key, runtimeNode.Attributes["value"].Value.ToString());
}
}
}
}
}
}
private void IniLanguageDic()
{
if (this._languageDic.Count > 0)
{
return;
}
this._languageDic.Clear();
DirectoryInfo dir = new DirectoryInfo(GetConfigFilePath(this._projectWizardName));
if (dir.Exists)
{
foreach (FileInfo fi in dir.GetFiles())
{
IniLanguageDic(fi);
}
}
}
#endregion
public string Read(int key)
{
if (this._languageDic.Count == 0)
{
IniLanguageDic();
}
string Result = string.Empty;
if (this._languageDic.TryGetValue(key, out Result))
{
return Result;
}
return Result;
}
public string Read(string projectWizardName, int key)
{
this.ProjectWizardName = projectWizardName;
return Read(key);
}
}
}