Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/exercises/en/Exercise19.activity/exercise19.py
blob: a7e28f4d3ba762b9b52984de02eb78692496be13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# coding=utf-8

"""Add internationalisation support to a Sugar activity.

Below is a simple Sugar activity which displays a different string every time
the button is pressed. You must modify it to add internationalisation support
so that its UI can be viewed in a different language.

All the other files for the activity (activity.info, setup.py, etc.) have been
provided already; you must modify the code in this file to add
internationalisation support, generate a POT file, and then translate it to
Spanish.

Note that the original strings in the program are always in English, by
convention.

 1. Add an ‘import’ statement for the gettext module:
        from gettext import gettext as _
 2. Mark all the relevant strings in the program as translatable using the _()
    function.
 3. Generate a POT file by executing ‘./setup.py genpot’ in a terminal (after
    changing to this directory).
 4. Copy the generated po/Exercise19.pot file to po/es.po.
 5. Either open es.po in a text editor, or use a translation tool like
    gtranslator, and translate the strings to Spanish.
 6. Run ‘./setup.py dev’ to add the activity to Sugar on your computer.
 7. Run ‘./setup.py build’ to compile the .po files into machine-readable .mo
    files in the ‘locale’ directory.
 8. Run ‘LANG=es sugar-emulator’ to run the Sugar emulator in Spanish and test
    the new translation. The whole activity should appear in Spanish.

Note: It is not possible to use Pootle for this exercise because it’s an online
service and would require the exercise’s code to be uploaded to the internet.
"""

from gi.repository import Gtk
from sugar3.activity import activity, widgets
from sugar3.graphics.toolbarbox import ToolbarBox


class Exercise19Activity(activity.Activity):
    """A simple window which displays a label and a button."""
    def __init__(self, handle):
        super(Exercise19Activity, self).__init__(handle)

        # Create the standard activity toolbox.
        toolbar_box = ToolbarBox()
        self.set_toolbar_box(toolbar_box)
        toolbar_box.show()

        main_toolbar = toolbar_box.toolbar

        activity_toolbar_button = widgets.ActivityToolbarButton(self)
        main_toolbar.insert(activity_toolbar_button, 0)
        activity_toolbar_button.show()

        stop_button = widgets.StopButton(self)
        stop_button.show()
        main_toolbar.insert(stop_button, -1)

        # Add a box to the window.
        box = Gtk.Box()
        box.set_orientation(Gtk.Orientation.VERTICAL)
        box.set_spacing(8)
        box.set_border_width(8)
        box.show()
        self.set_canvas(box)

        # Set up a list of text strings to display in the label.
        self._strings = (
            'Initial string.',
            'Second string.',
            'Handle object is ‘%s’.' % handle,
            'Another string.',
        )
        self._next_string = 0

        # Create and set up the label and button.
        label = Gtk.Label()
        label.show()
        box.pack_start(label, True, True, 0)

        button = Gtk.Button('Change Label')
        button.show()
        box.pack_start(button, False, True, 0)

        # Connect the button callback.
        button.connect('clicked', self.__button_clicked_cb)

        # Store the label as an object variable so it can be accessed from the
        # button callback, and then set the initial text for the label.
        self._label = label
        self.__rotate_label_text()

    def __button_clicked_cb(self, button):
        """Callback to change the label text when the button is clicked."""
        self.__rotate_label_text()

    def __rotate_label_text(self):
        """Change the label to display the next string in the rotation.

        The modulus operation is used to wrap self._next_string around when
        it reaches the final index in the self._strings array.
        """
        self._label.set_text(self._strings[self._next_string])
        self._next_string = (self._next_string + 1) % len(self._strings)