diff options
Diffstat (limited to 'exercises/en/Exercise19.activity/exercise19_solution.py')
-rw-r--r-- | exercises/en/Exercise19.activity/exercise19_solution.py | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/exercises/en/Exercise19.activity/exercise19_solution.py b/exercises/en/Exercise19.activity/exercise19_solution.py new file mode 100644 index 0000000..4818446 --- /dev/null +++ b/exercises/en/Exercise19.activity/exercise19_solution.py @@ -0,0 +1,107 @@ +# 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. + 7. 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 +from gettext import gettext as _ + + +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) |