summaryrefslogtreecommitdiff
path: root/src/Middleware
diff options
context:
space:
mode:
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);
+ }
+}