vendor/pimcore/pimcore/bundles/CoreBundle/EventListener/Frontend/LocaleListener.php line 79

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\CoreBundle\EventListener\Frontend;
  15. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Event\RequestEvent;
  18. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. /**
  21.  * @internal
  22.  */
  23. class LocaleListener implements EventSubscriberInterface
  24. {
  25.     use PimcoreContextAwareTrait;
  26.     protected $lastLocale null;
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             KernelEvents::REQUEST => ['onKernelRequest'1], // need to be after ElementListener
  34.             KernelEvents::RESPONSE => ['onKernelResponse'], // need to be after ElementListener
  35.         ];
  36.     }
  37.     /**
  38.      * @param RequestEvent $event
  39.      */
  40.     public function onKernelRequest(RequestEvent $event)
  41.     {
  42.         $request $event->getRequest();
  43.         $locale $request->getLocale();
  44.         if ($locale && $locale != $this->lastLocale) {
  45.             $this->lastLocale $locale;
  46.             // now we prepare everything for setlocale()
  47.             $localeList = [$locale '.utf8'];
  48.             $primaryLanguage \Locale::getPrimaryLanguage($locale);
  49.             if (\Locale::getRegion($locale)) {
  50.                 // add only the language to the list as a fallback
  51.                 $localeList[] = $primaryLanguage '.utf8';
  52.             } else {
  53.                 // try to get a list of territories for this language
  54.                 // usually OS have no "language only" locale, only the combination language-territory (eg. Debian)
  55.                 $languageRegionMapping = include PIMCORE_PATH '/bundles/CoreBundle/Resources/misc/cldr-language-territory-mapping.php';
  56.                 if (isset($languageRegionMapping[$primaryLanguage])) {
  57.                     foreach ($languageRegionMapping[$primaryLanguage] as $territory) {
  58.                         $localeList[] = $primaryLanguage '_' $territory '.utf8';
  59.                     }
  60.                 }
  61.             }
  62.             setlocale(LC_ALL$localeList);
  63.             setlocale(LC_NUMERIC'C');
  64.         }
  65.     }
  66.     public function onKernelResponse(ResponseEvent $event)
  67.     {
  68.         if ($this->lastLocale && $event->isMainRequest()) {
  69.             $response $event->getResponse();
  70.             $response->headers->set('Content-Language'strtolower(str_replace('_''-'$this->lastLocale)), true);
  71.         }
  72.     }
  73. }