[Sugar-devel] [PATCH sugar-toolkit v2] sugar.activity.i18n: add pgettext()

Sascha Silbe silbe at activitycentral.com
Tue Feb 15 14:49:10 EST 2011


Python 2 will never have pgettext() [1], so we need to ship our own version.
sugar.activity.i18n is the best place for that.

[1] http://bugs.python.org/issue2504#msg122482

Signed-off-by: Sascha Silbe <silbe at activitycentral.com>
---
v1->v2: add docstring and comment, msgid->message

 src/sugar/activity/i18n.py |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/src/sugar/activity/i18n.py b/src/sugar/activity/i18n.py
index 7b09fc2..1ddbd79 100644
--- a/src/sugar/activity/i18n.py
+++ b/src/sugar/activity/i18n.py
@@ -19,6 +19,7 @@

 import gconf

+from gettext import gettext
 import locale
 import os
 import struct
@@ -94,6 +95,23 @@ def _extract_modification_time(file_path):
     raise ValueError('Could not find a revision date')


+# We ship our own version of pgettext() because Python 2.x will never contain
+# it: http://bugs.python.org/issue2504#msg122482
+def pgettext(context, message):
+    """
+    Return the localized translation of message, based on context and
+    the current global domain, language, and locale directory.
+
+    Similar to gettext(). Context is a string used to disambiguate
+    messages that are the same in the source language (usually english),
+    but might be different in one or more of the target languages.
+    """
+    translation = gettext('\x04'.join([context, message]))
+    if '\x04' in translation:
+        return message
+    return translation
+
+
 def get_locale_path(bundle_id):
     """ Returns the locale path, which is the directory where the preferred
         MO file is located.
--
1.7.2.3



More information about the Sugar-devel mailing list