Esempio n. 1
0
 /**
  * {@inheritdoc }
  */
 public function detect(LocaleEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request)) {
         return;
     }
     if (!$event->hasSupported()) {
         return;
     }
     $locale = $request->getQuery($this->getQueryKey());
     if ($locale === null) {
         return;
     }
     if (!in_array($locale, $event->getSupported())) {
         return;
     }
     return $locale;
 }
Esempio n. 2
0
 public function found(LocaleEvent $event)
 {
     $locale = $event->getLocale();
     $request = $event->getRequest();
     $cookieName = $this->getCookieName();
     if (!$this->isHttpRequest($request)) {
         return;
     }
     $cookie = $request->getCookie();
     // Omit Set-Cookie header when cookie is present
     if ($cookie instanceof Cookie && $cookie->offsetExists($cookieName) && $locale === $cookie->offsetGet($cookieName)) {
         return;
     }
     $path = '/';
     if (method_exists($request, 'getBasePath')) {
         $path = rtrim($request->getBasePath(), '/') . '/';
     }
     $response = $event->getResponse();
     $setCookie = new SetCookie($cookieName, $locale, null, $path);
     $response->getHeaders()->addHeader($setCookie);
 }
 public function detect(LocaleEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request)) {
         return;
     }
     if ($lookup = $event->hasSupported()) {
         $supported = $event->getSupported();
     }
     $headers = $request->getHeaders();
     if ($headers->has('Accept-Language')) {
         $locales = $headers->get('Accept-Language')->getPrioritized();
         foreach ($locales as $locale) {
             $locale = $locale->getLanguage();
             if (!$lookup) {
                 return $locale;
             }
             if (Locale::lookup($supported, $locale)) {
                 return $locale;
             }
         }
     }
 }
Esempio n. 4
0
 /**
  * {@inheritdoc }
  */
 public function detect(LocaleEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request)) {
         return;
     }
     if (!$event->hasSupported()) {
         return;
     }
     $homepage = $_SERVER['REQUEST_URI'];
     $locale = $request->getQuery($this->getQueryKey());
     if ($locale === null) {
         if (strstr($homepage, '/fr/')) {
             return 'fr';
         }
         if (strstr($homepage, '/en/')) {
             return 'en';
         }
         if (strstr($homepage, '/es/')) {
             return 'es';
         }
         if (strstr($homepage, '/it/')) {
             return 'it';
         }
         if (strstr($homepage, '/nl/')) {
             return 'nl';
         }
         if (strstr($homepage, '/pt/')) {
             return 'pt';
         }
         return;
     }
     if (!in_array($locale, $event->getSupported())) {
         return;
     }
     return $locale;
 }
 /**
  * @param LocaleEvent $event
  * @return ResponseInterface|void
  */
 public function found(LocaleEvent $event)
 {
     $locale = $event->getLocale();
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request)) {
         return;
     }
     /** @var HttpRequest $request */
     $cookie = $request->getCookie();
     $cookieName = $this->getCookieName();
     // Omit Set-Cookie header when cookie is present
     // and Expires does not need renewing...
     if ($this->getCookieExpiration() === null && $cookie instanceof CookieHeader && $cookie->offsetExists($cookieName) && $locale === $cookie->offsetGet($cookieName)) {
         return;
     }
     $cookiePath = '/';
     if (method_exists($request, 'getBasePath')) {
         $cookiePath = rtrim($request->getBasePath(), '/') . '/';
     }
     $setCookie = new SetCookieHeader($cookieName, $locale, $this->getCookieExpiration(), $cookiePath);
     /** @var HttpResponse $response */
     $response = $event->getResponse();
     $response->getHeaders()->addHeader($setCookie);
 }
Esempio n. 6
0
 public function found(LocaleEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request)) {
         return;
     }
     if (!$event->hasSupported()) {
         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;
 }
Esempio n. 7
0
 public function found(LocaleEvent $event)
 {
     $request = $event->getRequest();
     $assetManager = $this->getServiceLocator()->getServiceLocator()->get('AssetManager\\Service\\AssetManager');
     $request = $event->getRequest();
     if ($assetManager->resolvesToAsset($request)) {
         return;
     }
     if (!$this->isHttpRequest($request)) {
         return;
     }
     $locale = $event->getLocale();
     if (null === $locale) {
         return;
     }
     if (!$this->redirectToCanonical() && null !== $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) {
         return;
     }
     if (!$this->redirectWhenFound()) {
         return;
     }
     $uri = $request->getUri();
     $path = $uri->getPath();
     if (!$found || $event->hasSupported() && !in_array($found, $event->getSupported())) {
         $path = $base . '/' . $locale . substr($path, strlen($base));
     } 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;
 }
Esempio n. 8
0
 public function found(LocaleEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request)) {
         return;
     }
     $locale = $event->getLocale();
     if (null === $locale) {
         return;
     }
     if (!$this->redirectToCanonical() && null !== $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) {
         return;
     }
     if (!$this->redirectWhenFound()) {
         return;
     }
     $uri = $request->getUri();
     $path = $uri->getPath();
     if (!$found || $event->hasSupported() && !in_array($found, $event->getSupported())) {
         $path = '/' . $locale . $path;
     } else {
         $path = preg_filter('/^\\/(.+?)(\\/.+)/', '/' . $locale . '$2', $path);
     }
     $uri->setPath($path);
     $response = $event->getResponse();
     $response->setStatusCode(self::REDIRECT_STATUS_CODE);
     $response->getHeaders()->addHeaderLine('Location', $uri->toString());
     return $response;
 }
Esempio n. 9
0
 /**
  * {@inheritdoc}
  */
 public function assemble(LocaleEvent $event)
 {
     $locale = $event->getLocale();
     foreach ($this->getAliases() as $alias => $item) {
         if ($item == $locale) {
             $tld = $alias;
         }
     }
     if (!isset($tld)) {
         throw new InvalidArgumentException('No matching tld found for current locale');
     }
     $port = $event->getRequest()->getServer()->get('SERVER_PORT');
     $hostname = str_replace(self::LOCALE_KEY, $tld, $this->getDomain());
     if (null !== $port && 80 != $port) {
         $hostname .= ':' . $port;
     }
     $uri = $event->getUri();
     $uri->setHost($hostname);
     return $uri;
 }
 /**
  * @param LocaleEvent $event
  * @return ResponseInterface|void
  */
 public function found(LocaleEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->isHttpRequest($request)) {
         return null;
     }
     /** @var HttpRequest $request */
     $locale = $this->getLocaleOrAlias($event);
     // Check if the route should be ignored...
     // Note that this check works only when the locale is not already in the URI path.
     if ($this->isIgnoredRoute()) {
         return null;
     }
     // Check if redirection is enabled for current request's method...
     if (!$this->isHttpRequestMethodEnabled($request)) {
         // Since it's a HTTP request, we need to set the base URL accordingly (with locale, i.e. /de),
         // when the locale is already in the URI.
         // Otherwise the route is not matched and we get a 404.
         $base = $this->getBasePath();
         $found = $this->getFirstSegmentInPath($request->getUri(), $base);
         if ($locale === $found) {
             /** @var TreeRouteStack $router */
             $router = $this->getRouter();
             $router->setBaseUrl($base . '/' . $locale);
         }
         return null;
     }
     return parent::found($event);
 }