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 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; }