From: Ben Darnell Date: Thu, 14 Jun 2012 08:41:04 +0000 (-0700) Subject: Fix load_gettext_translations on python 3 X-Git-Tag: v2.4.0~63 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=489997d18dfbd63e3e581b1fce62b2edf9583403;p=thirdparty%2Ftornado.git Fix load_gettext_translations on python 3 --- diff --git a/MANIFEST.in b/MANIFEST.in index 409978e9e..5b28bd644 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -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 diff --git a/setup.py b/setup.py index 180306cc7..a0a099d25 100644 --- 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", diff --git a/tornado/locale.py b/tornado/locale.py index c883a4e91..9f8ee7ea8 100644 --- a/tornado/locale.py +++ b/tornado/locale.py @@ -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 index 000000000..e2d7476bb --- /dev/null +++ b/tornado/test/gettext_translations/extract_me.py @@ -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 index 000000000..089f6c7ab 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 index 000000000..732ee6da8 --- /dev/null +++ b/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po @@ -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 , 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 \n" +"Language-Team: LANGUAGE \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" diff --git a/tornado/test/locale_test.py b/tornado/test/locale_test.py index eabc8ffc9..333fa05d9 100644 --- a/tornado/test/locale_test.py +++ b/tornado/test/locale_test.py @@ -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")