[Sugar-devel] [PATCH sugar-toolkit-gtk3] Use class-based API for gettext translations

Manuel Kaufmann humitos at gmail.com
Tue Aug 14 11:58:31 EDT 2012


gettext[1] supports both the GNU gettext catalog API and higher level,
class-based API that may be more appropriate for Python files.

This commit changes the code to use the higher level, class-based API
to return Unicode objects instead 8bits strings as it was. This allows
us to have the code consistent throughout all our activities.

Signed-off-by: Manuel Kaufmann <humitos at gmail.com>
---
 bin/sugar-activity                 |  7 ++++---
 src/sugar3/activity/activity.py    |  5 +++--
 src/sugar3/activity/widgets.py     |  4 +++-
 src/sugar3/graphics/alert.py       |  4 +++-
 src/sugar3/graphics/colorbutton.py |  4 +++-
 src/sugar3/util.py                 | 11 ++++++-----
 6 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/bin/sugar-activity b/bin/sugar-activity
index 5aef1ae..2bc82b0 100644
--- a/bin/sugar-activity
+++ b/bin/sugar-activity
@@ -101,9 +101,10 @@ def main():
 
     # must be done early, some activities set translations globally, SL #3654
     locale_path = i18n.get_locale_path(bundle.get_bundle_id())
-    gettext.bindtextdomain(bundle.get_bundle_id(), locale_path)
-    gettext.bindtextdomain('sugar-toolkit', sugar3.locale_path)
-    gettext.textdomain(bundle.get_bundle_id())
+
+    # Install the actual Activity Domain to be able to use _() to
+    # translate strings and get Unicode objects
+    gettext.install(bundle.get_bundle_id(), locale_path, unicode=1)
 
     splitted_module = args[0].rsplit('.', 1)
     module_name = splitted_module[0]
diff --git a/src/sugar3/activity/activity.py b/src/sugar3/activity/activity.py
index 8386c1e..b340853 100644
--- a/src/sugar3/activity/activity.py
+++ b/src/sugar3/activity/activity.py
@@ -73,6 +73,7 @@ from telepathy.interfaces import CHANNEL, \
 from telepathy.constants import CONNECTION_HANDLE_TYPE_CONTACT
 from telepathy.constants import CONNECTION_HANDLE_TYPE_ROOM
 
+import sugar3
 from sugar3 import util
 from sugar3.presence import presenceservice
 from sugar3.activity.activityservice import ActivityService
@@ -84,8 +85,8 @@ from sugar3.datastore import datastore
 from sugar3.session import XSMPClient
 from sugar3 import wm
 
-
-_ = lambda msg: gettext.dgettext('sugar-toolkit', msg)
+t = gettext.translation('sugar-toolkit', sugar3.locale_path)
+_ = t.ugettext
 
 SCOPE_PRIVATE = 'private'
 SCOPE_INVITE_ONLY = 'invite'  # shouldn't be shown in UI, it's implicit
diff --git a/src/sugar3/activity/widgets.py b/src/sugar3/activity/widgets.py
index c4015ce..5865cd3 100644
--- a/src/sugar3/activity/widgets.py
+++ b/src/sugar3/activity/widgets.py
@@ -23,6 +23,7 @@ import gettext
 from gi.repository import GConf
 import logging
 
+import sugar3
 from sugar3.graphics.toolbutton import ToolButton
 from sugar3.graphics.toolbarbox import ToolbarButton
 from sugar3.graphics.radiopalette import RadioPalette, RadioMenuButton
@@ -34,7 +35,8 @@ from sugar3.bundle.activitybundle import ActivityBundle
 from sugar3.graphics import style
 
 
-_ = lambda msg: gettext.dgettext('sugar-toolkit', msg)
+t = gettext.translation('sugar-toolkit', sugar3.locale_path)
+_ = t.ugettext
 
 
 def _create_activity_icon(metadata):
diff --git a/src/sugar3/graphics/alert.py b/src/sugar3/graphics/alert.py
index 16392cd..600d7fc 100644
--- a/src/sugar3/graphics/alert.py
+++ b/src/sugar3/graphics/alert.py
@@ -52,11 +52,13 @@ from gi.repository import GObject
 from gi.repository import GLib
 import math
 
+import sugar3
 from sugar3.graphics import style
 from sugar3.graphics.icon import Icon
 
 
-_ = lambda msg: gettext.dgettext('sugar-toolkit', msg)
+t = gettext.translation('sugar-toolkit', sugar3.locale_path)
+_ = t.ugettext
 
 
 class Alert(Gtk.EventBox):
diff --git a/src/sugar3/graphics/colorbutton.py b/src/sugar3/graphics/colorbutton.py
index 95d023a..56f668c 100644
--- a/src/sugar3/graphics/colorbutton.py
+++ b/src/sugar3/graphics/colorbutton.py
@@ -24,12 +24,14 @@ from gi.repository import GObject
 import struct
 import logging
 
+import sugar3
 from sugar3.graphics import style
 from sugar3.graphics.icon import Icon
 from sugar3.graphics.palette import Palette, ToolInvoker, WidgetInvoker
 
 
-_ = lambda msg: gettext.dgettext('sugar-toolkit', msg)
+t = gettext.translation('sugar-toolkit', sugar3.locale_path)
+_ = t.ugettext
 
 
 def get_svg_color_string(color):
diff --git a/src/sugar3/util.py b/src/sugar3/util.py
index baec3b1..923dc72 100644
--- a/src/sugar3/util.py
+++ b/src/sugar3/util.py
@@ -30,8 +30,10 @@ import tempfile
 import logging
 import atexit
 
+import sugar3
 
-_ = lambda msg: gettext.dgettext('sugar-toolkit', msg)
+t = gettext.translation('sugar-toolkit', sugar3.locale_path)
+_ = t.ugettext
 
 
 def printable_hash(in_hash):
@@ -270,10 +272,9 @@ def timestamp_to_elapsed_string(timestamp, max_levels=2):
             if key in _i18n_timestamps_cache:
                 time_period += _i18n_timestamps_cache[key]
             else:
-                tmp = gettext.dngettext('sugar-toolkit',
-                                        name_singular,
-                                        name_plural,
-                                        elapsed_units)
+                tmp = t.ngettext(name_singular,
+                                 name_plural,
+                                 elapsed_units)
                 # FIXME: This is a hack so we don't crash when a translation
                 # doesn't contain the expected number of placeholders (#2354)
                 try:
-- 
1.7.11.2



More information about the Sugar-devel mailing list