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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Windows/Forms/I18N/ControlTranslator.cs	Sat Dec 27 18:33:01 2008 +0000
     1.3 @@ -0,0 +1,87 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Text;
     1.7 +using System.Windows.Forms;
     1.8 +using System.ComponentModel;
     1.9 +using IBBoard.Lang;
    1.10 +
    1.11 +namespace IBBoard.Windows.Forms.I18N
    1.12 +{
    1.13 +    public class ControlTranslator
    1.14 +    {
    1.15 +        public static void TranslateControl(Control ctrl, params object[] replacements)
    1.16 +        {
    1.17 +            TranslateControl(ctrl, true, replacements);
    1.18 +        }
    1.19 +
    1.20 +        public static void TranslateControl(Control ctrl, bool cascadeTranslate, params object[] replacements)
    1.21 +        {
    1.22 +            if (ctrl is ITranslatable)
    1.23 +            {
    1.24 +                Translation.Translate((ITranslatable)ctrl, replacements);
    1.25 +            }
    1.26 +
    1.27 +            if (cascadeTranslate)
    1.28 +            {
    1.29 +                CascadeControlTranslation(ctrl, replacements);
    1.30 +            }
    1.31 +        }
    1.32 +
    1.33 +        private static void CascadeControlTranslation(Control ctrl, params object[] replacements)
    1.34 +        {
    1.35 +            if (ctrl is ToolBar)
    1.36 +            {
    1.37 +                foreach (ToolBarButton bttn in ((ToolBar)ctrl).Buttons)
    1.38 +                {
    1.39 +                    TranslateComponent(bttn, true, replacements);
    1.40 +                }
    1.41 +            }
    1.42 +            else
    1.43 +            {
    1.44 +                foreach (Control subctr in ctrl.Controls)
    1.45 +                {
    1.46 +                    TranslateControl(subctr, true, replacements);
    1.47 +                }
    1.48 +            }
    1.49 +        }
    1.50 +
    1.51 +        public static void TranslateComponent(Component comp, params object[] replacements)
    1.52 +        {
    1.53 +            TranslateComponent(comp, true, replacements);
    1.54 +        }
    1.55 +
    1.56 +        public static void TranslateComponent(Component comp, bool cascadeTranslate, params object[] replacements)
    1.57 +        {
    1.58 +            if (comp is ITranslatable)
    1.59 +            {
    1.60 +                Translation.Translate((ITranslatable)comp, replacements);
    1.61 +            }
    1.62 +            else if (comp is FileDialog)
    1.63 +            {
    1.64 +                //HACK: We can't override SWF dialogs in .Net 1.1, so put in a special condition check for them
    1.65 +                FileDialog dialog = (FileDialog)comp;
    1.66 +
    1.67 +                if (dialog.Title.StartsWith("Translatable:"))
    1.68 +                {
    1.69 +                    dialog.Title = Translation.GetTranslation(dialog.Title.Substring(13), replacements);
    1.70 +                }
    1.71 +            }
    1.72 +
    1.73 +            if (cascadeTranslate)
    1.74 +            {
    1.75 +                CascadeComponentTranslations(comp, cascadeTranslate, replacements);
    1.76 +            }
    1.77 +        }
    1.78 +
    1.79 +        private static void CascadeComponentTranslations(Component comp, params object[] replacements)
    1.80 +        {
    1.81 +            if (comp is Menu)
    1.82 +            {
    1.83 +                foreach (MenuItem mi in ((Menu)comp).MenuItems)
    1.84 +                {
    1.85 +                    TranslateComponent(mi, true, replacements);
    1.86 +                }
    1.87 +            }
    1.88 +        }
    1.89 +    }
    1.90 +}