diff --git a/composer.json b/composer.json index 0e6ee17d..1f53e546 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "symfony/http-foundation": "^3.3", "symfony/psr-http-message-bridge": "^1.0", "twbs/bootstrap": "^3.3", + "twig/extensions": "^1.5", "twig/twig": "^2.5", "zendframework/zend-diactoros": "^1.7" }, diff --git a/config/app.php b/config/app.php index 19118f1d..e309abe4 100644 --- a/config/app.php +++ b/config/app.php @@ -10,13 +10,13 @@ return [ \Engelsystem\Config\ConfigServiceProvider::class, \Engelsystem\Http\UrlGeneratorServiceProvider::class, \Engelsystem\Renderer\RendererServiceProvider::class, - \Engelsystem\Renderer\TwigServiceProvider::class, \Engelsystem\Database\DatabaseServiceProvider::class, \Engelsystem\Http\RequestServiceProvider::class, \Engelsystem\Http\SessionServiceProvider::class, \Engelsystem\Helpers\TranslationServiceProvider::class, \Engelsystem\Http\ResponseServiceProvider::class, \Engelsystem\Http\Psr7ServiceProvider::class, + \Engelsystem\Renderer\TwigServiceProvider::class, \Engelsystem\Middleware\RouteDispatcherServiceProvider::class, \Engelsystem\Middleware\RequestHandlerServiceProvider::class, ], diff --git a/src/Renderer/Twig/Extensions/Translation.php b/src/Renderer/Twig/Extensions/Translation.php new file mode 100644 index 00000000..63f9800e --- /dev/null +++ b/src/Renderer/Twig/Extensions/Translation.php @@ -0,0 +1,57 @@ +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]; + } +} diff --git a/src/Renderer/TwigServiceProvider.php b/src/Renderer/TwigServiceProvider.php index 4a5b1de3..0f453989 100644 --- a/src/Renderer/TwigServiceProvider.php +++ b/src/Renderer/TwigServiceProvider.php @@ -6,6 +6,7 @@ use Engelsystem\Container\ServiceProvider; use Engelsystem\Renderer\Twig\Extensions\Config; use Engelsystem\Renderer\Twig\Extensions\Globals; use Engelsystem\Renderer\Twig\Extensions\Session; +use Engelsystem\Renderer\Twig\Extensions\Translation; use Engelsystem\Renderer\Twig\Extensions\Url; use Twig_Environment as Twig; use Twig_LoaderInterface as TwigLoaderInterface; @@ -14,10 +15,11 @@ class TwigServiceProvider extends ServiceProvider { /** @var array */ protected $extensions = [ - 'config' => Config::class, - 'globals' => Globals::class, - 'session' => Session::class, - 'url' => Url::class, + 'config' => Config::class, + 'globals' => Globals::class, + 'session' => Session::class, + 'url' => Url::class, + 'translation' => Translation::class, ]; public function register() diff --git a/templates/errors/default.twig b/templates/errors/default.twig index a04afc4e..5fb8bcbd 100644 --- a/templates/errors/default.twig +++ b/templates/errors/default.twig @@ -1,6 +1,6 @@ {% extends "layouts/app.twig" %} -{% block title %}Error {{ status }}{% endblock %} +{% block title %}{% if status == 404 %}{{ __("Page not found") }}{% else %}Error {{ status }}{% endif %}{% endblock %} {% block content %}