From 4f2f5f8986594a5b05bd263bf4853587444de878 Mon Sep 17 00:00:00 2001 From: Brian Matherly Date: Tue, 12 Jan 2010 12:54:59 +0000 Subject: [PATCH] Add ngettext, sgettext, sngettext to libtranslate. svn: r14042 --- src/plugins/lib/libnarrate.py | 2 +- src/plugins/lib/libtranslate.py | 84 ++++++++++++++++++++++-- src/plugins/textreport/AncestorReport.py | 4 +- 3 files changed, 83 insertions(+), 7 deletions(-) diff --git a/src/plugins/lib/libnarrate.py b/src/plugins/lib/libnarrate.py index ae2deca9d..e32aea004 100644 --- a/src/plugins/lib/libnarrate.py +++ b/src/plugins/lib/libnarrate.py @@ -1494,7 +1494,7 @@ class Narrator(object): if translator is None: translator = Translator(Translator.DEFAULT_TRANSLATION_STR) - self.__translate_text = translator.get_text + self.__translate_text = translator.gettext self.__get_date = translator.get_date def set_subject(self, person): diff --git a/src/plugins/lib/libtranslate.py b/src/plugins/lib/libtranslate.py index 3492e9b1c..6fbdaf481 100644 --- a/src/plugins/lib/libtranslate.py +++ b/src/plugins/lib/libtranslate.py @@ -145,9 +145,9 @@ class Translator: else: 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. :type message: string @@ -156,9 +156,85 @@ class Translator: """ if self.__trans is None: - return gettext.gettext(message) + return unicode(gettext.gettext(message)) 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): """ diff --git a/src/plugins/textreport/AncestorReport.py b/src/plugins/textreport/AncestorReport.py index e653c2d32..011f72537 100644 --- a/src/plugins/textreport/AncestorReport.py +++ b/src/plugins/textreport/AncestorReport.py @@ -101,7 +101,7 @@ class AncestorReport(Report): raise ReportError(_("Person %s is not in the Database") % pid ) language = menu.get_option_by_name('trans').get_value() translator = Translator(language) - self._ = translator.get_text + self._ = translator.gettext self.__narrator = Narrator(self.database, translator=translator) @@ -226,7 +226,7 @@ class AncestorReport(Report): self.__narrator.set_subject(person) 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_buried_string())