/**
  * @param string $path
  * @return string
  * @throws \Exception
  */
 public function render($path = NULL)
 {
     if ($path === NULL) {
         $path = '404';
     }
     /** @var RequestHandler $activeRequestHandler */
     $activeRequestHandler = $this->bootstrap->getActiveRequestHandler();
     $parentHttpRequest = $activeRequestHandler->getHttpRequest();
     $requestPath = $parentHttpRequest->getUri()->getPath();
     $language = explode('/', ltrim($requestPath, '/'))[0];
     if ($language === 'neos') {
         throw new \Exception('NotFoundViewHelper can not be used for neos-routes.', 1435648210);
     }
     $language = $this->localeDetector->detectLocaleFromLocaleTag($language)->getLanguage();
     if ($this->contentDimensionPresetSource->findPresetByUriSegment('language', $language) === NULL) {
         $language = '';
     }
     if ($language !== '') {
         $language .= '/';
     }
     $request = Request::create(new Uri(rtrim($parentHttpRequest->getBaseUri(), '/') . '/' . $language . $path));
     $matchingRoute = $this->router->route($request);
     if (!$matchingRoute) {
         throw new \Exception(sprintf('Uri with path "%s" could not be found.', rtrim($parentHttpRequest->getBaseUri(), '/') . '/' . $language . $path), 1426446160);
     }
     $response = new Response();
     $objectManager = $this->bootstrap->getObjectManager();
     $baseComponentChain = $objectManager->get('TYPO3\\Flow\\Http\\Component\\ComponentChain');
     $componentContext = new ComponentContext($request, $response);
     $baseComponentChain->handle($componentContext);
     return $response->getContent();
 }
Exemple #2
0
 /**
  * Initializes some basic stuff that will basically be needed for each and
  * every action that is executed later on.
  */
 public function initializeAction()
 {
     // get the account of the authenticated user
     $this->account = $this->securityContext->getAccount();
     // set the locale
     $this->locale = $this->localeDetector->detectLocaleFromLocaleTag($this->settings['defaultLanguage']);
     if ($this->l18nService->getConfiguration()->getCurrentLocale() !== $this->locale) {
         $this->l18nService->getConfiguration()->setCurrentLocale($this->locale);
     }
 }
 /**
  * @param string $locale
  */
 public function showCommand($locale = 'en_EN')
 {
     ini_set('memory_limit', '340M');
     /** @var Language $language */
     $languages = $this->languageRepository->findAll();
     $localeObject = $this->detector->detectLocaleFromLocaleTag($locale);
     foreach ($languages as $language) {
         $this->outputFormatted($language->getKey() . ' - ' . $language->getNameForLocale($localeObject));
     }
 }
 /**
  * Get an array of all values in the CLDR where the key is the type attribute
  *
  * @param string $path The xpath to select values from
  * @return array|boolean
  */
 protected function getKeyValues($path)
 {
     $defaultLocale = $this->detector->detectLocaleFromLocaleTag('en');
     $model = $this->cldrRepository->getModelForLocale($defaultLocale);
     $data = $model->getRawArray($path);
     if ($data === FALSE) {
         return FALSE;
     }
     $filteredData = array();
     foreach ($data as $nodeString => $children) {
         if (CldrModel::getAttributeValue($nodeString, 'alt') === FALSE) {
             $key = CldrModel::getAttributeValue($nodeString, 'type');
             $filteredData[$key] = $children;
         }
     }
     return $filteredData;
 }
 /**
  * @test
  * @dataProvider sampleLocaleIdentifiers
  */
 public function detectingBestMatchingLocaleFromLocaleIdentifierWorksCorrectly($localeIdentifier, $expectedResult)
 {
     $locale = $this->detector->detectLocaleFromLocaleTag($localeIdentifier);
     $this->assertEquals($expectedResult, $locale);
 }
