Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Classes/Service/CountryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
9 changes: 8 additions & 1 deletion Classes/Service/LanguageManipulationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
Expand Down