Skip to content

Commit

Permalink
Fix error pages in multilang
Browse files Browse the repository at this point in the history
Fixes #4834
  • Loading branch information
distantnative committed Aug 11, 2024
1 parent 0830adc commit f7563bc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Cms/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\LogicException;
use Kirby\Filesystem\F;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Locale;
use Kirby\Toolkit\Str;
use Throwable;
Expand Down Expand Up @@ -203,7 +204,7 @@ public static function create(array $props): static
*/
public function delete(): bool
{
$kirby = App::instance();
$kirby = $this->kirby();
$code = $this->code();

if ($this->isDeletable() === false) {
Expand Down Expand Up @@ -364,7 +365,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?)';
Expand Down Expand Up @@ -503,7 +517,7 @@ public function update(array $props = null): static
// make sure the slug is nice and clean
$props['slug'] = Str::slug($props['slug'] ?? null);

$kirby = App::instance();
$kirby = $this->kirby();
$updated = $this->clone($props);

if (isset($props['translations']) === true) {
Expand Down
27 changes: 27 additions & 0 deletions tests/Cms/Languages/LanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,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
*/
Expand Down

0 comments on commit f7563bc

Please sign in to comment.