Example #1
0
 /**
  * {@inheritdoc}
  */
 public function detect(Event $event)
 {
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request) || !$event->hasLocales()) {
         return;
     }
     $locale = $request->getQuery($this->getQueryKey());
     if ($locale === null || !$event->hasLocale($locale)) {
         return;
     }
     return $locale;
 }
Example #2
0
 /**
  * @param Event $event
  * @return void|string
  */
 public function detect(Event $event)
 {
     $request = $event->getRequest();
     $cookieName = $this->getCookieName();
     if (!$this->isHttpRequest($request) || !$event->hasLocales()) {
         return;
     }
     $cookie = $request->getCookie();
     if (!$cookie || !$cookie->offsetExists($cookieName)) {
         return;
     }
     $locale = $cookie->offsetGet($cookieName);
     $locales = $event->getLocales();
     if (!$event->hasLocale($locale)) {
         return;
     }
     return $locale;
 }
 /**
  * {@inheritDoc}
  */
 public function detect(Event $event)
 {
     if (!($authService = $this->getAuthenticationService())) {
         return;
     }
     if ($lookup = $event->hasLocales()) {
         $locales = $event->getLocales();
     }
     if ($authService->hasIdentity()) {
         $identity = $authService->getIdentity();
         if ($identity instanceof LocalizableInterface && $identity->getLocale()) {
             $locale = $identity->getLocale()->getCanonicalName();
             if (!$lookup) {
                 return $locale;
             }
             if (\Locale::lookup($locales, $locale)) {
                 return $locale;
             }
         }
     }
 }
 /**
  * {@inheritDoc}
  */
 public function detect(Event $event)
 {
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request)) {
         return;
     }
     $lookup = $event->hasLocales();
     if ($lookup) {
         $locales = $event->getLocales();
     }
     $headers = $request->getHeaders();
     if ($headers->has('Accept-Language')) {
         foreach ($headers->get('Accept-Language')->getPrioritized() as $locale) {
             $locale = $locale->getLanguage();
             if (!$lookup) {
                 return $locale;
             }
             if (\Locale::lookup($locales, $locale)) {
                 return $locale;
             }
         }
     }
 }
Example #5
0
 /**
  * {@inheritDoc}
  */
 public function found(Event $event)
 {
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request) || !$event->hasLocales()) {
         return;
     }
     $locale = $event->getLocale();
     if (null === $locale) {
         return;
     }
     // By default, use the alias to redirect to
     if (!$this->redirectToCanonical()) {
         $locale = $this->getAliasForLocale($locale);
     }
     $host = str_replace(self::LOCALE_KEY, $locale, $this->getDomain());
     $uri = $request->getUri();
     if ($host === $uri->getHost()) {
         return;
     }
     $uri->setHost($host);
     $response = $event->getResponse();
     $response->setStatusCode(self::REDIRECT_STATUS_CODE);
     $response->getHeaders()->addHeaderLine('Location', $uri->toString());
     return $response;
 }
Example #6
0
 /**
  * {@inheritDoc}
  */
 public function found(Event $event)
 {
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request)) {
         return;
     }
     $locale = $event->getLocale();
     if (null === $locale) {
         return;
     }
     if (!$this->redirectToCanonical() && $this->getAliases()) {
         $alias = $this->getAliasForLocale($locale);
         if (null !== $alias) {
             $locale = $alias;
         }
     }
     $base = $this->getBasePath();
     $found = $this->getFirstSegmentInPath($request->getUri(), $base);
     $this->getRouter()->setBaseUrl($base . '/' . $locale);
     if ($locale === $found || !$this->redirectWhenFound()) {
         return;
     }
     $uri = $request->getUri();
     $path = $uri->getPath();
     if (!$found || $event->hasLocales() && !$event->hasLocale($found)) {
         $path = '/' . $locale . $path;
     } else {
         $path = str_replace($found, $locale, $path);
     }
     $uri->setPath($path);
     $response = $event->getResponse();
     $response->setStatusCode(self::REDIRECT_STATUS_CODE);
     $response->getHeaders()->addHeaderLine('Location', $uri->toString());
     return $response;
 }