/// <summary>
 /// Loads the passed language file and translates the control and all its childes to the passed language.
 /// The displayed Text and the ToolTip of the controls will be translated, if
 /// the value is found.
 /// </summary>
 /// <param name="localizer">A LanguageDictionaryManager with the wanted language to translate.</param>
 /// <param name="control">The Control to translate (Forms or UserControls)</param>
 /// <param name="language">The language to translate the controls to.</param>
 public static void TranslateControls(Localizer localizer, Control control, string language = null)
 {
     TranslateControls(localizer, control, language, null);
 }
        /// <summary>
        /// Loads the passed language file and translates the control and all its childes to the passed language.
        /// The displayed Text and the ToolTip of the controls will be translated, if
        /// the value is found.
        /// </summary>
        /// <param name="localizer">A LanguageDictionaryManager with the wanted language to translate.</param>
        /// <param name="control">The Control to translate (Forms or UserControls)</param>
        /// <param name="language">The language to translate the controls to.</param>
        /// <param name="tt">For internal use! The ToolTip Control of the control to translate. If null the function tries to get is automatically.</param>
        private static void TranslateControls(Localizer localizer, Control control, string language, ToolTip tt)
        {
            if (string.IsNullOrEmpty(language))
                language = localizer.CurrentLanguage;

            if (string.IsNullOrEmpty(language) || !localizer.ContainsLaguage(language))
            {
                Messenger.AddError(string.Format(MSG_LANGUAGE_0_NOT_FOUND, language));
                return;
            }

            string name = control.Name;

            // Get ToolTip Control from Forms or UserControls.
            Form form = control as Form;
            if (form != null)
                tt = GetToolTipControl(form);
            else
            {
                UserControl userControl = control as UserControl;
                if (userControl != null)
                    tt = GetToolTipControl(userControl);
            }


            if (!string.IsNullOrEmpty(name) && localizer.ContainsKey(language, name))
            {
                string value = localizer[language, name];

                if (!string.IsNullOrEmpty(value))
                    control.Text = value;
            }
            
            MenuStrip ms = control as MenuStrip;
            ToolStrip ts = control as ToolStrip;
            if (ms != null)
            {
                foreach (ToolStripItem item in ms.Items)
                    TryReadToolStripItems(localizer, language, item);
            }

            else if (ts != null)
            {
                foreach (ToolStripItem item in ts.Items)
                    TryReadToolStripItems(localizer, language, item);
            }

            else if (TryReadFromComboBox(localizer, language, control))
            { /* do nothing */ }

            else if (TryReadFromDataGridView(localizer, language, control))
            { /* do nothing */ }

            else if (TryReadFromListView(localizer, language, control))
            { /* do nothing */ }

            else
            { /* do nothing */ }

            // Try read ToolTip
            if (tt != null)
            {
                string key = control.Name + TOOLTIP;
                if (localizer.ContainsKey(language, key))
                    tt.SetToolTip(control, localizer[language, key]);
            }

            if (control.ContextMenuStrip != null)
                TranslateControls(localizer, control.ContextMenuStrip, language, tt);

            foreach (Control childControl in control.Controls)
                TranslateControls(localizer, childControl, language, tt);
        }
        private static bool TryReadFromComboBox(Localizer localizer, string language, Control control)
        {
            ComboBox cb = control as ComboBox;
            if (cb != null)
            {
                for (int i = 0; i < cb.Items.Count; ++i)
                {
                    string key = cb.Name + CB_ITEM + (i + 1);
                    if (localizer.ContainsKey(language, key))
                        cb.Items[i] = localizer[language, key];
                }

                return true;
            }

            return false;
        }
 /// <summary>
 /// Loads the passed language file and translates the control and all its childes to the passed language.
 /// The displayed Text and the ToolTip of the controls will be translated, if
 /// the value is found.
 /// </summary>
 /// <param name="filename">Path and file name to the language file to load.</param>
 /// <param name="control">The Control to translate (Forms or UserControls)</param>
 /// <param name="language">The language to translate the controls to.</param>
 public static void TranslateControls(string filename, Control control, string language)
 {
     Localizer localizer = new Localizer(filename);
     localizer.LoadLanguageFromXml(filename);
     TranslateControls(localizer, control, language);
 }
        private static bool TryReadFromListView(Localizer localizer, string language, Control control)
        {
            ListView lv = control as ListView;
            if (lv != null)
            {
                for (int i = 0; i < lv.Columns.Count; ++i)
                {
                    string key = lv.Name + COLUMN + (i + 1);
                    if (localizer.ContainsKey(language, key))
                        lv.Columns[i].Text = localizer[language, key];
                }

                for (int i = 0; i < lv.Groups.Count; ++i)
                {
                    string key = lv.Name + GROUP + (i + 1);
                    if (localizer.ContainsKey(language, key))
                        lv.Groups[i].Header = localizer[language, key];
                }

                for (int i = 0; i < lv.Items.Count; ++i)
                {
                    string key = lv.Name + ITEM + (i + 1);
                    if (localizer.ContainsKey(language, key))
                        lv.Items[i].Text = localizer[language, key];
                }

                return true;
            }

            return false;
        }
        private static bool TryReadFromDataGridView(Localizer localizer, string language, Control control)
        {
            DataGridView dgv = control as DataGridView;
            if (dgv != null)
            {
                for (int i = 0; i < dgv.Columns.Count; ++i)
                {
                    var column = dgv.Columns[i];
                    string key = column.Name;
                    if (localizer.ContainsKey(language, key))
                        column.HeaderText = localizer[language, key];
                }

                return true;
            }

            return false;
        }
        private static bool TryReadToolStripMenuItem(Localizer localizer, string language, object item)
        {
            ToolStripMenuItem tsmi = item as ToolStripMenuItem;
            if (tsmi != null)
            {
                string key = tsmi.Name;
                if (localizer.ContainsKey(language, key))
                    tsmi.Text = localizer[language, key];

                if (tsmi.HasDropDownItems)
                {
                    foreach (ToolStripItem child in tsmi.DropDownItems)
                        TryReadToolStripItems(localizer, language, child);
                }
                ////else if (tsmi.HasDropDown)
                ////{
                ////    foreach (ToolStripItem child in tsmi.DropDown.Items)
                ////        TryReadToolStripItems(localizer, language, child);
                ////}

                return true;
            }
            return false;
        }
        private static bool TryReadToolStripItem(Localizer localizer, string language, object item)
        {
            ToolStripItem tsi = item as ToolStripItem;
            if (tsi != null)
            {
                string key = tsi.Name;
                if (localizer.ContainsKey(language, key))
                    tsi.Text = localizer[language, key];

                return true;
            }
            return false;
        }
 private static bool TryReadToolStripTextBox(Localizer localizer, string language, object item)
 {
     return TryReadToolStripItem(localizer, language, item);
 }
        private static bool TryReadToolStripComboBox(Localizer localizer, string language, object item)
        {
            ToolStripComboBox tscb = item as ToolStripComboBox;
            if (tscb != null)
            {
                for (int i = 0; i < tscb.Items.Count; ++i)
                {
                    string key = tscb.Name + CB_ITEM + (i + 1);
                    if (localizer.ContainsKey(language, key))
                        tscb.Items[i] = localizer[language, key];
                }

                return true;
            }
            return false;
        }
        private static void TryReadToolStripItems(Localizer localizer, string language, ToolStripItem item)
        {
            if (TryReadToolStripComboBox(localizer, language, item))
            { }

            else if (TryReadToolStripDropDownButton(localizer, language, item))
            { }

            else if (TryReadToolStripSplitButton(localizer, language, item))
            { }

            else if (TryReadToolStripMenuItem(localizer, language, item))
            { }

            else if (TryReadToolStripLabel(localizer, language, item))
            { }

            else if (TryReadToolStripTextBox(localizer, language, item))
            { }

            else if (TryReadToolStripItem(localizer, language, item as object))
            { }

            string key = item.Name + TOOLTIP;
            if (localizer.ContainsKey(language, key))
                item.ToolTipText = localizer[language, key];
        }