BackendTwigExtension.php 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Lc\ShopBundle\Twig;
  3. use Symfony\Contracts\Translation\TranslatorInterface;
  4. use Twig\Extension\AbstractExtension;
  5. use Twig\TwigFilter;
  6. use Twig\TwigFunction;
  7. class BackendTwigExtension extends AbstractExtension
  8. {
  9. public $trans;
  10. public function __construct(TranslatorInterface $translator)
  11. {
  12. $this->trans = $translator;
  13. }
  14. public function getFunctions()
  15. {
  16. return array(
  17. );
  18. }
  19. public function getFilters()
  20. {
  21. return [
  22. new TwigFilter('lc_trad', [$this, 'lcTrad'])
  23. ];
  24. }
  25. public function lcTrad($field, $entityName, $type="field")
  26. {
  27. $tradKey = $type.'.'.$entityName.'.'.$field;
  28. $tradDefaultKey = $type.'.default.'.$field;
  29. $trad = $this->trans->trans($tradKey, array(), 'lcshop');
  30. if($trad == $tradKey){
  31. $trad = $this->trans->trans($tradDefaultKey, array(), 'lcshop');
  32. }
  33. return $trad;
  34. }
  35. }