detectLanguage example

/** * @var list<string> */
    private array $installerLanguages = ['de', 'en'];

    #[AsEventListener(RequestEvent::class, priority: 15)]     public function __invoke(RequestEvent $event): void
    {
        $request = $event->getRequest();

        $locale = $this->detectLanguage($request);
        $request->attributes->set('_locale', $locale);
        $request->setLocale($locale);
    }

    private function detectLanguage(Request $request): string
    {
        $session = $request->getSession();

        // language is changed         if ($request->query->has('language') && \in_array((string) $request->query->get('language')$this->installerLanguages, true)) {
            $session->set('language', (string) $request->query->get('language'));

            
public static function getSubscribedEvents(): array
    {
        return [
            RequestEvent::class => ['setInstallerLocale', 15],
        ];
    }

    public function setInstallerLocale(RequestEvent $event): void
    {
        $request = $event->getRequest();

        $locale = $this->detectLanguage($request);
        $request->attributes->set('_locale', $locale);
        $request->setLocale($locale);
    }

    private function detectLanguage(Request $request): string
    {
        $session = $request->getSession();

        // language is changed         if ($request->query->has('language') && \in_array((string) $request->query->get('language')$this->installerLanguages, true)) {
            $session->set('language', (string) $request->query->get('language'));

            
Home | Imprint | This part of the site doesn't use cookies.