summaryrefslogtreecommitdiff
path: root/src/Middleware/SetLocale.php
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-09-08 12:48:08 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-09-09 12:11:12 +0200
commit222c9fed7d4ca2b3b44c05907fbb7080c1efd342 (patch)
tree62b1bfa9233000985331b9dea703d51dc24515a2 /src/Middleware/SetLocale.php
parent2d6bca1357faff28bc1f86a56b432cc463ff7574 (diff)
parent8257864829ffdfb410f05e0dd0a9c781f48b741a (diff)
Merge remote-tracking branch 'MyIgel/templating'
Diffstat (limited to 'src/Middleware/SetLocale.php')
-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);
+ }
+}