diff --git a/Classes/Service/CountryService.php b/Classes/Service/CountryService.php index c74c291..74e6be8 100644 --- a/Classes/Service/CountryService.php +++ b/Classes/Service/CountryService.php @@ -134,8 +134,15 @@ public static function getCountryByUri(UriInterface $uri = null): ?Country $function = static function () use ($uri) { $path = ($uri ?: new Uri((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . ':// . ' . ($_SERVER['HTTP_HOST'] ?? '') . ($_SERVER['REQUEST_URI'] ?? '')))->getPath(); + $hasAMatch = false; + if (LanguageManipulationService::LANGUAGE_FIRST) { + $hasAMatch = preg_match('#^\/?[a-z]{2}' . LanguageManipulationService::BASE_DELIMITER . '([a-zA-Z0-9_-]+)#', $path, $matches); + } else { + $hasAMatch = preg_match('#^\/([a-zA-Z0-9_-]+)?' . LanguageManipulationService::BASE_DELIMITER . '[a-z]{2}#', $path, $matches); + } + return - preg_match('/^\/?[a-z]{2}' . LanguageManipulationService::BASE_DELIMITER . '([a-zA-Z0-9_-]+)/', $path, $matches) + $hasAMatch && ($countryParameter = $matches[1]) && ($country = self::getCountryByParameter($countryParameter)) ? $country : null; }; diff --git a/Classes/Service/LanguageManipulationService.php b/Classes/Service/LanguageManipulationService.php index f3be041..e1503df 100644 --- a/Classes/Service/LanguageManipulationService.php +++ b/Classes/Service/LanguageManipulationService.php @@ -14,6 +14,7 @@ class LanguageManipulationService { public const BASE_DELIMITER = '-'; + public const LANGUAGE_FIRST = true; protected static function cleanString(string $string, int $maxLength = null, bool $forceLowercase = null): string { @@ -49,7 +50,13 @@ protected static function cleanIsoCode(string $string, int $maxLength = null, bo public static function getBase(SiteLanguage $language, Country $country = null): UriInterface { if ($country && ($parameter = $country->getParameter())) { - return $language->getBase()->withPath('/' . self::cleanIsoCode($language->getTwoLetterIsoCode(), 2, true) . self::BASE_DELIMITER . self::cleanString($parameter) . '/'); + $languageCountryPart = ''; + if (self::LANGUAGE_FIRST) { + $languageCountryPart = self::cleanIsoCode($language->getTwoLetterIsoCode(), 2, true) . self::BASE_DELIMITER . self::cleanString($parameter); + } else { + $languageCountryPart = self::cleanString($parameter) . self::BASE_DELIMITER . self::cleanIsoCode($language->getTwoLetterIsoCode(), 2, true); + } + return $language->getBase()->withPath($siteBase->getPath() . $languageCountryPart . '/'); } return self::getOriginalLanguage($language)->getBase();