]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Fix load_gettext_translations on python 3
authorBen Darnell <ben@bendarnell.com>
Thu, 14 Jun 2012 08:41:04 +0000 (01:41 -0700)
committerBen Darnell <ben@bendarnell.com>
Thu, 14 Jun 2012 08:41:04 +0000 (01:41 -0700)
MANIFEST.in
setup.py
tornado/locale.py
tornado/test/gettext_translations/extract_me.py [new file with mode: 0644]
tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.mo [new file with mode: 0644]
tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po [new file with mode: 0644]
tornado/test/locale_test.py

index 409978e9e6794cb72e169aeeef4e41516b6ac222..5b28bd64470bc8d0a7fd37b47f2f95474801d49b 100644 (file)
@@ -5,6 +5,8 @@ include tornado/test/README
 include tornado/test/test.crt
 include tornado/test/test.key
 include tornado/test/csv_translations/fr_FR.csv
+include tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.mo
+include tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po
 include tornado/test/static/robots.txt
 include tornado/test/templates/utf8.html
 global-exclude _auto2to3*
\ No newline at end of file
index 180306cc72294381d2e0f6b2392f18126e7619ad..a0a099d2554b7398f766fa6b87b0cabc3d3a6441 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -45,9 +45,19 @@ distutils.core.setup(
     packages = ["tornado", "tornado.test", "tornado.platform"],
     package_data = {
         "tornado": ["ca-certificates.crt"],
-        # data files need to be listed both here and in MANIFEST.in
-        "tornado.test": ["README", "test.crt", "test.key", "static/robots.txt",
-                         "templates/utf8.html", "csv_translations/fr_FR.csv"],
+        # data files need to be listed both here (which determines what gets
+        # installed) and in MANIFEST.in (which determines what gets included
+        # in the sdist tarball)
+        "tornado.test": [
+            "README",
+            "test.crt",
+            "test.key",
+            "static/robots.txt",
+            "templates/utf8.html",
+            "csv_translations/fr_FR.csv",
+            "gettext_translations/fr_FR/LC_MESSAGES/tornado_test.mo",
+            "gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po",
+            ],
         },
     ext_modules = extensions,
     author="Facebook",
index c883a4e91a7ae29372663aee8dabfb6ec7cb11dc..9f8ee7ea8217c1b6da85a1f10c8dcd05d757aa0d 100644 (file)
@@ -188,7 +188,7 @@ def load_gettext_translations(directory, domain):
             continue
     _supported_locales = frozenset(_translations.keys() + [_default_locale])
     _use_gettext = True
-    logging.info("Supported locales: %s", sorted(_supported_locales))
+    logging.debug("Supported locales: %s", sorted(_supported_locales))
 
 
 def get_supported_locales():
@@ -423,12 +423,25 @@ class CSVLocale(Locale):
 
 class GettextLocale(Locale):
     """Locale implementation using the gettext module."""
+    def __init__(self, code, translations):
+        try:
+            # python 2
+            self.ngettext = translations.ungettext
+            self.gettext = translations.ugettext
+        except AttributeError:
+            # python 3
+            self.ngettext = translations.ngettext
+            self.gettext = translations.gettext
+        # self.gettext must exist before __init__ is called, since it
+        # calls into self.translate
+        super(GettextLocale, self).__init__(code, translations)
+
     def translate(self, message, plural_message=None, count=None):
         if plural_message is not None:
             assert count is not None
-            return self.translations.ungettext(message, plural_message, count)
+            return self.ngettext(message, plural_message, count)
         else:
-            return self.translations.ugettext(message)
+            return self.gettext(message)
 
 LOCALE_NAMES = {
     "af_ZA": {"name_en": u"Afrikaans", "name": u"Afrikaans"},
diff --git a/tornado/test/gettext_translations/extract_me.py b/tornado/test/gettext_translations/extract_me.py
new file mode 100644 (file)
index 0000000..e2d7476
--- /dev/null
@@ -0,0 +1,9 @@
+# Dummy source file to allow creation of the initial .po file in the
+# 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
+# 3) msgfmt tornado_test.po -o tornado_test.mo
+# 4) Put the file in the proper location: $LANG/LC_MESSAGES
+_("school")
diff --git a/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.mo b/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.mo
new file mode 100644 (file)
index 0000000..089f6c7
Binary files /dev/null and b/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.mo differ
diff --git a/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po b/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po
new file mode 100644 (file)
index 0000000..732ee6d
--- /dev/null
@@ -0,0 +1,22 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2012-06-14 01:10-0700\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"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=utf-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#: extract_me.py:1
+msgid "school"
+msgstr "école"
index eabc8ffc940a9ec9465c7ef247607ce5a521e4ff..333fa05d95bb5cd75672796c8a7a6eb890166079 100644 (file)
@@ -8,17 +8,32 @@ class TranslationLoaderTest(unittest.TestCase):
     # TODO: less hacky way to get isolated tests
     SAVE_VARS = ['_translations', '_supported_locales', '_use_gettext']
 
+    def clear_locale_cache(self):
+        if hasattr(tornado.locale.Locale, '_cache'):
+            del tornado.locale.Locale._cache
+
     def setUp(self):
         self.saved = {}
         for var in TranslationLoaderTest.SAVE_VARS:
             self.saved[var] = getattr(tornado.locale, var)
+        self.clear_locale_cache()
 
     def tearDown(self):
         for k, v in self.saved.items():
             setattr(tornado.locale, k, v)
+        self.clear_locale_cache()
 
     def test_csv(self):
         tornado.locale.load_translations(
             os.path.join(os.path.dirname(__file__), 'csv_translations'))
         locale = tornado.locale.get("fr_FR")
+        self.assertTrue(isinstance(locale, tornado.locale.CSVLocale))
+        self.assertEqual(locale.translate("school"), u"\u00e9cole")
+
+    def test_gettext(self):
+        tornado.locale.load_gettext_translations(
+            os.path.join(os.path.dirname(__file__), 'gettext_translations'),
+            "tornado_test")
+        locale = tornado.locale.get("fr_FR")
+        self.assertTrue(isinstance(locale, tornado.locale.GettextLocale))
         self.assertEqual(locale.translate("school"), u"\u00e9cole")