]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
merge pgettext and npgettext into one pgettext 1317/head
authorst4lk <myhappydo@gmail.com>
Tue, 27 Jan 2015 12:40:14 +0000 (15:40 +0300)
committerst4lk <myhappydo@gmail.com>
Tue, 27 Jan 2015 12:40:14 +0000 (15:40 +0300)
tornado/locale.py
tornado/test/gettext_translations/extract_me.py
tornado/test/locale_test.py

index ef354d83e520afca5b9f7daf2ba3057a561d0e75..c6492a172320a497d4d39207fb298ba14ba64fa2 100644 (file)
@@ -274,10 +274,7 @@ class Locale(object):
         """
         raise NotImplementedError()
 
-    def pgettext(self, context, message):
-        raise NotImplementedError()
-
-    def npgettext(self, context, singular, plural, number):
+    def pgettext(self, context, message, plural_message=None, count=None):
         raise NotImplementedError()
 
     def format_date(self, date, gmt_offset=0, relative=True, shorter=False,
@@ -429,13 +426,9 @@ class CSVLocale(Locale):
             message_dict = self.translations.get("unknown", {})
         return message_dict.get(message, message)
 
-    def pgettext(self, context, message):
+    def pgettext(self, context, message, plural_message=None, count=None):
         gen_log.warning('pgettext is not supported by CSVLocale')
-        return self.translate(message)
-
-    def npgettext(self, context, singular, plural, number):
-        gen_log.warning('npgettext is not supported by CSVLocale')
-        return self.translate(singular, plural, number)
+        return self.translate(message, plural_message, count)
 
 
 class GettextLocale(Locale):
@@ -460,47 +453,41 @@ class GettextLocale(Locale):
         else:
             return self.gettext(message)
 
-    def pgettext(self, context, message):
-        """Allows to set context for translation.
+    def pgettext(self, context, message, plural_message=None, count=None):
+        """Allows to set context for translation, accept plural forms.
 
         Usage example:
 
             pgettext("law", "right")
             pgettext("good", "right")
 
-        To generate POT file with context, add following option to step 1
-        of `load_gettext_translations` sequence:
-
-            xgettext [basic options] --keyword=pgettext:1c,2
-        """
-        msg_with_ctxt = "%s%s%s" % (context, CONTEXT_SEPARATOR, message)
-        result = self.gettext(msg_with_ctxt)
-        if CONTEXT_SEPARATOR in result:
-            # Translation not found
-            result = message
-        return result
+        Plural message example:
 
-    def npgettext(self, context, singular, plural, number):
-        """Allows to set context for translation with plural form.
-
-        Usage example:
-
-            npgettext("organization", "club", "clubs", len(clubs))
-            npgettext("stick", "club", "clubs", len(clubs))
+            pgettext("organization", "club", "clubs", len(clubs))
+            pgettext("stick", "club", "clubs", len(clubs))
 
         To generate POT file with context, add following option to step 1
         of `load_gettext_translations` sequence:
 
-            xgettext [basic options] --keyword=npgettext:1c,2,3
+            xgettext [basic options] --keyword=pgettext:1c,2 --keyword=pgettext:1c,2,3
         """
-        msgs_with_ctxt = ("%s%s%s" % (context, CONTEXT_SEPARATOR, singular),
-                          "%s%s%s" % (context, CONTEXT_SEPARATOR, plural),
-                          number)
-        result = self.ngettext(*msgs_with_ctxt)
-        if CONTEXT_SEPARATOR in result:
-            # Translation not found
-            result = self.ngettext(singular, plural, number)
-        return result
+        if plural_message is not None:
+            assert count is not None
+            msgs_with_ctxt = ("%s%s%s" % (context, CONTEXT_SEPARATOR, message),
+                          "%s%s%s" % (context, CONTEXT_SEPARATOR, plural_message),
+                          count)
+            result = self.ngettext(*msgs_with_ctxt)
+            if CONTEXT_SEPARATOR in result:
+                # Translation not found
+                result = self.ngettext(message, plural_message, count)
+            return result
+        else:
+            msg_with_ctxt = "%s%s%s" % (context, CONTEXT_SEPARATOR, message)
+            result = self.gettext(msg_with_ctxt)
+            if CONTEXT_SEPARATOR in result:
+                # Translation not found
+                result = message
+            return result
 
 LOCALE_NAMES = {
     "af_ZA": {"name_en": u("Afrikaans"), "name": u("Afrikaans")},
index 063fbc5c9677ea54ea6e1d9f403730586b161cfc..58740dfbefa5d4400c62d458f5679fff573b556e 100644 (file)
@@ -2,7 +2,7 @@
 # same way as a real project.  I'm not entirely sure about the real
 # workflow here, but this seems to work.
 #
-# 1) xgettext --language=Python --keyword=_:1,2 --keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 extract_me.py -o tornado_test.po
+# 1) xgettext --language=Python --keyword=_:1,2 --keyword=pgettext:1c,2 --keyword=pgettext:1c,2,3 extract_me.py -o tornado_test.po
 # 2) Edit tornado_test.po, setting CHARSET, Plural-Forms and setting msgstr
 # 3) msgfmt tornado_test.po -o tornado_test.mo
 # 4) Put the file in the proper location: $LANG/LC_MESSAGES
@@ -11,5 +11,5 @@ from __future__ import absolute_import, division, print_function, with_statement
 _("school")
 pgettext("law", "right")
 pgettext("good", "right")
-npgettext("organization", "club", "clubs", 1)
-npgettext("stick", "club", "clubs", 1)
+pgettext("organization", "club", "clubs", 1)
+pgettext("stick", "club", "clubs", 1)
index a0bea4336720ec6dcd849412d9f649febdd58f0b..28ab8489a1be904fdbd33a99a06347eb2cb2868c 100644 (file)
@@ -43,10 +43,10 @@ class TranslationLoaderTest(unittest.TestCase):
         self.assertEqual(locale.translate("school"), u("\u00e9cole"))
         self.assertEqual(locale.pgettext("law", "right"), u("le droit"))
         self.assertEqual(locale.pgettext("good", "right"), u("le bien"))
-        self.assertEqual(locale.npgettext("organization", "club", "clubs", 1), u("le club"))
-        self.assertEqual(locale.npgettext("organization", "club", "clubs", 2), u("les clubs"))
-        self.assertEqual(locale.npgettext("stick", "club", "clubs", 1), u("le b\xe2ton"))
-        self.assertEqual(locale.npgettext("stick", "club", "clubs", 2), u("les b\xe2tons"))
+        self.assertEqual(locale.pgettext("organization", "club", "clubs", 1), u("le club"))
+        self.assertEqual(locale.pgettext("organization", "club", "clubs", 2), u("les clubs"))
+        self.assertEqual(locale.pgettext("stick", "club", "clubs", 1), u("le b\xe2ton"))
+        self.assertEqual(locale.pgettext("stick", "club", "clubs", 2), u("les b\xe2tons"))
 
 
 class LocaleDataTest(unittest.TestCase):