public function detect(DetectorConfig $config, RequestInterface $request) { $domains = $config->getDomains(); $host = $request->getUri()->getHost(); $matched = null; if (null === $domains || empty($domains)) { throw new Exception\InvalidArgumentException('No domains where configured'); } foreach ($domains as $domain) { if (strpos($domain, self::LOCALE_KEY) === false) { throw new Exception\InvalidArgumentException(sprintf('The domain %s must contain a locale key part "%s"', $domain, self::LOCALE_KEY)); } $pattern = str_replace(self::LOCALE_KEY, '([a-zA-Z-_.]+)', $domain); $pattern = sprintf('@%s@', $pattern); $result = preg_match($pattern, $host, $matches); if ($result) { $matched = $matches; } } $locale = $matched[1]; $aliases = $config->getAliases(); if (null !== $aliases && array_key_exists($locale, $aliases)) { $locale = $aliases[$locale]; } return $locale; }
public function setUp() { $config = new DetectorConfig(); $config->setDomains(array('test.:locale')); $config->setSupported(array('en_US')); $config->setAliases(array('nl' => 'nl_NL')); $config->setDefault('nl_NL'); $this->config = $config; }
public function detect(DetectorConfig $config, RequestInterface $request) { $base = $this->getBasePath(); $locale = $this->getFirstSegmentInPath($request->getUri(), $base); $aliases = $config->getAliases(); if (null !== $aliases && array_key_exists($locale, $aliases)) { $locale = $aliases[$locale]; } return $locale; }
/** * @param ServiceLocatorInterface $serviceLocator * @return Detector */ public function createService(ServiceLocatorInterface $serviceLocator) { $config = $serviceLocator->get('config'); $config = $config['mq_locale']; $detectorConfig = new DetectorConfig(); if (array_key_exists('default', $config)) { $detectorConfig->setDefault($config['default']); } if (array_key_exists('supported', $config)) { $detectorConfig->setSupported($config['supported']); } if (array_key_exists('domains', $config)) { $detectorConfig->setDomains($config['domains']); } if (array_key_exists('aliases', $config)) { $detectorConfig->setAliases($config['aliases']); } if (array_key_exists('strategy', $config)) { $detectorConfig->setStrategy($config['strategy']); } return new Detector($detectorConfig); }