annotate Translatable/ControlTranslator.cs @ 39:35b88b848524

* Add simple dialog to add basic widgets to for use in WarFoundry
author IBBoard <dev@ibboard.co.uk>
date Tue, 18 Oct 2011 20:58:49 +0100
parents 70c46caee508
children f04e973e5ea0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (ControlTranslator.cs) is a part of the IBBoard.GtkSharp project and is copyright 2010 IBBoard
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 // // 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.
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using Gtk;
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using IBBoard.Lang;
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 namespace IBBoard.GtkSharp.Translatable
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 {
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 /// <summary>
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 /// A custom cascading translator. It takes any widget type and translates them if they implement <see>ITranslatable</see>
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 /// and optionally cascades the process to translate all children.
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 /// </summary>
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 public class ControlTranslator
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 {
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 public static void TranslateWidget(Widget toTranslate)
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 {
17
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
18 TranslateWidget(toTranslate, true);
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
19 }
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
20
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
21 public static void TranslateWidget(Widget toTranslate, bool cascade)
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
22 {
16
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 if (toTranslate is ITranslatable)
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 {
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 Translation.Translate((ITranslatable)toTranslate);
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 }
29
bcb437c04685 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
27 else if (toTranslate is MenuItem || toTranslate is ToolButton)
21
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
28 {
29
bcb437c04685 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
29 TranslateWidgetWithAction((Widget)toTranslate);
23
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
30 }
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
31
17
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
32 if (cascade && toTranslate is Container)
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
33 {
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
34 CascadeTranslations((Container)toTranslate, cascade);
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
35 }
16
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 }
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37
29
bcb437c04685 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
38 private static void TranslateWidgetWithAction(Widget widget)
23
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
39 {
29
bcb437c04685 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
40 Action action = widget.Action;
bcb437c04685 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
41 string translation = Translation.GetTranslation(widget.Name, "");
23
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
42
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
43 if (translation == "")
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
44 {
29
bcb437c04685 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
45 widget.TooltipText = translation;
23
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
46 }
29
bcb437c04685 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
47 else if (action != null)
23
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
48 {
29
bcb437c04685 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
49 widget.TooltipText = action.Label;
23
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
50 }
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
51 }
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
52
17
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
53 private static void CascadeTranslations(Container container, bool cascade)
16
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 {
23
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
55 if (container is ITranslatableWithActions)
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
56 {
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
57 TranslateActions((ITranslatableWithActions)container);
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
58 }
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
59
17
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
60 foreach (Widget childWidget in container.AllChildren)
16
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 {
17
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
62 TranslateWidget(childWidget, cascade);
16
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 }
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 }
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65
20
06715f517af6 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
66 private static void TranslateActions(ITranslatableWithActions actionContainer)
06715f517af6 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
67 {
06715f517af6 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
68 foreach (Action action in actionContainer.Actions)
06715f517af6 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
69 {
21
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
70 TranslateAction(action);
20
06715f517af6 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
71 }
06715f517af6 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
72 }
21
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
73
33
70c46caee508 * Resolve conflicts between System.Action and Gtk.Action
IBBoard <dev@ibboard.co.uk>
parents: 29
diff changeset
74 private static void TranslateAction(Gtk.Action action)
21
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
75 {
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
76 action.Label = Translation.GetTranslation(action.Name, action.Label);
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
77 }
26
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
78
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
79 public static void TranslateButtons(params Button[] buttons)
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
80 {
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
81 foreach (Button button in buttons)
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
82 {
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
83 TranslateButton(button);
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
84 }
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
85 }
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
86
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
87 public static void TranslateButton(Button button)
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
88 {
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
89 Widget image = button.Image;
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
90 button.Label = Translation.GetTranslation(button.Name, button.Label);
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
91 button.Image = image;
63919afde887 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
92 }
16
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 }
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 }
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95