vendor/jms/translation-bundle/Twig/TranslationExtension.php line 67

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. namespace JMS\TranslationBundle\Twig;
  19. /**
  20. * Provides some extensions for specifying translation metadata.
  21. *
  22. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  23. */
  24. use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
  25. use Symfony\Contracts\Translation\TranslatorInterface;
  26. use Twig\Extension\AbstractExtension;
  27. use Twig\TwigFilter;
  28. class TranslationExtension extends AbstractExtension
  29. {
  30. /**
  31. * @var TranslatorInterface|LegacyTranslatorInterface
  32. */
  33. private $translator;
  34. /**
  35. * @var bool
  36. */
  37. private $debug;
  38. /**
  39. * @param TranslatorInterface|LegacyTranslatorInterface $translator
  40. * @param bool $debug
  41. */
  42. public function __construct($translator, $debug = false)
  43. {
  44. if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
  45. throw new \InvalidArgumentException(sprintf(
  46. 'Argument 1 must be an instance of %s or %s, instance of %s given',
  47. TranslatorInterface::class,
  48. LegacyTranslatorInterface::class,
  49. get_class($translator)
  50. ));
  51. }
  52. $this->translator = $translator;
  53. $this->debug = $debug;
  54. }
  55. /**
  56. * @return array
  57. */
  58. public function getNodeVisitors()
  59. {
  60. $visitors = [
  61. new NormalizingNodeVisitor(),
  62. new RemovingNodeVisitor(),
  63. ];
  64. if ($this->debug) {
  65. $visitors[] = new DefaultApplyingNodeVisitor();
  66. }
  67. return $visitors;
  68. }
  69. /**
  70. * @return array
  71. */
  72. public function getFilters()
  73. {
  74. return [
  75. new TwigFilter('desc', [$this, 'desc']),
  76. new TwigFilter('meaning', [$this, 'meaning']),
  77. ];
  78. }
  79. /**
  80. * @param string $message
  81. * @param string $defaultMessage
  82. * @param int $count
  83. * @param array $arguments
  84. * @param string|null $domain
  85. * @param string|null $locale
  86. *
  87. * @return string
  88. */
  89. public function transchoiceWithDefault($message, $defaultMessage, $count, array $arguments = [], $domain = null, $locale = null)
  90. {
  91. if (null === $domain) {
  92. $domain = 'messages';
  93. }
  94. if (false === $this->translator->getCatalogue($locale)->defines($message, $domain)) {
  95. return $this->doTransChoice($defaultMessage, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale);
  96. }
  97. return $this->doTransChoice($message, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale);
  98. }
  99. private function doTransChoice($message, $count, array $arguments, $domain, $locale)
  100. {
  101. if ($this->translator instanceof LegacyTranslatorInterface) {
  102. return $this->translator->transChoice($message, $count, array_merge(['%count%' => $count], $arguments), $domain, $locale);
  103. }
  104. return $this->translator->trans($message, array_merge(['%count%' => $count], $arguments), $domain, $locale);
  105. }
  106. /**
  107. * @param mixed $v
  108. *
  109. * @return mixed
  110. */
  111. public function desc($v)
  112. {
  113. return $v;
  114. }
  115. /**
  116. * @param mixed $v
  117. *
  118. * @return mixed
  119. */
  120. public function meaning($v)
  121. {
  122. return $v;
  123. }
  124. /**
  125. * @return string
  126. */
  127. public function getName()
  128. {
  129. return 'jms_translation';
  130. }
  131. }