DeviceManager/DeviceRepairAndOptimization/Utils/LanguageResouce.cs
2024-05-28 22:36:38 +08:00

97 lines
3.0 KiB
C#

using System.Collections.Generic;
using System.IO;
using System.Text;
namespace DeviceRepairAndOptimization.Utils
{
public class LanguageManager
{
private static Dictionary<string, LanguageResouce> lst;
public static string ResouceText(string FileName, string Key, string defaultText = "")
{
if (lst == null)
lst = new Dictionary<string, LanguageResouce>();
if (lst.Count == 0 || !lst.ContainsKey(FileName))
{
lst.Add(FileName, new LanguageResouce(FileName));
}
return (!lst.ContainsKey(FileName)) ? lst[FileName].GetText(Key, defaultText) : "";
}
public static string ErrorText(string Key, string defaultText = "")
{
if (lst == null)
lst = new Dictionary<string, LanguageResouce>();
if (lst.Count == 0 || !lst.ContainsKey("Error"))
{
lst.Add("Error", new LanguageResouce("Error"));
}
return (!lst.ContainsKey("Error")) ? lst["Error"].GetText(Key, defaultText) : "";
}
public static string CommonText(string Key, string defaultText = "")
{
if (lst == null)
lst = new Dictionary<string, LanguageResouce>();
if (lst.Count == 0 || !lst.ContainsKey("Common"))
{
lst.Add("Common", new LanguageResouce("Common"));
}
return (!lst.ContainsKey("Common")) ? lst["Common"].GetText(Key, defaultText) : "";
}
}
public class LanguageResouce : Dictionary<string, string>
{
public LanguageResouce(string fileName)
{
ReadFileToDictionary(fileName);
}
void ReadFileToDictionary(string fileName)
{
fileName = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Resouces", $"{fileName}.txt");
if (!File.Exists(fileName))
return;
using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read))
{
using (StreamReader m_streamReader = new StreamReader(fs, Encoding.Default))
{
string strLine = m_streamReader.ReadLine();
int ReadEdLine = 0;
while (strLine != null)
{
string[] arr = strLine.Split('=');
if (arr.Length == 2)
{
this.Add(arr[0], arr[1]);
}
strLine = m_streamReader.ReadLine();
ReadEdLine++;
}
}
}
}
public string GetText(string Key, string defaultText = "")
{
if (this.Keys == null || this.Keys.Count == 0 || !this.ContainsKey(Key))
{
return "";
}
else
{
return this[Key];
}
}
}
}