diff --git a/src/Cms/Language.php b/src/Cms/Language.php index d9e3ebac24..eb8c63100b 100644 --- a/src/Cms/Language.php +++ b/src/Cms/Language.php @@ -8,6 +8,7 @@ use Kirby\Exception\LogicException; use Kirby\Exception\PermissionException; use Kirby\Filesystem\F; +use Kirby\Toolkit\A; use Kirby\Toolkit\Locale; use Kirby\Toolkit\Str; use Throwable; @@ -213,7 +214,7 @@ public static function create(array $props): static */ public function delete(): bool { - $kirby = App::instance(); + $kirby = $this->kirby(); $user = $kirby->user(); $code = $this->code(); @@ -382,7 +383,20 @@ public function pattern(): string $path = $this->path(); if (empty($path) === true) { - return '(:all)'; + $pattern = '(:all)'; + + // match anything except paths that begin with the prefix + // of any other language + $languages = $this->kirby()->languages()->not($this)->values( + fn ($language) => $language->path() + ); + + if (count($languages) > 0) { + $pattern = '^(?!(?:' . A::join($languages, '|') . ')\/)' . $pattern; + } + + + return $pattern; } return $path . '/(:all?)'; diff --git a/tests/Cms/Languages/LanguageTest.php b/tests/Cms/Languages/LanguageTest.php index d0b587dddf..8885f369e8 100644 --- a/tests/Cms/Languages/LanguageTest.php +++ b/tests/Cms/Languages/LanguageTest.php @@ -556,6 +556,33 @@ public function testPattern($input, $expected) $this->assertSame($expected, $language->pattern()); } + /** + * @covers ::pattern + */ + public function testPatternWithNoPathPrefixButOtherLanguages() + { + $app = $this->app->clone([ + 'languages' => [ + [ + 'code' => 'en', + 'name' => 'English', + 'default' => true, + 'url' => '/' + ], + [ + 'code' => 'de', + 'name' => 'Deutsch', + ], + [ + 'code' => 'fr', + 'name' => 'Frances', + ] + ] + ]); + + $this->assertSame('^(?!(?:de|fr)\/)(:all)', $app->language('en')->pattern()); + } + /** * @covers ::root */