Skip to content

Commit

Permalink
Implement default translation content fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Jun 20, 2024
1 parent 9f595b5 commit 6c03ae2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Content/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,25 @@ public function __construct(
*/
public function content(Language|string $language = 'default'): Content
{
$language = Language::ensure($language);
$fields = $this->read($language);

// This is where we merge content from the default language
// to provide a fallback for missing/untranslated fields.
//
// @todo This is the critical point that needs to be removed/refactored
// in the future, to provide multi-language support with truly
// individual versions of pages and no longer enforce the fallback.
if ($language->isDefault() === false) {
$fields = [
...$this->read('default'),
...$fields
];
}

return new Content(
parent: $this->model,
data: $this->read($language),
data: $fields,
);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/Content/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,31 @@ public function testContentSingleLanguage(): void
$this->assertSame($expected['content']['title'], $version->content()->get('title')->value());
}

/**
* @covers ::content
*/
public function testContentWithFallback(): void
{
$this->setUpMultiLanguage();

$version = new Version(
model: $this->model,
id: VersionId::published()
);

// write something to the content file to make sure it
// can be read from disk for the test.
Data::write($this->model->root() . '/article.en.txt', $content = [
'title' => 'Test'
]);

$this->assertSame($content, $version->content()->toArray());
$this->assertSame($content, $version->content('en')->toArray());

// make sure that the content fallback works
$this->assertSame($version->content('en')->toArray(), $version->content('de')->toArray());
}

/**
* @covers ::contentFile
*/
Expand Down

0 comments on commit 6c03ae2

Please sign in to comment.