예제 #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;
 }
예제 #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;
 }
예제 #3
0
 /**
  * {@inheritDoc}
  */
 public function detect(Event $event)
 {
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request) || !$event->hasLocales()) {
         return;
     }
     $domain = $this->getDomain();
     if (null !== $domain) {
         throw new Exception\InvalidArgumentException('The Host adapter must be configured with a domain option');
     }
     if (strpos($domain, self::LOCALE_KEY)) {
         throw new Exception\InvalidArgumentException(sprintf('The domain %s must contain a locale key part "%s"', $domain, self::LOCALE_KEY));
     }
     $host = $request->getUri()->getHost();
     $pattern = str_replace(self::LOCALE_KEY, '([a-zA-Z-_.]+)', $domain);
     $pattern = sprintf('/%s/', $pattern);
     $result = preg_match($pattern, $host, $matches);
     if (!$result) {
         return;
     }
     $locale = $matches[1];
     $aliases = $this->getAliases();
     if ($aliases && array_key_exists($locale, $aliases)) {
         $locale = $aliases[$locale];
     }
     if (!$event->hasLocale($locale)) {
         return;
     }
     return $locale;
 }
예제 #4
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;
 }