Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error pages in multilang #6608

Draft
wants to merge 3 commits into
base: develop-minor
Choose a base branch
from
Draft
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
18 changes: 16 additions & 2 deletions src/Cms/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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?)';
Expand Down
64 changes: 62 additions & 2 deletions tests/Cms/Languages/LanguageRoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class LanguageRoutesTest extends TestCase
{
protected $app;
public const FIXTURES = __DIR__ . '/fixtures';

public function setUp(): void
{
Expand Down Expand Up @@ -37,6 +38,19 @@ public function setUp(): void
public function testFallback()
{
$app = $this->app->clone([
'languages' => [
[
'code' => 'fr',
'name' => 'French',
'default' => true,
'url' => '/',
],
[
'code' => 'en',
'name' => 'English',
'url' => '/en',
],
],
'site' => [
'children' => [
[
Expand All @@ -48,10 +62,56 @@ public function testFallback()
]);

$app->call('notes');
$this->assertSame($app->language()->code(), 'fr');

$app->call('en/notes');
$this->assertSame($app->language()->code(), 'en');
}

public static function languagePrefixProvider(): array {
return [
['not-exists', 'Erreur'],
['en/not-exists', 'Error']
];
}

/**
* @dataProvider languagePrefixProvider
*/
public function testLanguagePrefix($path, $body)
{
$app = new App([
'roots' => [
'index' => static::FIXTURES,
'languages' => static::FIXTURES . '/languages',
'templates' => static::FIXTURES . '/templates'
],
'site' => [
'children' => [
[
'slug' => 'error',
'template' => 'error',
'translations' => [
[
'code' => 'fr',
'content' => [
'title' => 'Erreur'
]
],
[
'code' => 'en',
'content' => [
'title' => 'Error'
]
]
]
]
]
]
]);


$app->call('de/notes');
$this->assertSame($app->language()->code(), 'de');
$this->assertSame($body, $app->render($path)->body());
}

public function testNotNextWhenFalsyReturn()
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 @@ -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
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/Cms/Languages/fixtures/languages/en.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

return [
'code' => 'en',
'default' => false,
'direction' => 'ltr',
'locale' => [
'LC_ALL' => 'en_US'
],
'name' => 'English',
'translations' => [

],
'url' => NULL
];
15 changes: 15 additions & 0 deletions tests/Cms/Languages/fixtures/languages/fr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

return [
'code' => 'fr',
'default' => true,
'direction' => 'ltr',
'locale' => [
'LC_ALL' => 'fr_FR'
],
'name' => 'French',
'translations' => [

],
'url' => '/'
];
1 change: 1 addition & 0 deletions tests/Cms/Languages/fixtures/templates/default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?= $page->title();
Loading