test_i18n.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from django.template import TemplateSyntaxError
  4. from django.test import SimpleTestCase
  5. from django.utils import translation
  6. from django.utils.safestring import mark_safe
  7. from ..utils import setup
  8. class I18nTagTests(SimpleTestCase):
  9. libraries = {
  10. 'custom': 'template_tests.templatetags.custom',
  11. 'i18n': 'django.templatetags.i18n',
  12. }
  13. @setup({'i18n01': '{% load i18n %}{% trans \'xxxyyyxxx\' %}'})
  14. def test_i18n01(self):
  15. """
  16. simple translation of a string delimited by '
  17. """
  18. output = self.engine.render_to_string('i18n01')
  19. self.assertEqual(output, 'xxxyyyxxx')
  20. @setup({'i18n02': '{% load i18n %}{% trans "xxxyyyxxx" %}'})
  21. def test_i18n02(self):
  22. """
  23. simple translation of a string delimited by "
  24. """
  25. output = self.engine.render_to_string('i18n02')
  26. self.assertEqual(output, 'xxxyyyxxx')
  27. @setup({'i18n03': '{% load i18n %}{% blocktrans %}{{ anton }}{% endblocktrans %}'})
  28. def test_i18n03(self):
  29. """
  30. simple translation of a variable
  31. """
  32. output = self.engine.render_to_string('i18n03', {'anton': b'\xc3\x85'})
  33. self.assertEqual(output, 'Å')
  34. @setup({'i18n04': '{% load i18n %}{% blocktrans with berta=anton|lower %}{{ berta }}{% endblocktrans %}'})
  35. def test_i18n04(self):
  36. """
  37. simple translation of a variable and filter
  38. """
  39. output = self.engine.render_to_string('i18n04', {'anton': b'\xc3\x85'})
  40. self.assertEqual(output, 'å')
  41. @setup({'legacyi18n04': '{% load i18n %}'
  42. '{% blocktrans with anton|lower as berta %}{{ berta }}{% endblocktrans %}'})
  43. def test_legacyi18n04(self):
  44. """
  45. simple translation of a variable and filter
  46. """
  47. output = self.engine.render_to_string('legacyi18n04', {'anton': b'\xc3\x85'})
  48. self.assertEqual(output, 'å')
  49. @setup({'i18n05': '{% load i18n %}{% blocktrans %}xxx{{ anton }}xxx{% endblocktrans %}'})
  50. def test_i18n05(self):
  51. """
  52. simple translation of a string with interpolation
  53. """
  54. output = self.engine.render_to_string('i18n05', {'anton': 'yyy'})
  55. self.assertEqual(output, 'xxxyyyxxx')
  56. @setup({'i18n06': '{% load i18n %}{% trans "Page not found" %}'})
  57. def test_i18n06(self):
  58. """
  59. simple translation of a string to german
  60. """
  61. with translation.override('de'):
  62. output = self.engine.render_to_string('i18n06')
  63. self.assertEqual(output, 'Seite nicht gefunden')
  64. @setup({'i18n07': '{% load i18n %}'
  65. '{% blocktrans count counter=number %}singular{% plural %}'
  66. '{{ counter }} plural{% endblocktrans %}'})
  67. def test_i18n07(self):
  68. """
  69. translation of singular form
  70. """
  71. output = self.engine.render_to_string('i18n07', {'number': 1})
  72. self.assertEqual(output, 'singular')
  73. @setup({'legacyi18n07': '{% load i18n %}'
  74. '{% blocktrans count number as counter %}singular{% plural %}'
  75. '{{ counter }} plural{% endblocktrans %}'})
  76. def test_legacyi18n07(self):
  77. """
  78. translation of singular form
  79. """
  80. output = self.engine.render_to_string('legacyi18n07', {'number': 1})
  81. self.assertEqual(output, 'singular')
  82. @setup({'i18n08': '{% load i18n %}'
  83. '{% blocktrans count number as counter %}singular{% plural %}'
  84. '{{ counter }} plural{% endblocktrans %}'})
  85. def test_i18n08(self):
  86. """
  87. translation of plural form
  88. """
  89. output = self.engine.render_to_string('i18n08', {'number': 2})
  90. self.assertEqual(output, '2 plural')
  91. @setup({'legacyi18n08': '{% load i18n %}'
  92. '{% blocktrans count counter=number %}singular{% plural %}'
  93. '{{ counter }} plural{% endblocktrans %}'})
  94. def test_legacyi18n08(self):
  95. """
  96. translation of plural form
  97. """
  98. output = self.engine.render_to_string('legacyi18n08', {'number': 2})
  99. self.assertEqual(output, '2 plural')
  100. @setup({'i18n09': '{% load i18n %}{% trans "Page not found" noop %}'})
  101. def test_i18n09(self):
  102. """
  103. simple non-translation (only marking) of a string to german
  104. """
  105. with translation.override('de'):
  106. output = self.engine.render_to_string('i18n09')
  107. self.assertEqual(output, 'Page not found')
  108. @setup({'i18n10': '{{ bool|yesno:_("yes,no,maybe") }}'})
  109. def test_i18n10(self):
  110. """
  111. translation of a variable with a translated filter
  112. """
  113. with translation.override('de'):
  114. output = self.engine.render_to_string('i18n10', {'bool': True})
  115. self.assertEqual(output, 'Ja')
  116. @setup({'i18n11': '{{ bool|yesno:"ja,nein" }}'})
  117. def test_i18n11(self):
  118. """
  119. translation of a variable with a non-translated filter
  120. """
  121. output = self.engine.render_to_string('i18n11', {'bool': True})
  122. self.assertEqual(output, 'ja')
  123. @setup({'i18n12': '{% load i18n %}'
  124. '{% get_available_languages as langs %}{% for lang in langs %}'
  125. '{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}'})
  126. def test_i18n12(self):
  127. """
  128. usage of the get_available_languages tag
  129. """
  130. output = self.engine.render_to_string('i18n12')
  131. self.assertEqual(output, 'de')
  132. @setup({'i18n13': '{{ _("Password") }}'})
  133. def test_i18n13(self):
  134. """
  135. translation of constant strings
  136. """
  137. with translation.override('de'):
  138. output = self.engine.render_to_string('i18n13')
  139. self.assertEqual(output, 'Passwort')
  140. @setup({'i18n14': '{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}'})
  141. def test_i18n14(self):
  142. """
  143. translation of constant strings
  144. """
  145. with translation.override('de'):
  146. output = self.engine.render_to_string('i18n14')
  147. self.assertEqual(output, 'foo Passwort Passwort')
  148. @setup({'i18n15': '{{ absent|default:_("Password") }}'})
  149. def test_i18n15(self):
  150. """
  151. translation of constant strings
  152. """
  153. with translation.override('de'):
  154. output = self.engine.render_to_string('i18n15', {'absent': ''})
  155. self.assertEqual(output, 'Passwort')
  156. @setup({'i18n16': '{{ _("<") }}'})
  157. def test_i18n16(self):
  158. """
  159. translation of constant strings
  160. """
  161. with translation.override('de'):
  162. output = self.engine.render_to_string('i18n16')
  163. self.assertEqual(output, '<')
  164. @setup({'i18n17': '{% load i18n %}'
  165. '{% blocktrans with berta=anton|escape %}{{ berta }}{% endblocktrans %}'})
  166. def test_i18n17(self):
  167. """
  168. Escaping inside blocktrans and trans works as if it was directly in the template.
  169. """
  170. output = self.engine.render_to_string('i18n17', {'anton': 'α & β'})
  171. self.assertEqual(output, 'α &amp; β')
  172. @setup({'i18n18': '{% load i18n %}'
  173. '{% blocktrans with berta=anton|force_escape %}{{ berta }}{% endblocktrans %}'})
  174. def test_i18n18(self):
  175. output = self.engine.render_to_string('i18n18', {'anton': 'α & β'})
  176. self.assertEqual(output, 'α &amp; β')
  177. @setup({'i18n19': '{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}'})
  178. def test_i18n19(self):
  179. output = self.engine.render_to_string('i18n19', {'andrew': 'a & b'})
  180. self.assertEqual(output, 'a &amp; b')
  181. @setup({'i18n20': '{% load i18n %}{% trans andrew %}'})
  182. def test_i18n20(self):
  183. output = self.engine.render_to_string('i18n20', {'andrew': 'a & b'})
  184. self.assertEqual(output, 'a &amp; b')
  185. @setup({'i18n21': '{% load i18n %}{% blocktrans %}{{ andrew }}{% endblocktrans %}'})
  186. def test_i18n21(self):
  187. output = self.engine.render_to_string('i18n21', {'andrew': mark_safe('a & b')})
  188. self.assertEqual(output, 'a & b')
  189. @setup({'i18n22': '{% load i18n %}{% trans andrew %}'})
  190. def test_i18n22(self):
  191. output = self.engine.render_to_string('i18n22', {'andrew': mark_safe('a & b')})
  192. self.assertEqual(output, 'a & b')
  193. @setup({'legacyi18n17': '{% load i18n %}'
  194. '{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}'})
  195. def test_legacyi18n17(self):
  196. output = self.engine.render_to_string('legacyi18n17', {'anton': 'α & β'})
  197. self.assertEqual(output, 'α &amp; β')
  198. @setup({'legacyi18n18': '{% load i18n %}'
  199. '{% blocktrans with anton|force_escape as berta %}'
  200. '{{ berta }}{% endblocktrans %}'})
  201. def test_legacyi18n18(self):
  202. output = self.engine.render_to_string('legacyi18n18', {'anton': 'α & β'})
  203. self.assertEqual(output, 'α &amp; β')
  204. @setup({'i18n23': '{% load i18n %}{% trans "Page not found"|capfirst|slice:"6:" %}'})
  205. def test_i18n23(self):
  206. """
  207. #5972 - Use filters with the {% trans %} tag
  208. """
  209. with translation.override('de'):
  210. output = self.engine.render_to_string('i18n23')
  211. self.assertEqual(output, 'nicht gefunden')
  212. @setup({'i18n24': '{% load i18n %}{% trans \'Page not found\'|upper %}'})
  213. def test_i18n24(self):
  214. with translation.override('de'):
  215. output = self.engine.render_to_string('i18n24')
  216. self.assertEqual(output, 'SEITE NICHT GEFUNDEN')
  217. @setup({'i18n25': '{% load i18n %}{% trans somevar|upper %}'})
  218. def test_i18n25(self):
  219. with translation.override('de'):
  220. output = self.engine.render_to_string('i18n25', {'somevar': 'Page not found'})
  221. self.assertEqual(output, 'SEITE NICHT GEFUNDEN')
  222. @setup({'i18n26': '{% load i18n %}'
  223. '{% blocktrans with extra_field=myextra_field count counter=number %}'
  224. 'singular {{ extra_field }}{% plural %}plural{% endblocktrans %}'})
  225. def test_i18n26(self):
  226. """
  227. translation of plural form with extra field in singular form (#13568)
  228. """
  229. output = self.engine.render_to_string('i18n26', {'myextra_field': 'test', 'number': 1})
  230. self.assertEqual(output, 'singular test')
  231. @setup({'legacyi18n26': '{% load i18n %}'
  232. '{% blocktrans with myextra_field as extra_field count number as counter %}'
  233. 'singular {{ extra_field }}{% plural %}plural{% endblocktrans %}'})
  234. def test_legacyi18n26(self):
  235. output = self.engine.render_to_string('legacyi18n26', {'myextra_field': 'test', 'number': 1})
  236. self.assertEqual(output, 'singular test')
  237. @setup({'i18n27': '{% load i18n %}{% blocktrans count counter=number %}'
  238. '{{ counter }} result{% plural %}{{ counter }} results'
  239. '{% endblocktrans %}'})
  240. def test_i18n27(self):
  241. """
  242. translation of singular form in russian (#14126)
  243. """
  244. with translation.override('ru'):
  245. output = self.engine.render_to_string('i18n27', {'number': 1})
  246. self.assertEqual(output, '1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442')
  247. @setup({'legacyi18n27': '{% load i18n %}'
  248. '{% blocktrans count number as counter %}{{ counter }} result'
  249. '{% plural %}{{ counter }} results{% endblocktrans %}'})
  250. def test_legacyi18n27(self):
  251. with translation.override('ru'):
  252. output = self.engine.render_to_string('legacyi18n27', {'number': 1})
  253. self.assertEqual(output, '1 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442')
  254. @setup({'i18n28': '{% load i18n %}'
  255. '{% blocktrans with a=anton b=berta %}{{ a }} + {{ b }}{% endblocktrans %}'})
  256. def test_i18n28(self):
  257. """
  258. simple translation of multiple variables
  259. """
  260. output = self.engine.render_to_string('i18n28', {'anton': 'α', 'berta': 'β'})
  261. self.assertEqual(output, 'α + β')
  262. @setup({'legacyi18n28': '{% load i18n %}'
  263. '{% blocktrans with anton as a and berta as b %}'
  264. '{{ a }} + {{ b }}{% endblocktrans %}'})
  265. def test_legacyi18n28(self):
  266. output = self.engine.render_to_string('legacyi18n28', {'anton': 'α', 'berta': 'β'})
  267. self.assertEqual(output, 'α + β')
  268. # retrieving language information
  269. @setup({'i18n28_2': '{% load i18n %}'
  270. '{% get_language_info for "de" as l %}'
  271. '{{ l.code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}'})
  272. def test_i18n28_2(self):
  273. output = self.engine.render_to_string('i18n28_2')
  274. self.assertEqual(output, 'de: German/Deutsch bidi=False')
  275. @setup({'i18n29': '{% load i18n %}'
  276. '{% get_language_info for LANGUAGE_CODE as l %}'
  277. '{{ l.code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}'})
  278. def test_i18n29(self):
  279. output = self.engine.render_to_string('i18n29', {'LANGUAGE_CODE': 'fi'})
  280. self.assertEqual(output, 'fi: Finnish/suomi bidi=False')
  281. @setup({'i18n30': '{% load i18n %}'
  282. '{% get_language_info_list for langcodes as langs %}'
  283. '{% for l in langs %}{{ l.code }}: {{ l.name }}/'
  284. '{{ l.name_local }} bidi={{ l.bidi }}; {% endfor %}'})
  285. def test_i18n30(self):
  286. output = self.engine.render_to_string('i18n30', {'langcodes': ['it', 'no']})
  287. self.assertEqual(output, 'it: Italian/italiano bidi=False; no: Norwegian/norsk bidi=False; ')
  288. @setup({'i18n31': '{% load i18n %}'
  289. '{% get_language_info_list for langcodes as langs %}'
  290. '{% for l in langs %}{{ l.code }}: {{ l.name }}/'
  291. '{{ l.name_local }} bidi={{ l.bidi }}; {% endfor %}'})
  292. def test_i18n31(self):
  293. output = self.engine.render_to_string('i18n31', {'langcodes': (('sl', 'Slovenian'), ('fa', 'Persian'))})
  294. self.assertEqual(
  295. output,
  296. 'sl: Slovenian/Sloven\u0161\u010dina bidi=False; '
  297. 'fa: Persian/\u0641\u0627\u0631\u0633\u06cc bidi=True; '
  298. )
  299. @setup({'i18n32': '{% load i18n %}{{ "hu"|language_name }} '
  300. '{{ "hu"|language_name_local }} {{ "hu"|language_bidi }}'})
  301. def test_i18n32(self):
  302. output = self.engine.render_to_string('i18n32')
  303. self.assertEqual(output, 'Hungarian Magyar False')
  304. @setup({'i18n33': '{% load i18n %}'
  305. '{{ langcode|language_name }} {{ langcode|language_name_local }} '
  306. '{{ langcode|language_bidi }}'})
  307. def test_i18n33(self):
  308. output = self.engine.render_to_string('i18n33', {'langcode': 'nl'})
  309. self.assertEqual(output, 'Dutch Nederlands False')
  310. # blocktrans handling of variables which are not in the context.
  311. # this should work as if blocktrans was not there (#19915)
  312. @setup({'i18n34': '{% load i18n %}{% blocktrans %}{{ missing }}{% endblocktrans %}'})
  313. def test_i18n34(self):
  314. output = self.engine.render_to_string('i18n34')
  315. if self.engine.string_if_invalid:
  316. self.assertEqual(output, 'INVALID')
  317. else:
  318. self.assertEqual(output, '')
  319. @setup({'i18n34_2': '{% load i18n %}{% blocktrans with a=\'α\' %}{{ missing }}{% endblocktrans %}'})
  320. def test_i18n34_2(self):
  321. output = self.engine.render_to_string('i18n34_2')
  322. if self.engine.string_if_invalid:
  323. self.assertEqual(output, 'INVALID')
  324. else:
  325. self.assertEqual(output, '')
  326. @setup({'i18n34_3': '{% load i18n %}{% blocktrans with a=anton %}{{ missing }}{% endblocktrans %}'})
  327. def test_i18n34_3(self):
  328. output = self.engine.render_to_string('i18n34_3', {'anton': '\xce\xb1'})
  329. if self.engine.string_if_invalid:
  330. self.assertEqual(output, 'INVALID')
  331. else:
  332. self.assertEqual(output, '')
  333. # trans tag with as var
  334. @setup({'i18n35': '{% load i18n %}{% trans "Page not found" as page_not_found %}{{ page_not_found }}'})
  335. def test_i18n35(self):
  336. with translation.override('de'):
  337. output = self.engine.render_to_string('i18n35')
  338. self.assertEqual(output, 'Seite nicht gefunden')
  339. @setup({'i18n36': '{% load i18n %}'
  340. '{% trans "Page not found" noop as page_not_found %}{{ page_not_found }}'})
  341. def test_i18n36(self):
  342. with translation.override('de'):
  343. output = self.engine.render_to_string('i18n36')
  344. self.assertEqual(output, 'Page not found')
  345. @setup({'i18n37': '{% load i18n %}'
  346. '{% trans "Page not found" as page_not_found %}'
  347. '{% blocktrans %}Error: {{ page_not_found }}{% endblocktrans %}'})
  348. def test_i18n37(self):
  349. with translation.override('de'):
  350. output = self.engine.render_to_string('i18n37')
  351. self.assertEqual(output, 'Error: Seite nicht gefunden')
  352. # Test whitespace in filter arguments
  353. @setup({'i18n38': '{% load i18n custom %}'
  354. '{% get_language_info for "de"|noop:"x y" as l %}'
  355. '{{ l.code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}'})
  356. def test_i18n38(self):
  357. output = self.engine.render_to_string('i18n38')
  358. self.assertEqual(output, 'de: German/Deutsch bidi=False')
  359. @setup({'i18n38_2': '{% load i18n custom %}'
  360. '{% get_language_info_list for langcodes|noop:"x y" as langs %}'
  361. '{% for l in langs %}{{ l.code }}: {{ l.name }}/'
  362. '{{ l.name_local }} bidi={{ l.bidi }}; {% endfor %}'})
  363. def test_i18n38_2(self):
  364. output = self.engine.render_to_string('i18n38_2', {'langcodes': ['it', 'no']})
  365. self.assertEqual(output, 'it: Italian/italiano bidi=False; no: Norwegian/norsk bidi=False; ')
  366. @setup({'template': '{% load i18n %}{% trans %}A}'})
  367. def test_syntax_error_no_arguments(self):
  368. msg = "'trans' takes at least one argument"
  369. with self.assertRaisesMessage(TemplateSyntaxError, msg):
  370. self.engine.render_to_string('template')
  371. @setup({'template': '{% load i18n %}{% trans "Yes" badoption %}'})
  372. def test_syntax_error_bad_option(self):
  373. msg = "Unknown argument for 'trans' tag: 'badoption'"
  374. with self.assertRaisesMessage(TemplateSyntaxError, msg):
  375. self.engine.render_to_string('template')
  376. @setup({'template': '{% load i18n %}{% trans "Yes" as %}'})
  377. def test_syntax_error_missing_assignment(self):
  378. msg = "No argument provided to the 'trans' tag for the as option."
  379. with self.assertRaisesMessage(TemplateSyntaxError, msg):
  380. self.engine.render_to_string('template')
  381. @setup({'template': '{% load i18n %}{% trans "Yes" as var context %}'})
  382. def test_syntax_error_missing_context(self):
  383. msg = "No argument provided to the 'trans' tag for the context option."
  384. with self.assertRaisesMessage(TemplateSyntaxError, msg):
  385. self.engine.render_to_string('template')
  386. @setup({'template': '{% load i18n %}{% trans "Yes" context as var %}'})
  387. def test_syntax_error_context_as(self):
  388. msg = "Invalid argument 'as' provided to the 'trans' tag for the context option"
  389. with self.assertRaisesMessage(TemplateSyntaxError, msg):
  390. self.engine.render_to_string('template')
  391. @setup({'template': '{% load i18n %}{% trans "Yes" context noop %}'})
  392. def test_syntax_error_context_noop(self):
  393. msg = "Invalid argument 'noop' provided to the 'trans' tag for the context option"
  394. with self.assertRaisesMessage(TemplateSyntaxError, msg):
  395. self.engine.render_to_string('template')
  396. @setup({'template': '{% load i18n %}{% trans "Yes" noop noop %}'})
  397. def test_syntax_error_duplicate_option(self):
  398. msg = "The 'noop' option was specified more than once."
  399. with self.assertRaisesMessage(TemplateSyntaxError, msg):
  400. self.engine.render_to_string('template')