annotate Translatable/ControlTranslator.cs @ 23:ae37467335ae

Re #47: Add translatable GTK# widgets * Handle tool buttons, assuming that they're shown as icons and need a tooltip for text * Translate actions before contained widgets (where they exist) on the assumption that some widgets (e.g. menus and buttons) may want to use the action text
author IBBoard <dev@ibboard.co.uk>
date Tue, 28 Dec 2010 20:18:46 +0000
parents edae1d817962
children 63919afde887
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 System;
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using Gtk;
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using IBBoard.Lang;
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 namespace IBBoard.GtkSharp.Translatable
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 {
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 /// <summary>
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 /// 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
13 /// and optionally cascades the process to translate all children.
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 /// </summary>
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 public class ControlTranslator
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 {
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 public static void TranslateWidget(Widget toTranslate)
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 {
17
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
19 TranslateWidget(toTranslate, true);
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
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
22 public static void TranslateWidget(Widget toTranslate, bool cascade)
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
23 {
16
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 if (toTranslate is ITranslatable)
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 {
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 Translation.Translate((ITranslatable)toTranslate);
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 }
21
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
28 else if (toTranslate is MenuItem)
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
29 {
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
30 TranslateAction(((MenuItem)toTranslate).Action);
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
31 }
23
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
32 else if (toTranslate is ToolButton)
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
33 {
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
34 ToolButton toolButton = ((ToolButton)toTranslate);
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
35 TranslateToolButton(toolButton);
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
36 }
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
37
17
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
38 if (cascade && toTranslate is Container)
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
39 {
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
40 CascadeTranslations((Container)toTranslate, cascade);
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
41 }
16
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 }
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43
23
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
44 private static void TranslateToolButton(ToolButton toolButton)
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
45 {
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
46 Action action = toolButton.Action;
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
47 string translation = Translation.GetTranslation(toolButton.Name, "");
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
48
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
49 if (translation == "")
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 toolButton.TooltipText = action.Label;
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
52 }
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
53 else
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
54 {
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
55 toolButton.TooltipText = translation;
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 }
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
58
17
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
59 private static void CascadeTranslations(Container container, bool cascade)
16
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 {
23
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
61 if (container is ITranslatableWithActions)
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
62 {
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
63 TranslateActions((ITranslatableWithActions)container);
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
64 }
ae37467335ae Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
65
17
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
66 foreach (Widget childWidget in container.AllChildren)
16
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 {
17
a9c60e6c4b5b Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
68 TranslateWidget(childWidget, cascade);
16
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 }
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 }
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71
20
06715f517af6 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
72 private static void TranslateActions(ITranslatableWithActions actionContainer)
06715f517af6 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
73 {
06715f517af6 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
74 foreach (Action action in actionContainer.Actions)
06715f517af6 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
75 {
21
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
76 TranslateAction(action);
20
06715f517af6 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
77 }
06715f517af6 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
78 }
21
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
79
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
80 private static void TranslateAction(Action action)
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
81 {
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
82 action.Label = Translation.GetTranslation(action.Name, action.Label);
0364560fcec2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents: 20
diff changeset
83 }
16
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 }
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85 }
c9aeaeaa3ea2 Re #47: Add translatable GTK# widgets
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86