view Windows/Forms/I18N/ControlTranslator.cs @ 15:7c459ebc4210

* Code clean-up and documentation no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 18 Apr 2010 15:04:21 +0000
parents 774fd3daefe1
children 5f35edc84791
line wrap: on
line source

// This file (ColorableStatusBar.cs) is a part of the IBBoard.Windows.Forms library and is copyright 2009 IBBoard.
//
// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using IBBoard.Lang;

namespace IBBoard.Windows.Forms.I18N
{
	/// <summary>
	/// A custom cascading translator. It take any widget type (controls or components) and translates them
	/// if they implement <see>ITranslatable</see> and optionally cascades the process to translate all children.
	/// 
	/// Note: Translations <strong>will not</strong> cascade into MdiClient controls
	/// </summary>
	public class ControlTranslator
	{
		/// <summary>
		/// Recursively translates a collection of controls, such as those from a form's <code>Controls</code> collection
		/// </summary>
		/// <param name="controls"></param>
		public static void TranslateControls(Control.ControlCollection controls)
		{
			foreach (Control ctrl in controls)
			{
				if (!(ctrl is MdiClient))
				{
					//Client windows must translate themselves
					ControlTranslator.TranslateControl(ctrl);
				}
			}
		}

		/// <summary>
		/// Recursively translates a collection of components, such as those from a form's <code>components.Components</code> collection
		/// </summary>
		/// <param name="components">The components to recursively translate</param>
		public static void TranslateComponents(ComponentCollection components)
		{
			foreach (Component comp in components)
			{
				ControlTranslator.TranslateComponent(comp);
			}
		}

		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)
			{
				Translate((ITranslatable)ctrl, replacements);
			}

			if (cascadeTranslate)
			{
				CascadeControlTranslation(ctrl, replacements);
			}
		}

		private static void Translate(ITranslatable translatable, params object[] replacements)
		{
			Translation.Translate(translatable, translatable.Text, 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 if (ctrl is ToolStrip)
			{
				foreach (ToolStripItem item in ((ToolStrip)ctrl).Items)
				{
					TranslateComponent(item, true, replacements);
				}
			}
			else
			{
				TranslateControls(ctrl.Controls);
			}
		}

		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)
			{
				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:"))
				{
					string title = dialog.Title.Substring(13);
					dialog.Title = Translation.GetTranslation(title, title, 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);
				}
			}
			else if (comp is ToolStripDropDownItem)
			{
				foreach (ToolStripItem item in ((ToolStripDropDownItem)comp).DropDownItems)
				{
					TranslateComponent(item, true, replacements);
				}
			}
			else if (comp is Form)
			{
				TranslateControls(((Form)comp).Controls);
			}
		}
	}
}