alltube/classes/Middleware/LocaleMiddleware.php

87 lines
2.3 KiB
PHP
Raw Normal View History

2017-05-29 19:11:59 +00:00
<?php
2017-05-29 19:11:59 +00:00
/**
* LocaleMiddleware class.
*/
2017-05-29 19:13:10 +00:00
2020-10-20 21:13:48 +00:00
namespace Alltube\Middleware;
2017-05-29 19:11:59 +00:00
2020-10-20 21:13:48 +00:00
use Alltube\Locale;
use Alltube\LocaleManager;
2017-05-30 20:20:16 +00:00
use Psr\Container\ContainerInterface;
2017-05-29 19:11:59 +00:00
use Slim\Http\Request;
use Slim\Http\Response;
use Teto\HTTP\AcceptLanguage;
/**
* Detect user locale.
*/
class LocaleMiddleware
{
2017-05-30 21:49:38 +00:00
/**
* LocaleManager instance.
*
* @var LocaleManager
*/
private $localeManager;
2017-05-29 19:11:59 +00:00
/**
2017-05-30 20:20:16 +00:00
* LocaleMiddleware constructor.
2017-05-29 19:11:59 +00:00
*
2017-05-30 20:20:16 +00:00
* @param ContainerInterface $container Slim dependency container
2017-05-29 19:11:59 +00:00
*/
2017-05-30 20:20:16 +00:00
public function __construct(ContainerInterface $container)
{
2017-05-30 21:49:38 +00:00
$this->localeManager = $container->get('locale');
2017-05-30 20:20:16 +00:00
}
2017-05-29 19:11:59 +00:00
/**
* Test if a locale can be used for the current user.
*
2020-05-13 20:28:05 +00:00
* @param mixed[] $proposedLocale Locale array created by AcceptLanguage::parse()
2017-05-29 19:11:59 +00:00
*
2020-05-13 19:18:32 +00:00
* @return Locale|null Locale if chosen, nothing otherwise
2017-05-29 19:11:59 +00:00
*/
2020-12-17 21:43:05 +00:00
public function testLocale(array $proposedLocale): ?Locale
2017-05-29 19:11:59 +00:00
{
2017-05-30 21:49:38 +00:00
foreach ($this->localeManager->getSupportedLocales() as $locale) {
2017-05-29 19:11:59 +00:00
$parsedLocale = AcceptLanguage::parse($locale);
if (
isset($proposedLocale['language'])
2017-05-29 19:11:59 +00:00
&& $parsedLocale[1]['language'] == $proposedLocale['language']
&& $parsedLocale[1]['region'] == $proposedLocale['region']
) {
return new Locale($proposedLocale['language'] . '_' . $proposedLocale['region']);
2017-05-29 19:11:59 +00:00
}
}
2020-05-13 19:18:32 +00:00
return null;
2017-05-29 19:11:59 +00:00
}
/**
2017-05-29 19:13:10 +00:00
* Main middleware function.
2017-05-29 19:11:59 +00:00
*
2020-05-13 20:28:05 +00:00
* @param Request $request PSR request
2017-05-29 19:11:59 +00:00
* @param Response $response PSR response
2020-05-13 20:28:05 +00:00
* @param callable $next Next middleware
2017-05-29 19:11:59 +00:00
*
* @return Response
*/
2020-12-17 21:43:05 +00:00
public function __invoke(Request $request, Response $response, callable $next): Response
2017-05-29 19:11:59 +00:00
{
$headers = $request->getHeader('Accept-Language');
2017-05-30 21:49:38 +00:00
$curLocale = $this->localeManager->getLocale();
if (is_null($curLocale)) {
2017-05-30 21:30:21 +00:00
if (isset($headers[0])) {
2017-05-30 21:49:38 +00:00
$this->localeManager->setLocale(
2017-05-30 21:41:26 +00:00
AcceptLanguage::detect([$this, 'testLocale'], new Locale('en_US'), $headers[0])
2017-05-30 21:30:21 +00:00
);
} else {
2017-05-30 21:49:38 +00:00
$this->localeManager->setLocale(new Locale('en_US'));
2017-05-30 21:30:21 +00:00
}
2017-05-30 20:20:16 +00:00
}
2017-05-29 19:11:59 +00:00
return $next($request, $response);
}
}