<?php
namespace App\Service\Locale;
use App\Constants\AppConstant;
use Symfony\Component\HttpFoundation\RequestStack;
class LocaleProvider
{
private RequestStack $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
/**
* @return string
*/
public function provide(): string
{
$request = $this->requestStack->getCurrentRequest();
if (null !== $request) {
$locale = $request->get(AppConstant::LOCALE_SESSION_CONSTANTE)
?? $request->getSession()->get(AppConstant::LOCALE_SESSION_CONSTANTE)
?? AppConstant::DEFAULT_LOCALE;
if (!in_array($locale, AppConstant::LOCALES, true)) {
$locale = AppConstant::DEFAULT_LOCALE;
}
$request->setLocale($locale);
$request->getSession()->set(AppConstant::LOCALE_SESSION_CONSTANTE, $locale);
return $locale;
}
return AppConstant::DEFAULT_LOCALE;
}
}