using System.Collections.Generic; using System.IO; using System.Text; namespace DeviceRepairAndOptimization.Utils { public class LanguageManager { private static Dictionary lst; public static string ResouceText(string FileName, string Key, string defaultText = "") { if (lst == null) lst = new Dictionary(); 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(); 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(); 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 { 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]; } } } }