Exemple #6
0
 /**
  * @return \TYPO3\Flow\I18n\Locale
  */
 public function getLocalizedName()
 {
     $locale = $this->detector->detectLocaleFromLocaleTag($this->key);
     return $this->getNameForLocale($locale);
 }
 /**
  * Get a locale matching the identifier string
  * @param string $identifier
  * @return \TYPO3\Flow\I18n\Locale
  */
 public function getLocaleByIdentifier($identifier)
 {
     return $this->detector->detectLocaleFromLocaleTag($identifier);
 }
 /**
  * @param ComponentContext $componentContext
  * @return void
  */
 public function handle(ComponentContext $componentContext)
 {
     $httpRequest = $componentContext->getHttpRequest();
     $requestPath = $httpRequest->getUri()->getPath();
     $firstRequestPathSegment = explode('/', ltrim($requestPath, '/'))[0];
     //Check if url contains user, if so, don't detect language
     if (strpos($requestPath, '@user-')) {
         return;
     }
     if (isset($this->options['allowedMethods']) && !in_array($httpRequest->getMethod(), $this->options['allowedMethods'])) {
         //the current HTTP method is not within the allow methods, abort!
         return;
     }
     $preset = null;
     if (!isset($this->options['ignoreSegments']) || !in_array($firstRequestPathSegment, $this->options['ignoreSegments'])) {
         $preset = $this->findPreset($firstRequestPathSegment);
         if ($preset !== null) {
             //uri contains a valid language segment => no need for us to proceed
             return;
         }
     } else {
         //the configuration told us to ignore this segment => no need for us to proceed
         return;
     }
     $defaultPreset = $this->contentDimensionPresetSource->getDefaultPreset('language');
     $referer = $httpRequest->getHeaders()->get('Referer');
     $refererInfo = $this->parseUriInfo($referer);
     $currentInfo = $this->parseUriInfo((string) $httpRequest->getUri());
     $varnishInfo = isset($this->varnishSettings['varnishUrl']) ? $this->parseUriInfo($this->varnishSettings['varnishUrl']) : null;
     if ($refererInfo['host'] == $currentInfo['host'] || $varnishInfo !== null && $refererInfo['host'] == $varnishInfo['host']) {
         $firstRefererRequestPathSegment = explode('/', ltrim($refererInfo['requestPath'], '/'))[0];
         $refererPreset = $preset = $this->findPreset($firstRefererRequestPathSegment);
         if (empty($firstRequestPathSegment) && $refererPreset !== null && empty(ltrim(str_replace($firstRefererRequestPathSegment, '', $refererInfo['requestPath']), '/'))) {
             $preset = $defaultPreset;
         } else {
             $preset = $refererPreset;
         }
     } else {
         $detectedLocale = $this->localeDetector->detectLocaleFromHttpHeader($httpRequest->getHeader('Accept-Language'));
         if ($detectedLocale instanceof Locale) {
             $preset = $this->findPreset($detectedLocale->getLanguage());
             if ($preset !== null && empty(trim($requestPath, " \t\n\r\v/")) && $preset['uriSegment'] == $defaultPreset['uriSegment']) {
                 //we're on the homepage, and the browsers language is equal to the default language => no need for us to proceed
                 return;
             }
         }
     }
     if ($preset === null) {
         $preset = $defaultPreset;
     }
     if ($preset === null) {
         throw new Exception("Couldn't resolve the language and default language is not set. Check your language config.");
         return;
     }
     $uri = $httpRequest->getUri();
     if (isset($this->httpSettings['baseUri'])) {
         $baseInfo = $this->parseUriInfo($this->httpSettings['baseUri']);
         $uri->setHost($baseInfo['host']);
     }
     if (isset($this->httpSettings['port'])) {
         $uri->setPort($this->httpSettings['port']);
     }
     if (isset($this->httpSettings['username'])) {
         $uri->setUsername($this->httpSettings['username']);
     }
     if (isset($this->httpSettings['password'])) {
         $uri->setUsername($this->httpSettings['password']);
     }
     $uri->setPath('/' . $preset['uriSegment'] . $requestPath);
     $response = $componentContext->getHttpResponse();
     $response->setContent(sprintf('<html><head><meta http-equiv="refresh" content="0;url=%s"/></head></html>', htmlentities((string) $uri, ENT_QUOTES, 'utf-8')));
     $response->setHeader('Location', (string) $uri);
     $componentContext->setParameter(ComponentChain::class, 'cancel', TRUE);
 }