/Lib/test/test_gettext.py

http://unladen-swallow.googlecode.com/ · Python · 447 lines · 357 code · 54 blank · 36 comment · 19 complexity · 0f6c7448b0114cb4226de2a7601f36a7 MD5 · raw file

  1. import os
  2. import base64
  3. import shutil
  4. import gettext
  5. import unittest
  6. from test import test_support
  7. # TODO:
  8. # - Add new tests, for example for "dgettext"
  9. # - Remove dummy tests, for example testing for single and double quotes
  10. # has no sense, it would have if we were testing a parser (i.e. pygettext)
  11. # - Tests should have only one assert.
  12. GNU_MO_DATA = '''\
  13. 3hIElQAAAAAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj
  14. AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD
  15. AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAFJh
  16. eW1vbmQgTHV4dXJ5IFlhY2gtdABUaGVyZSBpcyAlcyBmaWxlAFRoZXJlIGFyZSAlcyBmaWxlcwBU
  17. aGlzIG1vZHVsZSBwcm92aWRlcyBpbnRlcm5hdGlvbmFsaXphdGlvbiBhbmQgbG9jYWxpemF0aW9u
  18. CnN1cHBvcnQgZm9yIHlvdXIgUHl0aG9uIHByb2dyYW1zIGJ5IHByb3ZpZGluZyBhbiBpbnRlcmZh
  19. Y2UgdG8gdGhlIEdOVQpnZXR0ZXh0IG1lc3NhZ2UgY2F0YWxvZyBsaWJyYXJ5LgBtdWxsdXNrAG51
  20. ZGdlIG51ZGdlAFByb2plY3QtSWQtVmVyc2lvbjogMi4wClBPLVJldmlzaW9uLURhdGU6IDIwMDAt
  21. MDgtMjkgMTI6MTktMDQ6MDAKTGFzdC1UcmFuc2xhdG9yOiBKLiBEYXZpZCBJYsOhw7FleiA8ai1k
  22. YXZpZEBub29zLmZyPgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uL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A3Yml0CkdlbmVyYXRl
  38. ZC1CeTogbWFudWFsbHkKAMKkeXoA
  39. '''
  40. MMO_DATA = '''\
  41. 3hIElQAAAAABAAAAHAAAACQAAAADAAAALAAAAAAAAAA4AAAAeAEAADkAAAABAAAAAAAAAAAAAAAA
  42. UHJvamVjdC1JZC1WZXJzaW9uOiBObyBQcm9qZWN0IDAuMApQT1QtQ3JlYXRpb24tRGF0ZTogV2Vk
  43. IERlYyAxMSAwNzo0NDoxNSAyMDAyClBPLVJldmlzaW9uLURhdGU6IDIwMDItMDgtMTQgMDE6MTg6
  44. NTgrMDA6MDAKTGFzdC1UcmFuc2xhdG9yOiBKb2huIERvZSA8amRvZUBleGFtcGxlLmNvbT4KSmFu
  45. ZSBGb29iYXIgPGpmb29iYXJAZXhhbXBsZS5jb20+Ckxhbmd1YWdlLVRlYW06IHh4IDx4eEBleGFt
  46. cGxlLmNvbT4KTUlNRS1WZXJzaW9uOiAxLjAKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFy
  47. c2V0PWlzby04ODU5LTE1CkNvbnRlbnQtVHJhbnNmZXItRW5jb2Rpbmc6IHF1b3RlZC1wcmludGFi
  48. bGUKR2VuZXJhdGVkLUJ5OiBweWdldHRleHQucHkgMS4zCgA=
  49. '''
  50. LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
  51. MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
  52. UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo')
  53. MMOFILE = os.path.join(LOCALEDIR, 'metadata.mo')
  54. try:
  55. LANG = os.environ['LANGUAGE']
  56. except:
  57. LANG = 'en'
  58. class GettextBaseTest(unittest.TestCase):
  59. def setUp(self):
  60. if not os.path.isdir(LOCALEDIR):
  61. os.makedirs(LOCALEDIR)
  62. fp = open(MOFILE, 'wb')
  63. fp.write(base64.decodestring(GNU_MO_DATA))
  64. fp.close()
  65. fp = open(UMOFILE, 'wb')
  66. fp.write(base64.decodestring(UMO_DATA))
  67. fp.close()
  68. fp = open(MMOFILE, 'wb')
  69. fp.write(base64.decodestring(MMO_DATA))
  70. fp.close()
  71. os.environ['LANGUAGE'] = 'xx'
  72. def tearDown(self):
  73. os.environ['LANGUAGE'] = LANG
  74. shutil.rmtree(os.path.split(LOCALEDIR)[0])
  75. class GettextTestCase1(GettextBaseTest):
  76. def setUp(self):
  77. GettextBaseTest.setUp(self)
  78. self.localedir = os.curdir
  79. self.mofile = MOFILE
  80. gettext.install('gettext', self.localedir)
  81. def test_some_translations(self):
  82. eq = self.assertEqual
  83. # test some translations
  84. eq(_('albatross'), 'albatross')
  85. eq(_(u'mullusk'), 'bacon')
  86. eq(_(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove')
  87. eq(_(ur'nudge nudge'), 'wink wink')
  88. def test_double_quotes(self):
  89. eq = self.assertEqual
  90. # double quotes
  91. eq(_("albatross"), 'albatross')
  92. eq(_(u"mullusk"), 'bacon')
  93. eq(_(r"Raymond Luxury Yach-t"), 'Throatwobbler Mangrove')
  94. eq(_(ur"nudge nudge"), 'wink wink')
  95. def test_triple_single_quotes(self):
  96. eq = self.assertEqual
  97. # triple single quotes
  98. eq(_('''albatross'''), 'albatross')
  99. eq(_(u'''mullusk'''), 'bacon')
  100. eq(_(r'''Raymond Luxury Yach-t'''), 'Throatwobbler Mangrove')
  101. eq(_(ur'''nudge nudge'''), 'wink wink')
  102. def test_triple_double_quotes(self):
  103. eq = self.assertEqual
  104. # triple double quotes
  105. eq(_("""albatross"""), 'albatross')
  106. eq(_(u"""mullusk"""), 'bacon')
  107. eq(_(r"""Raymond Luxury Yach-t"""), 'Throatwobbler Mangrove')
  108. eq(_(ur"""nudge nudge"""), 'wink wink')
  109. def test_multiline_strings(self):
  110. eq = self.assertEqual
  111. # multiline strings
  112. eq(_('''This module provides internationalization and localization
  113. support for your Python programs by providing an interface to the GNU
  114. gettext message catalog library.'''),
  115. '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
  116. fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
  117. trggrkg zrffntr pngnybt yvoenel.''')
  118. def test_the_alternative_interface(self):
  119. eq = self.assertEqual
  120. # test the alternative interface
  121. fp = open(self.mofile, 'rb')
  122. t = gettext.GNUTranslations(fp)
  123. fp.close()
  124. # Install the translation object
  125. t.install()
  126. eq(_('nudge nudge'), 'wink wink')
  127. # Try unicode return type
  128. t.install(unicode=True)
  129. eq(_('mullusk'), 'bacon')
  130. # Test installation of other methods
  131. import __builtin__
  132. t.install(unicode=True, names=["gettext", "lgettext"])
  133. eq(_, t.ugettext)
  134. eq(__builtin__.gettext, t.ugettext)
  135. eq(lgettext, t.lgettext)
  136. del __builtin__.gettext
  137. del __builtin__.lgettext
  138. class GettextTestCase2(GettextBaseTest):
  139. def setUp(self):
  140. GettextBaseTest.setUp(self)
  141. self.localedir = os.curdir
  142. # Set up the bindings
  143. gettext.bindtextdomain('gettext', self.localedir)
  144. gettext.textdomain('gettext')
  145. # For convenience
  146. self._ = gettext.gettext
  147. def test_bindtextdomain(self):
  148. self.assertEqual(gettext.bindtextdomain('gettext'), self.localedir)
  149. def test_textdomain(self):
  150. self.assertEqual(gettext.textdomain(), 'gettext')
  151. def test_some_translations(self):
  152. eq = self.assertEqual
  153. # test some translations
  154. eq(self._('albatross'), 'albatross')
  155. eq(self._(u'mullusk'), 'bacon')
  156. eq(self._(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove')
  157. eq(self._(ur'nudge nudge'), 'wink wink')
  158. def test_double_quotes(self):
  159. eq = self.assertEqual
  160. # double quotes
  161. eq(self._("albatross"), 'albatross')
  162. eq(self._(u"mullusk"), 'bacon')
  163. eq(self._(r"Raymond Luxury Yach-t"), 'Throatwobbler Mangrove')
  164. eq(self._(ur"nudge nudge"), 'wink wink')
  165. def test_triple_single_quotes(self):
  166. eq = self.assertEqual
  167. # triple single quotes
  168. eq(self._('''albatross'''), 'albatross')
  169. eq(self._(u'''mullusk'''), 'bacon')
  170. eq(self._(r'''Raymond Luxury Yach-t'''), 'Throatwobbler Mangrove')
  171. eq(self._(ur'''nudge nudge'''), 'wink wink')
  172. def test_triple_double_quotes(self):
  173. eq = self.assertEqual
  174. # triple double quotes
  175. eq(self._("""albatross"""), 'albatross')
  176. eq(self._(u"""mullusk"""), 'bacon')
  177. eq(self._(r"""Raymond Luxury Yach-t"""), 'Throatwobbler Mangrove')
  178. eq(self._(ur"""nudge nudge"""), 'wink wink')
  179. def test_multiline_strings(self):
  180. eq = self.assertEqual
  181. # multiline strings
  182. eq(self._('''This module provides internationalization and localization
  183. support for your Python programs by providing an interface to the GNU
  184. gettext message catalog library.'''),
  185. '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
  186. fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
  187. trggrkg zrffntr pngnybt yvoenel.''')
  188. class PluralFormsTestCase(GettextBaseTest):
  189. def setUp(self):
  190. GettextBaseTest.setUp(self)
  191. self.mofile = MOFILE
  192. def test_plural_forms1(self):
  193. eq = self.assertEqual
  194. x = gettext.ngettext('There is %s file', 'There are %s files', 1)
  195. eq(x, 'Hay %s fichero')
  196. x = gettext.ngettext('There is %s file', 'There are %s files', 2)
  197. eq(x, 'Hay %s ficheros')
  198. def test_plural_forms2(self):
  199. eq = self.assertEqual
  200. fp = open(self.mofile, 'rb')
  201. t = gettext.GNUTranslations(fp)
  202. fp.close()
  203. x = t.ngettext('There is %s file', 'There are %s files', 1)
  204. eq(x, 'Hay %s fichero')
  205. x = t.ngettext('There is %s file', 'There are %s files', 2)
  206. eq(x, 'Hay %s ficheros')
  207. def test_hu(self):
  208. eq = self.assertEqual
  209. f = gettext.c2py('0')
  210. s = ''.join([ str(f(x)) for x in range(200) ])
  211. eq(s, "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
  212. def test_de(self):
  213. eq = self.assertEqual
  214. f = gettext.c2py('n != 1')
  215. s = ''.join([ str(f(x)) for x in range(200) ])
  216. eq(s, "10111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
  217. def test_fr(self):
  218. eq = self.assertEqual
  219. f = gettext.c2py('n>1')
  220. s = ''.join([ str(f(x)) for x in range(200) ])
  221. eq(s, "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
  222. def test_gd(self):
  223. eq = self.assertEqual
  224. f = gettext.c2py('n==1 ? 0 : n==2 ? 1 : 2')
  225. s = ''.join([ str(f(x)) for x in range(200) ])
  226. eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
  227. def test_gd2(self):
  228. eq = self.assertEqual
  229. # Tests the combination of parentheses and "?:"
  230. f = gettext.c2py('n==1 ? 0 : (n==2 ? 1 : 2)')
  231. s = ''.join([ str(f(x)) for x in range(200) ])
  232. eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
  233. def test_lt(self):
  234. eq = self.assertEqual
  235. f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2')
  236. s = ''.join([ str(f(x)) for x in range(200) ])
  237. eq(s, "20111111112222222222201111111120111111112011111111201111111120111111112011111111201111111120111111112011111111222222222220111111112011111111201111111120111111112011111111201111111120111111112011111111")
  238. def test_ru(self):
  239. eq = self.assertEqual
  240. f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
  241. s = ''.join([ str(f(x)) for x in range(200) ])
  242. eq(s, "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222")
  243. def test_pl(self):
  244. eq = self.assertEqual
  245. f = gettext.c2py('n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
  246. s = ''.join([ str(f(x)) for x in range(200) ])
  247. eq(s, "20111222222222222222221112222222111222222211122222221112222222111222222211122222221112222222111222222211122222222222222222111222222211122222221112222222111222222211122222221112222222111222222211122222")
  248. def test_sl(self):
  249. eq = self.assertEqual
  250. f = gettext.c2py('n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3')
  251. s = ''.join([ str(f(x)) for x in range(200) ])
  252. eq(s, "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333")
  253. def test_security(self):
  254. raises = self.assertRaises
  255. # Test for a dangerous expression
  256. raises(ValueError, gettext.c2py, "os.chmod('/etc/passwd',0777)")
  257. class UnicodeTranslationsTest(GettextBaseTest):
  258. def setUp(self):
  259. GettextBaseTest.setUp(self)
  260. fp = open(UMOFILE, 'rb')
  261. try:
  262. self.t = gettext.GNUTranslations(fp)
  263. finally:
  264. fp.close()
  265. self._ = self.t.ugettext
  266. def test_unicode_msgid(self):
  267. unless = self.failUnless
  268. unless(isinstance(self._(''), unicode))
  269. unless(isinstance(self._(u''), unicode))
  270. def test_unicode_msgstr(self):
  271. eq = self.assertEqual
  272. eq(self._(u'ab\xde'), u'\xa4yz')
  273. class WeirdMetadataTest(GettextBaseTest):
  274. def setUp(self):
  275. GettextBaseTest.setUp(self)
  276. fp = open(MMOFILE, 'rb')
  277. try:
  278. try:
  279. self.t = gettext.GNUTranslations(fp)
  280. except:
  281. self.tearDown()
  282. raise
  283. finally:
  284. fp.close()
  285. def test_weird_metadata(self):
  286. info = self.t.info()
  287. self.assertEqual(info['last-translator'],
  288. 'John Doe <jdoe@example.com>\nJane Foobar <jfoobar@example.com>')
  289. def test_main():
  290. test_support.run_unittest(__name__)
  291. if __name__ == '__main__':
  292. test_main()
  293. # For reference, here's the .po file used to created the GNU_MO_DATA above.
  294. #
  295. # The original version was automatically generated from the sources with
  296. # pygettext. Later it was manually modified to add plural forms support.
  297. '''
  298. # Dummy translation for the Python test_gettext.py module.
  299. # Copyright (C) 2001 Python Software Foundation
  300. # Barry Warsaw <barry@python.org>, 2000.
  301. #
  302. msgid ""
  303. msgstr ""
  304. "Project-Id-Version: 2.0\n"
  305. "PO-Revision-Date: 2003-04-11 14:32-0400\n"
  306. "Last-Translator: J. David Ibanez <j-david@noos.fr>\n"
  307. "Language-Team: XX <python-dev@python.org>\n"
  308. "MIME-Version: 1.0\n"
  309. "Content-Type: text/plain; charset=iso-8859-1\n"
  310. "Content-Transfer-Encoding: 8bit\n"
  311. "Generated-By: pygettext.py 1.1\n"
  312. "Plural-Forms: nplurals=2; plural=n!=1;\n"
  313. #: test_gettext.py:19 test_gettext.py:25 test_gettext.py:31 test_gettext.py:37
  314. #: test_gettext.py:51 test_gettext.py:80 test_gettext.py:86 test_gettext.py:92
  315. #: test_gettext.py:98
  316. msgid "nudge nudge"
  317. msgstr "wink wink"
  318. #: test_gettext.py:16 test_gettext.py:22 test_gettext.py:28 test_gettext.py:34
  319. #: test_gettext.py:77 test_gettext.py:83 test_gettext.py:89 test_gettext.py:95
  320. msgid "albatross"
  321. msgstr ""
  322. #: test_gettext.py:18 test_gettext.py:24 test_gettext.py:30 test_gettext.py:36
  323. #: test_gettext.py:79 test_gettext.py:85 test_gettext.py:91 test_gettext.py:97
  324. msgid "Raymond Luxury Yach-t"
  325. msgstr "Throatwobbler Mangrove"
  326. #: test_gettext.py:17 test_gettext.py:23 test_gettext.py:29 test_gettext.py:35
  327. #: test_gettext.py:56 test_gettext.py:78 test_gettext.py:84 test_gettext.py:90
  328. #: test_gettext.py:96
  329. msgid "mullusk"
  330. msgstr "bacon"
  331. #: test_gettext.py:40 test_gettext.py:101
  332. msgid ""
  333. "This module provides internationalization and localization\n"
  334. "support for your Python programs by providing an interface to the GNU\n"
  335. "gettext message catalog library."
  336. msgstr ""
  337. "Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba\n"
  338. "fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH\n"
  339. "trggrkg zrffntr pngnybt yvoenel."
  340. # Manually added, as neither pygettext nor xgettext support plural forms
  341. # in Python.
  342. msgid "There is %s file"
  343. msgid_plural "There are %s files"
  344. msgstr[0] "Hay %s fichero"
  345. msgstr[1] "Hay %s ficheros"
  346. '''
  347. # Here's the second example po file example, used to generate the UMO_DATA
  348. # containing utf-8 encoded Unicode strings
  349. '''
  350. # Dummy translation for the Python test_gettext.py module.
  351. # Copyright (C) 2001 Python Software Foundation
  352. # Barry Warsaw <barry@python.org>, 2000.
  353. #
  354. msgid ""
  355. msgstr ""
  356. "Project-Id-Version: 2.0\n"
  357. "PO-Revision-Date: 2003-04-11 12:42-0400\n"
  358. "Last-Translator: Barry A. WArsaw <barry@python.org>\n"
  359. "Language-Team: XX <python-dev@python.org>\n"
  360. "MIME-Version: 1.0\n"
  361. "Content-Type: text/plain; charset=utf-8\n"
  362. "Content-Transfer-Encoding: 7bit\n"
  363. "Generated-By: manually\n"
  364. #: nofile:0
  365. msgid "ab\xc3\x9e"
  366. msgstr "\xc2\xa4yz"
  367. '''
  368. # Here's the third example po file, used to generate MMO_DATA
  369. '''
  370. msgid ""
  371. msgstr ""
  372. "Project-Id-Version: No Project 0.0\n"
  373. "POT-Creation-Date: Wed Dec 11 07:44:15 2002\n"
  374. "PO-Revision-Date: 2002-08-14 01:18:58+00:00\n"
  375. "Last-Translator: John Doe <jdoe@example.com>\n"
  376. "Jane Foobar <jfoobar@example.com>\n"
  377. "Language-Team: xx <xx@example.com>\n"
  378. "MIME-Version: 1.0\n"
  379. "Content-Type: text/plain; charset=iso-8859-15\n"
  380. "Content-Transfer-Encoding: quoted-printable\n"
  381. "Generated-By: pygettext.py 1.3\n"
  382. '''