From 5dc157569de2087873deb5af431767196dd175e6 Mon Sep 17 00:00:00 2001 From: Pierre Métras Date: Sat, 15 Nov 2008 04:25:59 +0000 Subject: Initial import (version 5) --- (limited to 'pgettext.py') 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 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) + + -- cgit v0.9.1