add plural context to TransUtils.py

svn: r11772
This commit is contained in:
Benny Malengier 2009-01-31 08:34:15 +00:00
parent 0d3201d8ab
commit 3d257ea84a

View File

@ -24,7 +24,7 @@
Provide translation assistance
"""
from gettext import gettext
from gettext import (gettext, ngettext)
def sgettext(msgid, sep='|'):
"""
@ -48,3 +48,31 @@ def sgettext(msgid, sep='|'):
sep_idx = msgid.rfind(sep)
msgval = msgid[sep_idx+1:]
return msgval
def sngettext(singular, plural, n, sep='|'):
"""
Strip the context used for resolving translation ambiguities.
The translation of singular/plural is returned unless the translation is
not available and the singular contains the separator. In that case,
the returned value is the portion of singular following the last
separator. Default separator is '|'.
@param singular: The singular form of the string to be translated.
may contain a context seperator
@type singular: unicode
@param plural: The plural form of the string to be translated.
@type plural: unicode
@param n: the amount for which to decide the translation
@type n: int
@param sep: The separator marking the context.
@type sep: unicode
@return: Translation or the original with context stripped.
@rtype: unicode
"""
msgval = ngettext(singular, plural,n)
if msgval == singular:
sep_idx = singular.rfind(sep)
msgval = singular[sep_idx+1:]
return msgval