Add ngettext, sgettext, sngettext to libtranslate.

svn: r14042
This commit is contained in:
Brian Matherly 2010-01-12 12:54:59 +00:00
parent 9ccdb167cd
commit 4f2f5f8986
3 changed files with 83 additions and 7 deletions

View File

@ -1494,7 +1494,7 @@ class Narrator(object):
if translator is None: if translator is None:
translator = Translator(Translator.DEFAULT_TRANSLATION_STR) translator = Translator(Translator.DEFAULT_TRANSLATION_STR)
self.__translate_text = translator.get_text self.__translate_text = translator.gettext
self.__get_date = translator.get_date self.__get_date = translator.get_date
def set_subject(self, person): def set_subject(self, person):

View File

@ -145,9 +145,9 @@ class Translator:
else: else:
self.__dd = DateHandler.displayer self.__dd = DateHandler.displayer
def get_text(self, message): def gettext(self, message):
""" """
Return the translated string. Return the unicode translated string.
:param message: The message to be translated. :param message: The message to be translated.
:type message: string :type message: string
@ -156,9 +156,85 @@ class Translator:
""" """
if self.__trans is None: if self.__trans is None:
return gettext.gettext(message) return unicode(gettext.gettext(message))
else: else:
return self.__trans.gettext(message) return self.__trans.ugettext(message)
def ngettext(self, singular, plural, n):
"""
Return the unicode translated singular/plural string.
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 separator
: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
:returns: The translated singular/plural message
:rtype: unicode
"""
if self.__trans is None:
return unicode(gettext.ngettext(singular, plural, n))
else:
return self.__trans.ungettext(singular, plural, n)
def sgettext(self, msgid, sep='|'):
"""
Strip the context used for resolving translation ambiguities.
The translation of msgid is returned unless the translation is
not available and the msgid contains the separator. In that case,
the returned value is the portion of msgid following the last
separator. Default separator is '|'.
:param msgid: The string to translated.
:type msgid: unicode
:param sep: The separator marking the context.
:type sep: unicode
:returns: Translation or the original with context stripped.
:rtype: unicode
"""
msgval = self.gettext(msgid)
if msgval == msgid:
sep_idx = msgid.rfind(sep)
msgval = msgid[sep_idx+1:]
return unicode(msgval)
def sngettext(self, 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
:returns: Translation or the original with context stripped.
:rtype: unicode
"""
msgval = self.ngettext(singular, plural, n)
if msgval == singular:
sep_idx = singular.rfind(sep)
msgval = singular[sep_idx+1:]
return unicode(msgval)
def get_date(self, date): def get_date(self, date):
""" """

View File

@ -101,7 +101,7 @@ class AncestorReport(Report):
raise ReportError(_("Person %s is not in the Database") % pid ) raise ReportError(_("Person %s is not in the Database") % pid )
language = menu.get_option_by_name('trans').get_value() language = menu.get_option_by_name('trans').get_value()
translator = Translator(language) translator = Translator(language)
self._ = translator.get_text self._ = translator.gettext
self.__narrator = Narrator(self.database, self.__narrator = Narrator(self.database,
translator=translator) translator=translator)
@ -226,7 +226,7 @@ class AncestorReport(Report):
self.__narrator.set_subject(person) self.__narrator.set_subject(person)
self.doc.write_text(self.__narrator.get_born_string()) self.doc.write_text(self.__narrator.get_born_string())
self.doc.write_text(self.__narrator.get_baptized_string()) self.doc.write_text(self.__narrator.get_baptised_string())
self.doc.write_text(self.__narrator.get_died_string()) self.doc.write_text(self.__narrator.get_died_string())
self.doc.write_text(self.__narrator.get_buried_string()) self.doc.write_text(self.__narrator.get_buried_string())