Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pgettext.py
diff options
context:
space:
mode:
Diffstat (limited to 'pgettext.py')
-rw-r--r--pgettext.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/pgettext.py b/pgettext.py
new file mode 100644
index 0000000..ac1473c
--- /dev/null
+++ b/pgettext.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Code released in the Public Domain. You can do whatever you want with this package.
+# Originally written by Pierre Métras <pierre@alterna.tv> for the OLPC XO laptop.
+
+
+from gettext import gettext
+
+
+# pgettext(msgctxt, msgid) from gettext is not supported in Python implementation < v2.6.
+# http://bugs.python.org/issue2504
+# Meanwhile we get official support, we have to simulate it.
+# See http://www.gnu.org/software/gettext/manual/gettext.html#Ambiguities for
+# more information about pgettext.
+
+# The separator between message context and message id.This value is the same as
+# the one used in gettext.h, so PO files should be still valid when Python gettext
+# module will include pgettext() function.
+GETTEXT_CONTEXT_GLUE = "\004"
+
+def pgettext(msgctxt, msgid):
+ """A custom implementation of GNU pgettext().
+ """
+ if msgctxt is not None and msgctxt is not "":
+ translation = gettext(msgctxt + GETTEXT_CONTEXT_GLUE + msgid)
+ if translation.startswith(msgctxt + GETTEXT_CONTEXT_GLUE):
+ return msgid
+ else:
+ return translation
+ else:
+ return gettext(msgid)
+
+# Map our pgettext() custom function to _p()
+_p = lambda msgctxt, msgid: pgettext(msgctxt, msgid)
+
+