Permalink
Browse files

Fix load_gettext_translations on python 3

  • Loading branch information...
1 parent 562d960 commit 489997d18dfbd63e3e581b1fce62b2edf9583403 @bdarnell bdarnell committed Jun 14, 2012
View
@@ -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*
View
@@ -45,9 +45,19 @@
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",
View
@@ -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 @@ def translate(self, message, plural_message=None, count=None):
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"},
@@ -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")
@@ -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"
@@ -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")

0 comments on commit 489997d

Please sign in to comment.