locale_test.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import datetime
  2. import os
  3. import shutil
  4. import tempfile
  5. import unittest
  6. import tornado.locale
  7. from tornado.escape import utf8, to_unicode
  8. from tornado.util import unicode_type
  9. class TranslationLoaderTest(unittest.TestCase):
  10. # TODO: less hacky way to get isolated tests
  11. SAVE_VARS = ["_translations", "_supported_locales", "_use_gettext"]
  12. def clear_locale_cache(self):
  13. tornado.locale.Locale._cache = {}
  14. def setUp(self):
  15. self.saved = {} # type: dict
  16. for var in TranslationLoaderTest.SAVE_VARS:
  17. self.saved[var] = getattr(tornado.locale, var)
  18. self.clear_locale_cache()
  19. def tearDown(self):
  20. for k, v in self.saved.items():
  21. setattr(tornado.locale, k, v)
  22. self.clear_locale_cache()
  23. def test_csv(self):
  24. tornado.locale.load_translations(
  25. os.path.join(os.path.dirname(__file__), "csv_translations")
  26. )
  27. locale = tornado.locale.get("fr_FR")
  28. self.assertTrue(isinstance(locale, tornado.locale.CSVLocale))
  29. self.assertEqual(locale.translate("school"), u"\u00e9cole")
  30. def test_csv_bom(self):
  31. with open(
  32. os.path.join(os.path.dirname(__file__), "csv_translations", "fr_FR.csv"),
  33. "rb",
  34. ) as f:
  35. char_data = to_unicode(f.read())
  36. # Re-encode our input data (which is utf-8 without BOM) in
  37. # encodings that use the BOM and ensure that we can still load
  38. # it. Note that utf-16-le and utf-16-be do not write a BOM,
  39. # so we only test whichver variant is native to our platform.
  40. for encoding in ["utf-8-sig", "utf-16"]:
  41. tmpdir = tempfile.mkdtemp()
  42. try:
  43. with open(os.path.join(tmpdir, "fr_FR.csv"), "wb") as f:
  44. f.write(char_data.encode(encoding))
  45. tornado.locale.load_translations(tmpdir)
  46. locale = tornado.locale.get("fr_FR")
  47. self.assertIsInstance(locale, tornado.locale.CSVLocale)
  48. self.assertEqual(locale.translate("school"), u"\u00e9cole")
  49. finally:
  50. shutil.rmtree(tmpdir)
  51. def test_gettext(self):
  52. tornado.locale.load_gettext_translations(
  53. os.path.join(os.path.dirname(__file__), "gettext_translations"),
  54. "tornado_test",
  55. )
  56. locale = tornado.locale.get("fr_FR")
  57. self.assertTrue(isinstance(locale, tornado.locale.GettextLocale))
  58. self.assertEqual(locale.translate("school"), u"\u00e9cole")
  59. self.assertEqual(locale.pgettext("law", "right"), u"le droit")
  60. self.assertEqual(locale.pgettext("good", "right"), u"le bien")
  61. self.assertEqual(
  62. locale.pgettext("organization", "club", "clubs", 1), u"le club"
  63. )
  64. self.assertEqual(
  65. locale.pgettext("organization", "club", "clubs", 2), u"les clubs"
  66. )
  67. self.assertEqual(locale.pgettext("stick", "club", "clubs", 1), u"le b\xe2ton")
  68. self.assertEqual(locale.pgettext("stick", "club", "clubs", 2), u"les b\xe2tons")
  69. class LocaleDataTest(unittest.TestCase):
  70. def test_non_ascii_name(self):
  71. name = tornado.locale.LOCALE_NAMES["es_LA"]["name"]
  72. self.assertTrue(isinstance(name, unicode_type))
  73. self.assertEqual(name, u"Espa\u00f1ol")
  74. self.assertEqual(utf8(name), b"Espa\xc3\xb1ol")
  75. class EnglishTest(unittest.TestCase):
  76. def test_format_date(self):
  77. locale = tornado.locale.get("en_US")
  78. date = datetime.datetime(2013, 4, 28, 18, 35)
  79. self.assertEqual(
  80. locale.format_date(date, full_format=True), "April 28, 2013 at 6:35 pm"
  81. )
  82. now = datetime.datetime.utcnow()
  83. self.assertEqual(
  84. locale.format_date(now - datetime.timedelta(seconds=2), full_format=False),
  85. "2 seconds ago",
  86. )
  87. self.assertEqual(
  88. locale.format_date(now - datetime.timedelta(minutes=2), full_format=False),
  89. "2 minutes ago",
  90. )
  91. self.assertEqual(
  92. locale.format_date(now - datetime.timedelta(hours=2), full_format=False),
  93. "2 hours ago",
  94. )
  95. self.assertEqual(
  96. locale.format_date(
  97. now - datetime.timedelta(days=1), full_format=False, shorter=True
  98. ),
  99. "yesterday",
  100. )
  101. date = now - datetime.timedelta(days=2)
  102. self.assertEqual(
  103. locale.format_date(date, full_format=False, shorter=True),
  104. locale._weekdays[date.weekday()],
  105. )
  106. date = now - datetime.timedelta(days=300)
  107. self.assertEqual(
  108. locale.format_date(date, full_format=False, shorter=True),
  109. "%s %d" % (locale._months[date.month - 1], date.day),
  110. )
  111. date = now - datetime.timedelta(days=500)
  112. self.assertEqual(
  113. locale.format_date(date, full_format=False, shorter=True),
  114. "%s %d, %d" % (locale._months[date.month - 1], date.day, date.year),
  115. )
  116. def test_friendly_number(self):
  117. locale = tornado.locale.get("en_US")
  118. self.assertEqual(locale.friendly_number(1000000), "1,000,000")
  119. def test_list(self):
  120. locale = tornado.locale.get("en_US")
  121. self.assertEqual(locale.list([]), "")
  122. self.assertEqual(locale.list(["A"]), "A")
  123. self.assertEqual(locale.list(["A", "B"]), "A and B")
  124. self.assertEqual(locale.list(["A", "B", "C"]), "A, B and C")
  125. def test_format_day(self):
  126. locale = tornado.locale.get("en_US")
  127. date = datetime.datetime(2013, 4, 28, 18, 35)
  128. self.assertEqual(locale.format_day(date=date, dow=True), "Sunday, April 28")
  129. self.assertEqual(locale.format_day(date=date, dow=False), "April 28")