view Translatable/ControlTranslator.cs @ 22:edae1d817962

Re #47: Add translatable GTK# widgets * Remove debugging print statement
author IBBoard <dev@ibboard.co.uk>
date Tue, 28 Dec 2010 19:50:04 +0000
parents 0364560fcec2
children ae37467335ae
line wrap: on
line source

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

using System;
using Gtk;
using IBBoard.Lang;

namespace IBBoard.GtkSharp.Translatable
{
	/// <summary>
	/// A custom cascading translator. It takes any widget type and translates them if they implement <see>ITranslatable</see>
	/// and optionally cascades the process to translate all children.
	/// </summary>
	public class ControlTranslator
	{
		public static void TranslateWidget(Widget toTranslate)
		{
			TranslateWidget(toTranslate, true);
		}

		public static void TranslateWidget(Widget toTranslate, bool cascade)
		{
			if (toTranslate is ITranslatable)
			{
				Translation.Translate((ITranslatable)toTranslate);
			}
			else if (toTranslate is MenuItem)
			{
				TranslateAction(((MenuItem)toTranslate).Action);
			}
			
			if (cascade && toTranslate is Container)
			{
				CascadeTranslations((Container)toTranslate, cascade);
			}
		}

		private static void CascadeTranslations(Container container, bool cascade)
		{
			foreach (Widget childWidget in container.AllChildren)
			{
				TranslateWidget(childWidget, cascade);
			}

			if (container is ITranslatableWithActions)
			{
				TranslateActions((ITranslatableWithActions)container);
			}
		}

		private static void TranslateActions(ITranslatableWithActions actionContainer)
		{
			foreach (Action action in actionContainer.Actions)
			{
				TranslateAction(action);
			}
		}

		private static void TranslateAction(Action action)
		{
			action.Label = Translation.GetTranslation(action.Name, action.Label);
		}
	}
}