TwigNodeTrans::compile

public TwigNodeTrans::compile(\Twig_Compiler $compiler)

Overrides Twig_Node::compile

File

core/lib/Drupal/Core/Template/TwigNodeTrans.php, line 32

Class

TwigNodeTrans
A class that defines the Twig 'trans' tag for Drupal.

Namespace

Drupal\Core\Template

Code

1
2
3
4
5
6
7
8
9
10
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
public function compile(\Twig_Compiler $compiler) {
  $compiler->addDebugInfo($this);
 
  $options = $this->getNode('options');
 
  list($singular, $tokens) = $this->compileString($this->getNode('body'));
  $plural = NULL;
 
  if (NULL !== $this->getNode('plural')) {
    list($plural, $pluralTokens) = $this->compileString($this->getNode('plural'));
    $tokens = array_merge($tokens, $pluralTokens);
  }
 
  // Start writing with the function to be called.
  $compiler->write('echo ' . (empty($plural) ? 't' : '\Drupal::translation()->formatPlural') . '(');
 
  // Move the count to the beginning of the parameters list.
  if (!empty($plural)) {
    $compiler->raw('abs(')->subcompile($this->getNode('count'))->raw('), ');
  }
 
  // Write the singular text parameter.
  $compiler->subcompile($singular);
 
  // Write the plural text parameter, if necessary.
  if (!empty($plural)) {
    $compiler->raw(', ')->subcompile($plural);
  }
 
  // Write any tokens found as an associative array parameter, otherwise just
  // leave as an empty array.
  $compiler->raw(', array(');
  foreach ($tokens as $token) {
    $compiler->string($token->getAttribute('placeholder'))->raw(' => ')->subcompile($token)->raw(', ');
  }
  $compiler->raw(')');
 
  // Write any options passed.
  if (!empty($options)) {
    $compiler->raw(', ')->subcompile($options);
  }
 
  // Write function closure.
  $compiler->raw(')');
 
  // @todo Add debug output, see https://www.drupal.org/node/2512672
 
  // End writing.
  $compiler->raw(";\n");
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.