Added translation support for twig templates
See https://twig-extensions.readthedocs.io/en/latest/i18n.html for documentationmain
parent
a2c47304d8
commit
8257864829
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Renderer\Twig\Extensions;
|
||||
|
||||
use Engelsystem\Helpers\Translator;
|
||||
use Twig_Extension as TwigExtension;
|
||||
use Twig_Extensions_TokenParser_Trans as TranslationTokenParser;
|
||||
use Twig_Filter as TwigFilter;
|
||||
use Twig_Function as TwigFunction;
|
||||
use Twig_TokenParserInterface as TwigTokenParser;
|
||||
|
||||
class Translation extends TwigExtension
|
||||
{
|
||||
/** @var Translator */
|
||||
protected $translator;
|
||||
|
||||
/** @var TranslationTokenParser */
|
||||
protected $tokenParser;
|
||||
|
||||
/**
|
||||
* @param Translator $translator
|
||||
* @param TranslationTokenParser $tokenParser
|
||||
*/
|
||||
public function __construct(Translator $translator, TranslationTokenParser $tokenParser)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
$this->tokenParser = $tokenParser;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('trans', [$this->translator, 'translate']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TwigFunction[]
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('__', [$this->translator, 'translate']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TwigTokenParser[]
|
||||
*/
|
||||
public function getTokenParsers()
|
||||
{
|
||||
return [$this->tokenParser];
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
|
||||
|
||||
use Engelsystem\Helpers\Translator;
|
||||
use Engelsystem\Renderer\Twig\Extensions\Translation;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Twig_Extensions_TokenParser_Trans as TranslationTokenParser;
|
||||
|
||||
class TranslationTest extends ExtensionTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Renderer\Twig\Extensions\Translation::__construct
|
||||
* @covers \Engelsystem\Renderer\Twig\Extensions\Translation::getFilters
|
||||
*/
|
||||
public function testGeFilters()
|
||||
{
|
||||
/** @var Translator|MockObject $translator */
|
||||
$translator = $this->createMock(Translator::class);
|
||||
/** @var TranslationTokenParser|MockObject $parser */
|
||||
$parser = $this->createMock(TranslationTokenParser::class);
|
||||
|
||||
$extension = new Translation($translator, $parser);
|
||||
$filters = $extension->getFilters();
|
||||
|
||||
$this->assertExtensionExists('trans', [$translator, 'translate'], $filters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Renderer\Twig\Extensions\Translation::getFunctions
|
||||
*/
|
||||
public function testGetFunctions()
|
||||
{
|
||||
/** @var Translator|MockObject $translator */
|
||||
$translator = $this->createMock(Translator::class);
|
||||
/** @var TranslationTokenParser|MockObject $parser */
|
||||
$parser = $this->createMock(TranslationTokenParser::class);
|
||||
|
||||
$extension = new Translation($translator, $parser);
|
||||
$functions = $extension->getFunctions();
|
||||
|
||||
$this->assertExtensionExists('__', [$translator, 'translate'], $functions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Renderer\Twig\Extensions\Translation::getTokenParsers
|
||||
*/
|
||||
public function testGetTokenParsers()
|
||||
{
|
||||
/** @var Translator|MockObject $translator */
|
||||
$translator = $this->createMock(Translator::class);
|
||||
/** @var TranslationTokenParser|MockObject $parser */
|
||||
$parser = $this->createMock(TranslationTokenParser::class);
|
||||
|
||||
$extension = new Translation($translator, $parser);
|
||||
$tokenParsers = $extension->getTokenParsers();
|
||||
|
||||
$this->assertTokenParserExists($parser, $tokenParsers);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue