diff options
author | Pierre Métras <pierre@alterna.tv> | 2008-11-15 04:25:59 (GMT) |
---|---|---|
committer | Pierre Métras <pierre@alterna.tv> | 2008-11-15 04:25:59 (GMT) |
commit | 5dc157569de2087873deb5af431767196dd175e6 (patch) | |
tree | 7f63ff4f794cee3e95837bbd58b06f6d5e92d6a9 /pgettext.py |
Initial import (version 5)v5
Diffstat (limited to 'pgettext.py')
-rw-r--r-- | pgettext.py | 37 |
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) + + |