src/Aqarmap/Bundle/MainBundle/Locale/DefaultLocaleResolver.php line 40

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\MainBundle\Locale;
  3. use JMS\I18nRoutingBundle\Router\LocaleResolverInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. /**
  6.  * Default Locale Resolver.
  7.  */
  8. class DefaultLocaleResolver implements LocaleResolverInterface
  9. {
  10.     private $cookieName;
  11.     private $hostMap;
  12.     public function __construct($cookieName$acceptHeaderLanguages true, array $hostMap = [])
  13.     {
  14.         $this->cookieName $cookieName;
  15.         $this->hostMap $hostMap;
  16.     }
  17.     public function resolveLocale(Request $request, array $availableLocales)
  18.     {
  19.         if ($this->hostMap && isset($this->hostMap[$host $request->getHost()])) {
  20.             return $this->hostMap[$host];
  21.         }
  22.         // if a locale has been specifically set as a query parameter, use it
  23.         if ($request->query->has('hl')) {
  24.             $hostLanguage $request->query->get('hl');
  25.             if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i'$hostLanguage)) {
  26.                 return $hostLanguage;
  27.             }
  28.         }
  29.         // check if a session exists, and if it contains a locale
  30.         if ($request->hasPreviousSession()) {
  31.             $session $request->getSession();
  32.             if ($session->has('_locale')) {
  33.                 return $session->get('_locale');
  34.             }
  35.         }
  36.         // if user sends a cookie, use it
  37.         if ($request->cookies->has($this->cookieName)) {
  38.             $hostLanguage $request->cookies->get($this->cookieName);
  39.             if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i'$hostLanguage)) {
  40.                 return $hostLanguage;
  41.             }
  42.         }
  43.     }
  44. }