summaryrefslogtreecommitdiff
path: root/src/Middleware
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-08-28 22:23:59 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-08-29 23:46:31 +0200
commit427315195bdd379a0207fc9b2aaf69a5b5b86c79 (patch)
tree678245351333a16c5a5bca129aada4bfd311c23c /src/Middleware
parentdf6360044b5c2396b2bee0dfa9e8d744bfa424d5 (diff)
Moved translation/internationalization to Helpers\Translator class
Diffstat (limited to 'src/Middleware')
-rw-r--r--src/Middleware/SetLocale.php49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/Middleware/SetLocale.php b/src/Middleware/SetLocale.php
new file mode 100644
index 00000000..86fa0b7f
--- /dev/null
+++ b/src/Middleware/SetLocale.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace Engelsystem\Middleware;
+
+use Engelsystem\Helpers\Translator;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+use Symfony\Component\HttpFoundation\Session\Session;
+
+class SetLocale implements MiddlewareInterface
+{
+ /** @var Translator */
+ protected $translator;
+
+ /** @var Session */
+ protected $session;
+
+ /**
+ * @param Translator $translator
+ * @param Session $session
+ */
+ public function __construct(Translator $translator, Session $session)
+ {
+ $this->translator = $translator;
+ $this->session = $session;
+ }
+
+ /**
+ * Process an incoming server request and setting the locale if required
+ *
+ * @param ServerRequestInterface $request
+ * @param RequestHandlerInterface $handler
+ * @return ResponseInterface
+ */
+ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
+ {
+ $query = $request->getQueryParams();
+ if (isset($query['set-locale']) && $this->translator->hasLocale($query['set-locale'])) {
+ $locale = $query['set-locale'];
+
+ $this->translator->setLocale($locale);
+ $this->session->set('locale', $locale);
+ }
+
+ return $handler->handle($request);
+ }
+}