Skip to content

Commit

Permalink
language selector middleware added
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Jan 27, 2021
1 parent e85f228 commit 8d4ef5d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"laminas/laminas-diactoros": "^2.5.0",
"laminas/laminas-inputfilter": "^2.11.0",
"laminas/laminas-permissions-rbac": "^3.0.2",
"laminas/laminas-servicemanager": "^3.6.2",
"laminas/laminas-servicemanager": "^3.6.3",
"laminas/laminas-stdlib": "^3.3.1",
"lcobucci/jwt": "^4.0.0",
"league/flysystem": "^1.1.3",
Expand All @@ -76,7 +76,7 @@
"mezzio/mezzio-csrf": "^1.0.1",
"mezzio/mezzio-fastroute": "^3.1.0",
"mezzio/mezzio-helpers": "^5.4.0",
"mezzio/mezzio-problem-details": "^1.2.0",
"mezzio/mezzio-problem-details": "^1.3.0",
"mezzio/mezzio-session-ext": "^1.10.0",
"mezzio/mezzio-twigrenderer": "^2.6.1",
"mkorkmaz/redislabs-rejson": "^1.0",
Expand Down Expand Up @@ -108,12 +108,11 @@
"filp/whoops": "^2.9.2",
"laminas/laminas-development-mode": "^3.3.0",
"malukenho/mcbumpface": "^1.1.5",
"mezzio/mezzio-tooling": "^1.3.0",
"phpunit/phpunit": "^9.5.1",
"roave/security-advisories": "dev-master",
"rskuipers/php-assumptions": "^0.8.0",
"squizlabs/php_codesniffer": "^3.5.8",
"swoole/ide-helper": "^4.6.1"
"swoole/ide-helper": "^4.6.2"
},
"autoload": {
"psr-4": {
Expand Down
76 changes: 76 additions & 0 deletions src/BackendBase/Shared/Middleware/LanguageSelectorMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace BackendBase\Shared\Middleware;

use Locale;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use const LC_ALL;
use function bind_textdomain_codeset;
use function bindtextdomain;
use function copy;
use function file_exists;
use function filemtime;
use function glob;
use function putenv;
use function setlocale;
use function textdomain;
use function unlink;

final class LanguageSelectorMiddleware implements MiddlewareInterface
{
private $config;

public function __construct(array $config)
{
$this->config = $config;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
$uri = $request->getUri();
$url = $uri->getPath();
$selectedLanguage = $this->config['multilingual']['default-language'];
if ($url !== '/') {
$urlParts = explode('/', trim($url, '/'));
$lang = array_shift($urlParts);
if (in_array($lang, $this->config['multilingual']['valid-languages'])) {
$selectedLanguage = $lang;
$request = $request->withUri($uri->withPath('/'. implode('/', $urlParts)));
}
}
$this->setLocale($selectedLanguage, $request->getAttribute('moduleName'));

return $handler->handle($request->withAttribute('selectedLanguage', $selectedLanguage));
}

private function setLocale(string $locale, string $domain) : void
{
$localeFile = 'data/cache/locale/' . $locale . '/LC_MESSAGES/' . $domain . '.mo';
if (! file_exists($localeFile)) {
return;
}
$modifiedTime = filemtime($localeFile);
$localeFileRuntime = 'data/cache/locale/' . $locale . '/LC_MESSAGES/' . $domain . '_' . $modifiedTime . '.mo';
if (! file_exists($localeFileRuntime)) {
$dir = glob('data/cache/locale/' . $locale . '/LC_MESSAGES/' . $domain . '_*.mo');
foreach ($dir as $file) {
unlink($file);
}
copy($localeFile, $localeFileRuntime);
}
$domain .='_' . $modifiedTime;
$lang = $locale . '.UTF8';
putenv("LANG={$lang}");
putenv("LANGUAGE={$lang}");
setlocale(LC_ALL, $lang);
Locale::setDefault($locale . '.UTF-8');
bindtextdomain($domain, 'data/cache/locale');
bind_textdomain_codeset($domain, 'UTF-8');
textdomain($domain);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace BackendBase\Shared\Middleware;

use BackendBase\Infrastructure\Persistence\Doctrine\Repository\RolesRepository;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Psr\Http\Server\MiddlewareInterface;

final class LanguageSelectorMiddlewareFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null) : MiddlewareInterface
{
return new LanguageSelectorMiddleware($container->get('config'));
}
}

0 comments on commit 8d4ef5d

Please sign in to comment.