twig_theme_test.trans.html.twig 2 KB
Newer Older
1 2 3 4 5
{# Test trans tag with string argument. #}
<div>
  {% trans 'Hello sun.' %}
</div>

6 7 8 9 10
{# Test trans tag with string argument and context #}
<div>
  {% trans 'Hello sun.' with {'context': 'Lolspeak'} %}
</div>

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
{# Test trans filter. #}
<div>
  {{ 'Hello Earth.'|trans }}
</div>

{# Test trans tag with text content. #}
<div>
  {% trans %}
    Hello moon.
  {% endtrans %}
</div>

{# Test trans/plural tag where count = 1. #}
<div>
  {% set count = 1 %}
  {% trans %}
    Hello star.
  {% plural count %}
    Hello {{ count }} stars.
  {% endtrans %}
</div>

{# Test trans/plural tag where count = 2. #}
<div>
  {% set count = 2 %}
  {% trans %}
    Hello star.
  {% plural count %}
    Hello {{ count }} stars.
  {% endtrans %}
</div>

{# Test trans tag with different filters applied to tokens. #}
{% set string = '&"<>' %}
<div>
  {% trans %}
    Escaped: {{ string }}
  {% endtrans %}
</div>
<div>
  {% trans %}
    Placeholder: {{ string|placeholder }}
  {% endtrans %}
</div>

{# Test trans tag with complex tokens. #}
{% set token = {'name': 'complex token', 'numbers': '12345', 'bad_text': '&"<>' } %}
{% set count = token|length %}
<div>
  {% trans %}
61
    This {{ token.name }} has a length of: {{ count }}. It contains: {{ token.numbers|placeholder }} and {{ token.bad_text }}.
62 63
  {% endtrans %}
</div>
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

{# Test trans tag but with a context only msgid. #}
<div>
  {% trans %}
    I have context.
  {% endtrans %}
</div>

{# Test trans tag with context. #}
<div>
  {% trans with {'context': 'Lolspeak'} %}
    I have context.
  {% endtrans %}
</div>

{# Test trans tag with a specified language. #}
<div>
  {% trans with {'langcode': 'zz'} %}
    Hello new text.
  {% endtrans %}
</div>

{# Test trans tag with context and a specified language. #}
<div>
  {% trans with {'context': 'Lolspeak', 'langcode': 'zz'} %}
    Hello new text.
  {% endtrans %}
</div>
92 93 94 95 96 97 98

{# Tests that https://www.drupal.org/node/2489024 doesn't happen without twig debug. #}
<div>
  {% trans %}
    Number I never remember: ' . print(pi()) . '
  {% endtrans %}
</div>