mirror of
https://github.com/flarum/core.git
synced 2025-10-14 00:15:51 +02:00
Starting with version 5.9, the global funtions will be deprecated. * https://laravel-news.com/laravel-5-8-deprecates-string-and-array-helpers * https://github.com/laravel/framework/pull/26898
55 lines
1.3 KiB
PHP
55 lines
1.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Flarum.
|
|
*
|
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Flarum\Http\Middleware;
|
|
|
|
use Flarum\Locale\LocaleManager;
|
|
use Illuminate\Support\Arr;
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
|
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
|
|
|
class SetLocale implements Middleware
|
|
{
|
|
/**
|
|
* @var LocaleManager
|
|
*/
|
|
protected $locales;
|
|
|
|
/**
|
|
* @param LocaleManager $locales
|
|
*/
|
|
public function __construct(LocaleManager $locales)
|
|
{
|
|
$this->locales = $locales;
|
|
}
|
|
|
|
public function process(Request $request, Handler $handler): Response
|
|
{
|
|
$actor = $request->getAttribute('actor');
|
|
|
|
if ($actor->exists) {
|
|
$locale = $actor->getPreference('locale');
|
|
} else {
|
|
$locale = Arr::get($request->getCookieParams(), 'locale');
|
|
}
|
|
|
|
if ($locale && $this->locales->hasLocale($locale)) {
|
|
$this->locales->setLocale($locale);
|
|
}
|
|
|
|
$request = $request->withAttribute('locale', $this->locales->getLocale());
|
|
|
|
return $handler->handle($request);
|
|
}
|
|
}
|