Split user language (UI translation) from locale (formatted values) (#4595)

This commit is contained in:
Kevin Papst
2024-01-30 00:09:53 +01:00
committed by GitHub
parent 12ef19df28
commit df3ca9d5a9
39 changed files with 685 additions and 881 deletions

View File

@@ -10,6 +10,7 @@
namespace App\EventSubscriber;
use App\Entity\User;
use App\Twig\LocaleFormatExtensions;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
@@ -18,7 +19,11 @@ use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
final class UserEnvironmentSubscriber implements EventSubscriberInterface
{
public function __construct(private TokenStorageInterface $tokenStorage, private AuthorizationCheckerInterface $auth)
public function __construct(
private readonly TokenStorageInterface $tokenStorage,
private readonly AuthorizationCheckerInterface $auth,
private readonly LocaleFormatExtensions $localeFormatExtensions
)
{
}
@@ -36,19 +41,21 @@ final class UserEnvironmentSubscriber implements EventSubscriberInterface
return;
}
// the locale depends on the request, not on the user configuration
\Locale::setDefault($event->getRequest()->getLocale());
$locale = $event->getRequest()->getLocale();
// ignore events like the toolbar where we do not have a token
if (null === ($token = $this->tokenStorage->getToken())) {
return;
// events like the toolbar might not have a token
if (null !== ($token = $this->tokenStorage->getToken())) {
$user = $token->getUser();
if ($user instanceof User) {
$locale = $user->getLocale();
date_default_timezone_set($user->getTimezone());
$user->initCanSeeAllData($this->auth->isGranted('view_all_data'));
}
}
$user = $token->getUser();
if ($user instanceof User) {
date_default_timezone_set($user->getTimezone());
$user->initCanSeeAllData($this->auth->isGranted('view_all_data'));
}
// the locale is primarily used for formatting values, so we depend on the user locale if available
\Locale::setDefault($locale);
$this->localeFormatExtensions->setLocale($locale);
}
}