mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-12 01:54:41 +08:00
Updated system to use plates instead of twig
This commit is contained in:
@@ -9,7 +9,7 @@ use Shlinkio\Shlink\Common\Image;
|
||||
use Shlinkio\Shlink\Common\Image\ImageBuilder;
|
||||
use Shlinkio\Shlink\Common\Middleware\LocaleMiddleware;
|
||||
use Shlinkio\Shlink\Common\Service;
|
||||
use Shlinkio\Shlink\Common\Twig\Extension\TranslatorExtension;
|
||||
use Shlinkio\Shlink\Common\Template\Extension\TranslatorExtension;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Zend\I18n\Translator\Translator;
|
||||
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
<?php
|
||||
use Shlinkio\Shlink\Common\Twig\Extension\TranslatorExtension;
|
||||
declare(strict_types=1);
|
||||
|
||||
use Shlinkio\Shlink\Common\Template\Extension\TranslatorExtension;
|
||||
|
||||
return [
|
||||
|
||||
'twig' => [
|
||||
'templates' => [
|
||||
'extensions' => [
|
||||
TranslatorExtension::class,
|
||||
],
|
||||
|
||||
25
module/Common/src/Template/Extension/TranslatorExtension.php
Normal file
25
module/Common/src/Template/Extension/TranslatorExtension.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Common\Template\Extension;
|
||||
|
||||
use League\Plates\Engine;
|
||||
use League\Plates\Extension\ExtensionInterface;
|
||||
use Zend\I18n\Translator\TranslatorInterface;
|
||||
|
||||
class TranslatorExtension implements ExtensionInterface
|
||||
{
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
private $translator;
|
||||
|
||||
public function __construct(TranslatorInterface $translator)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function register(Engine $engine)
|
||||
{
|
||||
$engine->registerFunction('translate', [$this->translator, 'translate']);
|
||||
$engine->registerFunction('translate_plural', [$this->translator, 'translatePlural']);
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Common\Twig\Extension;
|
||||
|
||||
use Zend\I18n\Translator\TranslatorInterface;
|
||||
|
||||
class TranslatorExtension extends \Twig_Extension implements TranslatorInterface
|
||||
{
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
private $translator;
|
||||
|
||||
public function __construct(TranslatorInterface $translator)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the extension.
|
||||
*
|
||||
* @return string The extension name
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return __CLASS__;
|
||||
}
|
||||
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new \Twig_SimpleFunction('translate', [$this, 'translate']),
|
||||
new \Twig_SimpleFunction('translate_plural', [$this, 'translatePlural']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a message.
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $textDomain
|
||||
* @param string $locale
|
||||
* @return string
|
||||
*/
|
||||
public function translate($message, $textDomain = 'default', $locale = null)
|
||||
{
|
||||
return $this->translator->translate($message, $textDomain, $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a plural message.
|
||||
*
|
||||
* @param string $singular
|
||||
* @param string $plural
|
||||
* @param int $number
|
||||
* @param string $textDomain
|
||||
* @param string|null $locale
|
||||
* @return string
|
||||
*/
|
||||
public function translatePlural(
|
||||
$singular,
|
||||
$plural,
|
||||
$number,
|
||||
$textDomain = 'default',
|
||||
$locale = null
|
||||
) {
|
||||
$this->translator->translatePlural($singular, $plural, $number, $textDomain, $locale);
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,6 @@ class ConfigProviderTest extends TestCase
|
||||
$config = $this->configProvider->__invoke();
|
||||
|
||||
$this->assertArrayHasKey('dependencies', $config);
|
||||
$this->assertArrayHasKey('twig', $config);
|
||||
$this->assertArrayHasKey('templates', $config);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
namespace ShlinkioTest\Shlink\Common\Template\Extension;
|
||||
|
||||
use League\Plates\Engine;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Shlinkio\Shlink\Common\Template\Extension\TranslatorExtension;
|
||||
use Zend\I18n\Translator\Translator;
|
||||
|
||||
class TranslatorExtensionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var TranslatorExtension
|
||||
*/
|
||||
protected $extension;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->extension = new TranslatorExtension($this->prophesize(Translator::class)->reveal());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function properFunctionsAreReturned()
|
||||
{
|
||||
$engine = $this->prophesize(Engine::class);
|
||||
|
||||
$engine->registerFunction('translate', Argument::type('callable'))->shouldBeCalledTimes(1);
|
||||
$engine->registerFunction('translate_plural', Argument::type('callable'))->shouldBeCalledTimes(1);
|
||||
|
||||
$funcs = $this->extension->register($engine->reveal());
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
<?php
|
||||
namespace ShlinkioTest\Shlink\Common\Twig\Extension;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\Common\Twig\Extension\TranslatorExtension;
|
||||
use Zend\I18n\Translator\Translator;
|
||||
|
||||
class TranslatorExtensionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var TranslatorExtension
|
||||
*/
|
||||
protected $extension;
|
||||
/**
|
||||
* @var ObjectProphecy
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->translator = $this->prophesize(Translator::class);
|
||||
$this->extension = new TranslatorExtension($this->translator->reveal());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function extensionNameIsClassName()
|
||||
{
|
||||
$this->assertEquals(TranslatorExtension::class, $this->extension->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function properFunctionsAreReturned()
|
||||
{
|
||||
$funcs = $this->extension->getFunctions();
|
||||
$this->assertCount(2, $funcs);
|
||||
foreach ($funcs as $func) {
|
||||
$this->assertInstanceOf(\Twig_SimpleFunction::class, $func);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function translateFallbacksToTranslator()
|
||||
{
|
||||
$this->translator->translate('foo', 'default', null)->shouldBeCalledTimes(1);
|
||||
$this->extension->translate('foo');
|
||||
|
||||
$this->translator->translate('bar', 'baz', 'en')->shouldBeCalledTimes(1);
|
||||
$this->extension->translate('bar', 'baz', 'en');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function translatePluralFallbacksToTranslator()
|
||||
{
|
||||
$this->translator->translatePlural('foo', 'bar', 'baz', 'default', null)->shouldBeCalledTimes(1);
|
||||
$this->extension->translatePlural('foo', 'bar', 'baz');
|
||||
|
||||
$this->translator->translatePlural('foo', 'bar', 'baz', 'another', 'en')->shouldBeCalledTimes(1);
|
||||
$this->extension->translatePlural('foo', 'bar', 'baz', 'another', 'en');
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ return [
|
||||
|
||||
'templates' => [
|
||||
'paths' => [
|
||||
'module/Core/templates',
|
||||
'ShlinkCore' => __DIR__ . '/../templates',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ return [
|
||||
|
||||
'zend-expressive' => [
|
||||
'error_handler' => [
|
||||
'template_404' => 'core/error/404.html.twig',
|
||||
'template_error' => 'core/error/error.html.twig',
|
||||
'template_404' => 'ShlinkCore::error/404',
|
||||
'template_error' => 'ShlinkCore::error/error',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
Reference in New Issue
Block a user