Skip to content

Instantly share code, notes, and snippets.

@pankajlele
Forked from bwaidelich/DetectLanguageComponent.php
Last active August 31, 2015 20:44
Show Gist options
  • Save pankajlele/b8a878ae520978dc403d to your computer and use it in GitHub Desktop.
Save pankajlele/b8a878ae520978dc403d to your computer and use it in GitHub Desktop.
A TYPO3 Flow HTTP component that detects the user agent language and redirects to the corresponding URL if no language has been requested explicitly.
<?php
namespace Wwwision\Test\Http;
/* *
* This script belongs to the TYPO3 Flow package "Wwwision.Test". *
* *
* */
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Http\Component\ComponentChain;
use TYPO3\Flow\Http\Component\ComponentContext;
use TYPO3\Flow\Http\Component\ComponentInterface;
use TYPO3\Flow\I18n\Detector;
use TYPO3\Flow\I18n\Locale;
use TYPO3\Neos\Domain\Service\ContentDimensionPresetSourceInterface;
/**
* A HTTP component that detects the user agent language and redirects to a corresponding section
*/
class DetectLanguageComponent implements ComponentInterface {
/**
* @Flow\Inject
* @var Detector
*/
protected $localeDetector;
/**
* @Flow\Inject
* @var ContentDimensionPresetSourceInterface
*/
protected $contentDimensionPresetSource;
/**
* @var array
*/
protected $options;
/**
* @param array $options The component options
*/
public function __construct(array $options = array()) {
$this->options = $options;
}
/**
* @param ComponentContext $componentContext
* @return void
*/
public function handle(ComponentContext $componentContext) {
$httpRequest = $componentContext->getHttpRequest();
$requestPath = $httpRequest->getUri()->getPath();
if (preg_match($this->options['ignorePattern'], $requestPath) === 1) {
return;
}
$firstRequestPathSegment = explode('/', ltrim($requestPath, '/'))[0];
$preset = $this->contentDimensionPresetSource->findPresetByUriSegment('language', $firstRequestPathSegment);
// first request path segment corresponds to a valid language preset -> no need to redirect
if ($preset !== NULL) {
return;
}
$detectedLocale = $this->localeDetector->detectLocaleFromHttpHeader($httpRequest->getHeader('Accept-Language'));
if (!$detectedLocale instanceof Locale) {
return;
}
$detectedLanguage = $detectedLocale->getLanguage();
$preset = $this->contentDimensionPresetSource->findPresetByUriSegment('language', $detectedLanguage);
// language preset for detected language is invalid -> do not redirect
if ($preset === NULL) {
return;
}
$uri = $httpRequest->getUri();
$uri->setPath('/' . $detectedLanguage . $requestPath);
$httpResponse = $componentContext->getHttpResponse();
$httpResponse->setContent(sprintf('<html><head><meta http-equiv="refresh" content="0;url=%s"/></head></html>', htmlentities((string)$uri, ENT_QUOTES, 'utf-8')));
$httpResponse->setStatus(303);
$httpResponse->setHeader('Location', (string)$uri);
$componentContext->setParameter(ComponentChain::class, 'cancel', TRUE);
}
}
TYPO3:
Flow:
http:
chain:
'preprocess':
chain:
'detectLanguage':
component: 'Wwwision\Test\Http\DetectLanguageComponent'
componentOptions:
ignorePattern: '/(^\/neos|^\/@)/'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment