예제 #1
0
 /**
  * {@inheritDoc}
  */
 public function assemble(Event $event)
 {
     $uri = $event->getUri();
     $locale = $event->getLocale();
     $query = $uri->getQueryAsArray();
     $key = $this->getQueryKey();
     $query[$key] = $locale;
     $uri->setQuery($query);
     return $uri;
 }
예제 #2
0
 /**
  * @param Event $event
  * @return void
  */
 public function found(Event $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);
 }
 /**
  * {@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;
             }
         }
     }
 }
예제 #4
0
 /**
  * {@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;
             }
         }
     }
 }
예제 #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;
 }
예제 #6
0
 /**
  * {@inheritDoc}
  */
 public function assemble(Event $event)
 {
     $uri = $event->getUri();
     $base = $this->getBasePath();
     $locale = $event->getLocale();
     $current = $this->getFirstSegmentInPath($uri, $base);
     if (!$this->redirectToCanonical() && $this->getAliases()) {
         $alias = $this->getAliasForLocale($locale);
         if (null !== $alias) {
             $locale = $alias;
         }
     }
     $path = $uri->getPath();
     // Last part of base is now always locale, remove that
     $parts = explode('/', trim($base, '/'));
     array_pop($parts);
     $base = implode('/', $parts);
     if ($base) {
         $path = substr($path, strlen($base));
     }
     $parts = explode('/', trim($path, '/'));
     // Remove first part
     array_shift($parts);
     $path = $base . '/' . $locale . '/' . implode('/', $parts);
     $uri->setPath($path);
     return $uri;
 }
예제 #7
0
파일: Detector.php 프로젝트: coolms/locale
 /**
  * @param string $locale
  * @param string|\Zend\Uri\Uri $uri
  * @return \Zend\Uri\Uri
  */
 public function assemble($locale, $uri)
 {
     $event = new Event(Event::EVENT_ASSEMBLE, $this);
     $event->setLocale($locale);
     if ($this->hasLocales()) {
         $event->setLocales($this->getLocales());
     }
     if (!$uri instanceof Uri) {
         $uri = new Uri($uri);
     }
     $event->setUri($uri);
     $eventManager = $this->getEventManager();
     $results = $eventManager->trigger($event);
     if (!$results->stopped()) {
         return $uri;
     }
     return $results->last();
 }