changeset 2:b829a3320688

Re #3 - Copy control related translation to new class in I18N
author IBBoard <dev@ibboard.co.uk>
date Sat, 27 Dec 2008 18:33:01 +0000
parents 258ef411e698
children 6e0d34fefe0a
files Windows/Forms/I18N/ControlTranslator.cs
diffstat 1 files changed, 87 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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);
+                }
+            }
+        }
+    }
+}