| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace FactorioModManager
- {
- public class LanguageManager
- {
- /// <summary>
- /// Path to the folder with language files relative to the CURRENT program folder
- /// </summary>
- public static string LanguageDir { get; set; } = "lang";
- /// <summary>
- /// List of available languages
- /// </summary>
- public static List<string> AvailableLangs { get { return _langs; } }
- private static List<string> _langs = new List<string>();
- private static Dictionary<string, string> _lines = new Dictionary<string, string>();
- private static string _current_lang = null;
- public static bool LanguageFounded { get; private set; } = false;
- /// <summary>
- /// Applies the selected language to all form controls
- /// </summary>
- /// <param name="lang">Selected language</param>
- /// <param name="form">Target form</param>
- /// <returns>Returns true if localization has been SUCCESSFULLY applied to ALL form elements</returns>
- public static bool ApplyLanguage(string lang, Form form)
- {
- bool result = true;
- if (form.Tag != null)
- {
- string text = GetStringByTag(lang, (string)form.Tag);
- if (text == (string)form.Tag) result = false;
- form.Text = text;
- }
- foreach(Control ctrl in form.Controls)
- {
- bool tmp = ApplyLanguage(lang, ctrl);
- result = result && tmp;
- }
- return result;
- }
- /// <summary>
- /// Applies the selected language to this control
- /// </summary>
- /// <param name="lang">Selected language</param>
- /// <param name="control">Target control</param>
- /// <returns>Returns true if localization has been SUCCESSFULLY applied</returns>
- public static bool ApplyLanguage(string lang, Control control)
- {
-
- bool result = true;
- if (control.Tag != null)
- {
- if ((string)control.Tag == "$Empty") control.Text = String.Empty;
- else
- {
- string text = GetStringByTag(lang, (string)control.Tag);
- if (text == (string)control.Tag) result = false;
- control.Text = text;
- }
- }
- if (control.HasChildren)
- {
- foreach (Control ctrl in control.Controls)
- {
- bool tmp = ApplyLanguage(lang, ctrl);
- result = result && tmp;
- }
- }
- if (control is MenuStrip)
- {
- foreach (ToolStripMenuItem ctrl in ((MenuStrip)control).Items)
- {
- bool tmp = ApplyLanguage(lang, ctrl);
- result = result && tmp;
- }
- }
- if(control is ListView)
- {
- foreach (ColumnHeader ctrl in ((ListView)control).Columns)
- {
- bool tmp = ApplyLanguage(lang, ctrl);
- result = result && tmp;
- }
- }
- return result;
- }
- public static bool ApplyLanguage(string lang, ColumnHeader control)
- {
- bool result = true;
- if (control.Tag != null)
- {
- if (control.Tag.ToString().StartsWith("$")) {
- string text = GetStringByTag(lang, (string)control.Tag);
- if (text == (string)control.Tag) result = false;
- control.Text = text;
- }
- else
- {
- control.Text = "";
- }
- }
- return result;
- }
- public static bool ApplyLanguage(string lang, ToolStripMenuItem control)
- {
- bool result = true;
- if (control.Tag != null)
- {
- if ((string)control.Tag == "$Empty") control.Text = String.Empty;
- else
- {
- string text = GetStringByTag(lang, (string)control.Tag);
- if (text == (string)control.Tag) result = false;
- control.Text = text;
- }
- }
- if (control.HasDropDownItems)
- {
- for(int i = 0; i < control.DropDownItems.Count; i++)
- {
- if(!(control.DropDownItems[i] is ToolStripSeparator))
- {
- bool tmp = ApplyLanguage(lang, (ToolStripMenuItem)control.DropDownItems[i]);
- result = result && tmp;
- }
- }
- }
- return result;
- }
- /// <summary>
- /// Returns a string containing the localized decryption of the tag. If there is none, it returns the tag.
- /// </summary>
- /// <param name="lang">Language</param>
- /// <param name="tag">Tag</param>
- /// <returns></returns>
- public static string GetStringByTag(string lang,string tag)
- {
- if (LanguageFounded)
- {
- if (_current_lang == null || _current_lang != lang || _lines.Count == 0)
- {
- StreamReader sr = new StreamReader(Path.Combine(Directory.GetCurrentDirectory(), LanguageDir, lang + ".lang"));
- List<string> lines = sr.ReadToEnd().Split('\n').ToList();
- sr.Close();
- _lines.Clear();
- for (int i = 0; i < lines.Count; i++)
- {
- if (!String.IsNullOrEmpty(lines[i]))
- {
- string key, value;
- lines[i] = lines[i].Trim();
- key = lines[i].Split('=')[0];
- value = lines[i].Split('=')[1];
- value = value.Replace("\\n", "\n").Replace("\\t", "\t");
- _lines.Add(key, value);
- }
- }
- }
- }
- if (_lines.ContainsKey(tag)) return _lines[tag];
- else return tag;
-
-
- }
- /// <summary>
- /// Scans the folder for language files
- /// </summary>
- /// <param name="recursivly">DO NOT SET TO TRUE!!!</param>
- public static void ScanForLanguages(bool recursivly = false)
- {
- Directory.CreateDirectory(LanguageDir);
- List<string> files = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), LanguageDir)).ToList();
- for (int i = 0; i < files.Count; i++)
- {
- if (files[i].EndsWith(".lang"))
- {
- files[i] = files[i].Substring(files[i].LastIndexOf('\\') + 1);
- files[i] = files[i].Substring(0, files[i].LastIndexOf('.'));
- }
- }
- _langs = files;
- LanguageFounded = files.Count > 0;
- if (!LanguageFounded && !recursivly)
- {
- Stream default_language = Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name + "." + "en.lang");
- if (default_language == null) return;
- FileStream file = new FileStream(Path.Combine(LanguageDir, "en.lang"), FileMode.CreateNew, FileAccess.Write);
- default_language.CopyTo(file);
- file.Dispose();
- default_language.Dispose();
- ScanForLanguages(true);
- }
- }
- public static string Get(string tag)
- {
- return GetStringByTag(_current_lang, tag);
- }
- }
- }
|