LanguageManager.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace FactorioModManager
  10. {
  11. public class LanguageManager
  12. {
  13. /// <summary>
  14. /// Path to the folder with language files relative to the CURRENT program folder
  15. /// </summary>
  16. public static string LanguageDir { get; set; } = "lang";
  17. /// <summary>
  18. /// List of available languages
  19. /// </summary>
  20. public static List<string> AvailableLangs { get { return _langs; } }
  21. private static List<string> _langs = new List<string>();
  22. private static Dictionary<string, string> _lines = new Dictionary<string, string>();
  23. private static string _current_lang = null;
  24. public static bool LanguageFounded { get; private set; } = false;
  25. /// <summary>
  26. /// Applies the selected language to all form controls
  27. /// </summary>
  28. /// <param name="lang">Selected language</param>
  29. /// <param name="form">Target form</param>
  30. /// <returns>Returns true if localization has been SUCCESSFULLY applied to ALL form elements</returns>
  31. public static bool ApplyLanguage(string lang, Form form)
  32. {
  33. bool result = true;
  34. if (form.Tag != null)
  35. {
  36. string text = GetStringByTag(lang, (string)form.Tag);
  37. if (text == (string)form.Tag) result = false;
  38. form.Text = text;
  39. }
  40. foreach(Control ctrl in form.Controls)
  41. {
  42. bool tmp = ApplyLanguage(lang, ctrl);
  43. result = result && tmp;
  44. }
  45. return result;
  46. }
  47. /// <summary>
  48. /// Applies the selected language to this control
  49. /// </summary>
  50. /// <param name="lang">Selected language</param>
  51. /// <param name="control">Target control</param>
  52. /// <returns>Returns true if localization has been SUCCESSFULLY applied</returns>
  53. public static bool ApplyLanguage(string lang, Control control)
  54. {
  55. bool result = true;
  56. if (control.Tag != null)
  57. {
  58. if ((string)control.Tag == "$Empty") control.Text = String.Empty;
  59. else
  60. {
  61. string text = GetStringByTag(lang, (string)control.Tag);
  62. if (text == (string)control.Tag) result = false;
  63. control.Text = text;
  64. }
  65. }
  66. if (control.HasChildren)
  67. {
  68. foreach (Control ctrl in control.Controls)
  69. {
  70. bool tmp = ApplyLanguage(lang, ctrl);
  71. result = result && tmp;
  72. }
  73. }
  74. if (control is MenuStrip)
  75. {
  76. foreach (ToolStripMenuItem ctrl in ((MenuStrip)control).Items)
  77. {
  78. bool tmp = ApplyLanguage(lang, ctrl);
  79. result = result && tmp;
  80. }
  81. }
  82. if(control is ListView)
  83. {
  84. foreach (ColumnHeader ctrl in ((ListView)control).Columns)
  85. {
  86. bool tmp = ApplyLanguage(lang, ctrl);
  87. result = result && tmp;
  88. }
  89. }
  90. return result;
  91. }
  92. public static bool ApplyLanguage(string lang, ColumnHeader control)
  93. {
  94. bool result = true;
  95. if (control.Tag != null)
  96. {
  97. if (control.Tag.ToString().StartsWith("$")) {
  98. string text = GetStringByTag(lang, (string)control.Tag);
  99. if (text == (string)control.Tag) result = false;
  100. control.Text = text;
  101. }
  102. else
  103. {
  104. control.Text = "";
  105. }
  106. }
  107. return result;
  108. }
  109. public static bool ApplyLanguage(string lang, ToolStripMenuItem control)
  110. {
  111. bool result = true;
  112. if (control.Tag != null)
  113. {
  114. if ((string)control.Tag == "$Empty") control.Text = String.Empty;
  115. else
  116. {
  117. string text = GetStringByTag(lang, (string)control.Tag);
  118. if (text == (string)control.Tag) result = false;
  119. control.Text = text;
  120. }
  121. }
  122. if (control.HasDropDownItems)
  123. {
  124. for(int i = 0; i < control.DropDownItems.Count; i++)
  125. {
  126. if(!(control.DropDownItems[i] is ToolStripSeparator))
  127. {
  128. bool tmp = ApplyLanguage(lang, (ToolStripMenuItem)control.DropDownItems[i]);
  129. result = result && tmp;
  130. }
  131. }
  132. }
  133. return result;
  134. }
  135. /// <summary>
  136. /// Returns a string containing the localized decryption of the tag. If there is none, it returns the tag.
  137. /// </summary>
  138. /// <param name="lang">Language</param>
  139. /// <param name="tag">Tag</param>
  140. /// <returns></returns>
  141. public static string GetStringByTag(string lang,string tag)
  142. {
  143. if (LanguageFounded)
  144. {
  145. if (_current_lang == null || _current_lang != lang || _lines.Count == 0)
  146. {
  147. StreamReader sr = new StreamReader(Path.Combine(Directory.GetCurrentDirectory(), LanguageDir, lang + ".lang"));
  148. List<string> lines = sr.ReadToEnd().Split('\n').ToList();
  149. sr.Close();
  150. _lines.Clear();
  151. for (int i = 0; i < lines.Count; i++)
  152. {
  153. if (!String.IsNullOrEmpty(lines[i]))
  154. {
  155. string key, value;
  156. lines[i] = lines[i].Trim();
  157. key = lines[i].Split('=')[0];
  158. value = lines[i].Split('=')[1];
  159. value = value.Replace("\\n", "\n").Replace("\\t", "\t");
  160. _lines.Add(key, value);
  161. }
  162. }
  163. }
  164. }
  165. if (_lines.ContainsKey(tag)) return _lines[tag];
  166. else return tag;
  167. }
  168. /// <summary>
  169. /// Scans the folder for language files
  170. /// </summary>
  171. /// <param name="recursivly">DO NOT SET TO TRUE!!!</param>
  172. public static void ScanForLanguages(bool recursivly = false)
  173. {
  174. Directory.CreateDirectory(LanguageDir);
  175. List<string> files = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), LanguageDir)).ToList();
  176. for (int i = 0; i < files.Count; i++)
  177. {
  178. if (files[i].EndsWith(".lang"))
  179. {
  180. files[i] = files[i].Substring(files[i].LastIndexOf('\\') + 1);
  181. files[i] = files[i].Substring(0, files[i].LastIndexOf('.'));
  182. }
  183. }
  184. _langs = files;
  185. LanguageFounded = files.Count > 0;
  186. if (!LanguageFounded && !recursivly)
  187. {
  188. Stream default_language = Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name + "." + "en.lang");
  189. if (default_language == null) return;
  190. FileStream file = new FileStream(Path.Combine(LanguageDir, "en.lang"), FileMode.CreateNew, FileAccess.Write);
  191. default_language.CopyTo(file);
  192. file.Dispose();
  193. default_language.Dispose();
  194. ScanForLanguages(true);
  195. }
  196. }
  197. public static string Get(string tag)
  198. {
  199. return GetStringByTag(_current_lang, tag);
  200. }
  201. }
  202. }