trypost/app/Http/Middleware/App/SetLocale.php
Paulo Castellano c9209cb2a9 Replace the boolean isRtl() with a direction() accessor on ContentLanguage
isRtl() existed only to be mapped to an 'rtl'/'ltr' string in SetLocale, so the
boolean was the redundant concept. direction() returns the string the one caller
needs, which drops the null-safe-plus-nested-ternary from the middleware and lets
it resolve the language once with an explicit DEFAULT fallback.
2026-07-03 18:57:09 -03:00

40 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Middleware\App;
use App\Enums\Workspace\ContentLanguage;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\View;
use Symfony\Component\HttpFoundation\Response;
class SetLocale
{
/**
* @param Closure(Request): (Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$available = config('languages.available');
$locale = $request->cookie('locale');
$isValid = $locale && array_key_exists($locale, $available);
$activeLocale = $isValid ? $locale : config('languages.default');
$language = ContentLanguage::tryFrom($activeLocale) ?? ContentLanguage::DEFAULT;
App::setLocale($activeLocale);
View::share('htmlDir', $language->direction());
$response = $next($request);
if (! $isValid) {
$response->withCookie(
cookie()->forever('locale', config('languages.default'), '/', config('session.domain')),
);
}
return $response;
}
}