/// <summary> /// Lazy Loads the text for each Control in form /// </summary> /// <param name="ControlDefaultValues">A dictionary with ControlName-Text pairs</param> public static Thread LazyLoadTranslations(FinOrgForm f) { try { Thread t = new Thread(() => { // Get a list of items to fetch IEnumerable <string> items = f.ControlDefaultValues.Values; if (Translations != null) { items = items.Except(Translations.Keys).ToArray(); } SqlConnection con = FinOrgForm.getSqlConnection(); try { con.Open(); SqlCommand cmd = new SqlCommand(string.Format("SELECT text, {0} FROM TRANSLATIONS WHERE text IN ({{keys}});", currentLanguage), con); cmd.AddArrayParameters(items, "keys"); if (cmd.Parameters.Count > 0) { using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { // Only Keys NOT Present in Translations Dictionary is fetched from Database if (!Translations.ContainsKey(reader["text"].ToString())) { Translations.Add(reader["text"].ToString(), reader[currentLanguage].ToString()); } } } } con.Close(); } catch (Exception e) { con.Close(); MessageBox.Show(e.Message, "FinOrg Languages LazyLoadTranslations"); } if (LANG_DEBUG_MODE) // Copies the value from form to database currentLanguage field { InsertFormTranslations(f.ControlDefaultValues); } // Apply Translation f.BeginInvoke(new Action(() => { ApplyTranslation(f); })); }); // finish this fast t.Priority = ThreadPriority.Highest; t.Start(); return(t); } catch (Exception ex) { return(null); } }
// public static Thread Init() { Thread t = new Thread(() => { SqlConnection con = FinOrgForm.getSqlConnection(); try { con.Open(); SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'TRANSLATIONS';", con); if ((int)cmd.ExecuteScalar() == 0) { // create translations table cmd.CommandText = TRANSLATIONS_TABLE_SQL; cmd.ExecuteNonQuery(); } if (LANG_DEBUG_MODE) { LoadAllTranslations(); } else { Translations = new Dictionary <string, string>(); } con.Close(); } catch (Exception e) { con.Close(); MessageBox.Show(e.Message, "FinOrg Languages Init"); } }); t.Start(); return(t); }
/// <summary> /// Initialize Languaging Process for a form /// </summary> /// <param name="f"></param> public static void InitFormLanguage(FinOrgForm f) { try { f.ControlDefaultValues = new Dictionary <string, string>(); foreach (Control c in f.GetAllControlChildren()) { if (c.IsTranslatableControl() && !string.IsNullOrEmpty(c.Name)) { f.ControlDefaultValues.Add(c.Name, c.Text.Simplified(true)); // c.AutoSize = false; } // if Control is a type of ToolStrip, iterate for ToolStripItem if (c.GetType().IsSubclassOf(typeof(ToolStrip)) || c.GetType() == typeof(ToolStrip)) { foreach (ToolStripItem toolStripItem in ((ToolStrip)c).GetAllToolStripItems()) { if (!string.IsNullOrEmpty(toolStripItem.Name)) { f.ControlDefaultValues.Add(toolStripItem.Name, toolStripItem.Text.Simplified(true)); } } } // if Control is a type of DataGridView, iterate for DataGridViewColumn if (c.GetType() == typeof(DataGridView)) { foreach (DataGridViewColumn col in ((DataGridView)c).Columns) { if (!string.IsNullOrEmpty(col.Name)) { f.ControlDefaultValues.Add(col.Name, col.HeaderText.Simplified(true)); } } } // if Control is TabControl if (c.GetType() == typeof(TabControl)) { foreach (TabPage p in ((TabControl)c).TabPages) { if (!string.IsNullOrEmpty(p.Name)) { f.ControlDefaultValues.Add(p.Name, p.Text.Simplified(true)); } } } } // ControlDefaultValues loaded // LazyLoad these in Languages Languages.LazyLoadTranslations(f); } catch (Exception ex) { } }
public static void CheckLicense() { https: //stackoverflow.com/questions/17292366/hashing-with-sha1-algorithm-in-c-sharp using (SqlConnection con = FinOrgForm.getSqlConnection()) { con.Open(); SqlDataAdapter adapter = new SqlDataAdapter(@" SELECT * FROM LicenseAuth; EXEC xp_instance_regread 'HKEY_LOCAL_MACHINE', 'HARDWARE\DESCRIPTION\System\CentralProcessor\0', 'ProcessorNameString'; EXEC xp_instance_regread 'HKEY_LOCAL_MACHINE', 'HARDWARE\DESCRIPTION\System\BIOS', 'BaseBoardManufacturer'; EXEC xp_instance_regread 'HKEY_LOCAL_MACHINE', 'HARDWARE\DESCRIPTION\System\BIOS', 'BaseBoardProduct'; EXEC xp_instance_regread 'HKEY_LOCAL_MACHINE', 'HARDWARE\DESCRIPTION\System\BIOS', 'BaseBoardVersion'; EXEC xp_instance_regread 'HKEY_LOCAL_MACHINE', 'HARDWARE\DESCRIPTION\System\BIOS', 'SystemProductName'; " , con); DataSet _data = new DataSet(); adapter.Fill(_data); string str = _data.Tables[0].Rows[0]["name"].ToString(); for (int i = 1; i < _data.Tables.Count; i++) { foreach (DataRow r in _data.Tables[i].Rows) { str += r["Value"].ToString() + r["Data"].ToString(); } } string hashed = Hash(str); if (_data.Tables[0].Rows[0]["license_key"].ToString() != hashed) { MessageBox.Show("Not licensed"); } else { MessageBox.Show("Licensed"); } } }
private static void LoadAllTranslations() { SqlConnection con = FinOrgForm.getSqlConnection(); con.Open(); SqlCommand cmd = new SqlCommand(string.Format("SELECT text, {0} FROM TRANSLATIONS", currentLanguage), con); using (SqlDataReader reader = cmd.ExecuteReader()) { Translations = new Dictionary <string, string>(); while (reader.Read()) { Translations.Add(reader["text"].ToString(), reader[currentLanguage].ToString()); } } con.Close(); }
public static string GetStringTranslation(string s) { // lower string key = s.Simplified(true); if (!Translations.ContainsKey(key)) { SqlConnection con = FinOrgForm.getSqlConnection(); SqlCommand cmd = new SqlCommand("", con); try { con.Open(); // Insert to DB if (LANG_DEBUG_MODE) { cmd.CommandText = "INSERT INTO TRANSLATIONS (text, english) VALUES (@key, @v);"; cmd.Parameters.Add(new SqlParameter("key", key)); cmd.Parameters.Add(new SqlParameter("v", s)); cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); } // fetch from DB cmd.CommandText = string.Format("SELECT {0} FROM TRANSLATIONS WHERE text = @v", currentLanguage); cmd.Parameters.Add(new SqlParameter("v", key)); object data = cmd.ExecuteScalar(); con.Close(); if (data != null) { Translations.Add(key, data.ToString()); return(data.ToString()); } else { return(s); } } catch (Exception ef) { MessageBox.Show(ef.Message + "\nSQL: " + cmd.CommandText, "FinOrg Languages GetTranslation"); return(""); } } else { return(Translations[key]); } }
/// <summary> /// Copies values /// Call from a thread, not from UI /// </summary> /// <param name="ControlDefaultValues"></param> public static void InsertFormTranslations(Dictionary <string, string> ControlDefaultValues) { if (ControlDefaultValues.Count == 0) { return; } SqlConnection con = FinOrgForm.getSqlConnection(); SqlCommand cmd = new SqlCommand(string.Format("INSERT INTO TRANSLATIONS (text, {0}) VALUES ", currentLanguage), con); try { con.Open(); int i = 0; // dictionary doesnt have proper index to loop with forloop foreach (KeyValuePair <string, string> e in ControlDefaultValues) { // check for duplications if (Translations.ContainsKey(e.Value.Simplified())) { continue; } if (i > 0) { cmd.CommandText += ", "; } cmd.CommandText += string.Format("(@text{0}, @value{0})", i); cmd.Parameters.Add(new SqlParameter("@text" + i, e.Value.Simplified())); cmd.Parameters.Add(new SqlParameter("@value" + i, e.Value.Simplified())); Translations.Add(e.Value.Simplified(), e.Value.Simplified()); i++; } if (cmd.Parameters.Count > 0) { cmd.ExecuteNonQuery(); } con.Close(); } catch (Exception e) { con.Close(); MessageBox.Show(e.Message + "\n" + cmd.CommandText, "FinOrg: Languages InsertFormTranslations"); } }
public static void ApplyTranslation(FinOrgForm f) { f.RightToLeftLayout = currentLanguage == "arabic"; f.RightToLeft = currentLanguage == "arabic" ? RightToLeft.Yes : RightToLeft.No; foreach (Control c in f.GetAllControlChildren()) { try { if (c.IsTranslatableControl()) { c.Text = Translations[f.ControlDefaultValues[c.Name]]; } // Apply on ToolStrips if (c.GetType().IsSubclassOf(typeof(ToolStrip)) || c.GetType() == typeof(ToolStrip)) { foreach (ToolStripItem toolStripItem in ((ToolStrip)c).GetAllToolStripItems()) { if (!string.IsNullOrEmpty(toolStripItem.Name)) { toolStripItem.Text = Translations[f.ControlDefaultValues[toolStripItem.Name]]; } } } // if Control is a type of DataGridView, iterate for DataGridViewColumn if (c.GetType() == typeof(DataGridView)) { foreach (DataGridViewColumn col in ((DataGridView)c).Columns) { if (!string.IsNullOrEmpty(col.Name)) { if (!f.ControlDefaultValues.ContainsKey(col.Name)) { continue; } col.HeaderText = Translations[f.ControlDefaultValues[col.Name]]; if (col.GetType() == typeof(DataGridViewButtonColumn)) { ((DataGridViewButtonColumn)col).Text = col.HeaderText; } } } } // if Control is TabControl if (c.GetType() == typeof(TabControl)) { foreach (TabPage p in ((TabControl)c).TabPages) { if (!string.IsNullOrEmpty(p.Name)) { p.Text = Translations[f.ControlDefaultValues[p.Name]]; } } } // Apply RTL on the following Controls if (c.GetType() == typeof(Panel) || c.GetType() == typeof(GroupBox)) { if (c.Tag == null || string.IsNullOrWhiteSpace(c.Tag.ToString())) { c.Tag = "english"; } if (!c.Tag.ToString().Equals(currentLanguage)) { // panel type, rearrage children foreach (Control panel_child in c.Controls) { panel_child.Location = new System.Drawing.Point(c.Size.Width - panel_child.Size.Width - panel_child.Location.X, panel_child.Location.Y); } } c.Tag = currentLanguage; } } catch (Exception e) { MessageBox.Show(string.Format("{0}({4}) : {1} ({2})\nKey: {5}\nMessage: {3}", f.Name, c.Name, c.GetType(), e.Message, f.GetType(), "f.ControlDefaultValues[c.Name]"), "FinOrg Langauges ApplyTranslation"); } } }