/** * Returns best-matching Locale object based on the Accept-Language header * provided as parameter. System default locale will be returned if no * successful matches were done. * * @param string $acceptLanguageHeader The Accept-Language HTTP header * @return \TYPO3\Flow\I18n\Locale Best-matching existing Locale instance * @api */ public function detectLocaleFromHttpHeader($acceptLanguageHeader) { $acceptableLanguages = \TYPO3\Flow\I18n\Utility::parseAcceptLanguageHeader($acceptLanguageHeader); if ($acceptableLanguages === false) { return $this->localizationService->getConfiguration()->getDefaultLocale(); } foreach ($acceptableLanguages as $languageIdentifier) { if ($languageIdentifier === '*') { return $this->localizationService->getConfiguration()->getDefaultLocale(); } try { $locale = new \TYPO3\Flow\I18n\Locale($languageIdentifier); } catch (\TYPO3\Flow\I18n\Exception\InvalidLocaleIdentifierException $exception) { continue; } $bestMatchingLocale = $this->localeCollection->findBestMatchingLocale($locale); if ($bestMatchingLocale !== null) { return $bestMatchingLocale; } } return $this->localizationService->getConfiguration()->getDefaultLocale(); }
/** * @test * @dataProvider sampleHttpAcceptLanguageHeaders */ public function httpAcceptLanguageHeadersAreParsedCorrectly($acceptLanguageHeader, array $expectedResult) { $languages = \TYPO3\Flow\I18n\Utility::parseAcceptLanguageHeader($acceptLanguageHeader); $this->assertEquals($expectedResult, $languages); }