]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
add pgettext, npgettext
authorst4lk <myhappydo@gmail.com>
Tue, 27 Jan 2015 11:45:13 +0000 (14:45 +0300)
committerst4lk <myhappydo@gmail.com>
Tue, 27 Jan 2015 11:45:13 +0000 (14:45 +0300)
tornado/locale.py
tornado/test/gettext_translations/extract_me.py
tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.mo
tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po
tornado/test/locale_test.py

index 07c6d582b4e6a4093f71485539df5fd66320eaee..4762c5a86783d8410cbf2b89d47c9d333399a022 100644 (file)
@@ -55,6 +55,7 @@ _default_locale = "en_US"
 _translations = {}
 _supported_locales = frozenset([_default_locale])
 _use_gettext = False
+CONTEXT_SEPARATOR = "\x04"
 
 
 def get(*locale_codes):
@@ -273,6 +274,12 @@ class Locale(object):
         """
         raise NotImplementedError()
 
+    def pgettext(self, context, message):
+        raise NotImplementedError()
+
+    def npgettext(self, context, singular, plural, number):
+        raise NotImplementedError()
+
     def format_date(self, date, gmt_offset=0, relative=True, shorter=False,
                     full_format=False):
         """Formats the given date (which should be GMT).
@@ -422,6 +429,14 @@ class CSVLocale(Locale):
             message_dict = self.translations.get("unknown", {})
         return message_dict.get(message, message)
 
+    def pgettext(self, context, message):
+        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)
+
 
 class GettextLocale(Locale):
     """Locale implementation using the `gettext` module."""
@@ -445,6 +460,24 @@ class GettextLocale(Locale):
         else:
             return self.gettext(message)
 
+    def pgettext(self, context, message):
+        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
+
+    def npgettext(self, context, singular, plural, number):
+        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
+
 LOCALE_NAMES = {
     "af_ZA": {"name_en": u("Afrikaans"), "name": u("Afrikaans")},
     "am_ET": {"name_en": u("Amharic"), "name": u('\u12a0\u121b\u122d\u129b')},
index 75406ecc77d70611d2a3986cdeb82c788063a7c2..063fbc5c9677ea54ea6e1d9f403730586b161cfc 100644 (file)
@@ -2,10 +2,14 @@
 # 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 -d tornado_test extract_me.py -o tornado_test.po
-# 2) Edit tornado_test.po, setting CHARSET and setting msgstr
+# 1) xgettext --language=Python --keyword=_:1,2 --keyword=pgettext:1c,2 --keyword=npgettext: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
 
 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)
index 089f6c7ab79294f4441121c15ebaa10c3cea02f3..a97bf9c57460ecfc27761accf90d712ea5cebb44 100644 (file)
Binary files a/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.mo and b/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.mo differ
index 732ee6da8e0e13e591a94651d7f70a18b676c753..88d72c8623a4275c85cb32e2ec35205b5b907176 100644 (file)
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2012-06-14 01:10-0700\n"
+"POT-Creation-Date: 2015-01-27 11:05+0300\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -16,7 +16,32 @@ msgstr ""
 "MIME-Version: 1.0\n"
 "Content-Type: text/plain; charset=utf-8\n"
 "Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n > 1);\n"
 
-#: extract_me.py:1
+#: extract_me.py:11
 msgid "school"
 msgstr "école"
+
+#: extract_me.py:12
+msgctxt "law"
+msgid "right"
+msgstr "le droit"
+
+#: extract_me.py:13
+msgctxt "good"
+msgid "right"
+msgstr "le bien"
+
+#: extract_me.py:14
+msgctxt "organization"
+msgid "club"
+msgid_plural "clubs"
+msgstr[0] "le club"
+msgstr[1] "les clubs"
+
+#: extract_me.py:15
+msgctxt "stick"
+msgid "club"
+msgid_plural "clubs"
+msgstr[0] "le bâton"
+msgstr[1] "les bâtons"
index d12ad52ffa810e7d809b53c71bb94a399eeba4e3..a0bea4336720ec6dcd849412d9f649febdd58f0b 100644 (file)
@@ -41,6 +41,12 @@ class TranslationLoaderTest(unittest.TestCase):
         locale = tornado.locale.get("fr_FR")
         self.assertTrue(isinstance(locale, tornado.locale.GettextLocale))
         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"))
 
 
 class LocaleDataTest(unittest.TestCase):