# HG changeset patch # User IBBoard # Date 1230402781 0 # Node ID b829a332068888c48807f389747f2ba22a9e9793 # Parent 258ef411e698dc5137a7d80ed05d4cf0e5cf852e Re #3 - Copy control related translation to new class in I18N diff -r 258ef411e698 -r b829a3320688 Windows/Forms/I18N/ControlTranslator.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Windows/Forms/I18N/ControlTranslator.cs Sat Dec 27 18:33:01 2008 +0000 @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Windows.Forms; +using System.ComponentModel; +using IBBoard.Lang; + +namespace IBBoard.Windows.Forms.I18N +{ + public class ControlTranslator + { + public static void TranslateControl(Control ctrl, params object[] replacements) + { + TranslateControl(ctrl, true, replacements); + } + + public static void TranslateControl(Control ctrl, bool cascadeTranslate, params object[] replacements) + { + if (ctrl is ITranslatable) + { + Translation.Translate((ITranslatable)ctrl, replacements); + } + + if (cascadeTranslate) + { + CascadeControlTranslation(ctrl, replacements); + } + } + + private static void CascadeControlTranslation(Control ctrl, params object[] replacements) + { + if (ctrl is ToolBar) + { + foreach (ToolBarButton bttn in ((ToolBar)ctrl).Buttons) + { + TranslateComponent(bttn, true, replacements); + } + } + else + { + foreach (Control subctr in ctrl.Controls) + { + TranslateControl(subctr, true, replacements); + } + } + } + + public static void TranslateComponent(Component comp, params object[] replacements) + { + TranslateComponent(comp, true, replacements); + } + + public static void TranslateComponent(Component comp, bool cascadeTranslate, params object[] replacements) + { + if (comp is ITranslatable) + { + Translation.Translate((ITranslatable)comp, replacements); + } + else if (comp is FileDialog) + { + //HACK: We can't override SWF dialogs in .Net 1.1, so put in a special condition check for them + FileDialog dialog = (FileDialog)comp; + + if (dialog.Title.StartsWith("Translatable:")) + { + dialog.Title = Translation.GetTranslation(dialog.Title.Substring(13), replacements); + } + } + + if (cascadeTranslate) + { + CascadeComponentTranslations(comp, cascadeTranslate, replacements); + } + } + + private static void CascadeComponentTranslations(Component comp, params object[] replacements) + { + if (comp is Menu) + { + foreach (MenuItem mi in ((Menu)comp).MenuItems) + { + TranslateComponent(mi, true, replacements); + } + } + } + } +}