Skip to content

Commit

Permalink
Add the latest version of a translation automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Nov 13, 2024
1 parent b4d8b7c commit 1995ec2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Content/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ public function create(
Language|string $language = 'default'
): void {
$language = Language::ensure($language);
$latest = $this->model->version(VersionId::latest());

// if the latest version of the translation does not exist yet,
// we have to copy over the content from the default language first.
if (
$this->isLatest() === false &&
$language->isDefault() === false &&
$latest->exists($language) === false
) {
$latest->create(
fields: $latest->read(Language::ensure('default')),
language: $language
);
}

// check if creating is allowed
VersionRules::create($this, $fields, $language);
Expand Down
38 changes: 38 additions & 0 deletions tests/Content/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,44 @@ public function testCreateMultiLanguage(): void
$this->assertContentFileExists('de');
}

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

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

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

$this->assertContentFileDoesNotExist('en', $latest->id());
$this->assertContentFileDoesNotExist('en', $changes->id());
$this->assertContentFileDoesNotExist('de', $latest->id());
$this->assertContentFileDoesNotExist('de', $changes->id());

// create the latest version for the default translation
$latest->save([
'title' => 'Test'
], $this->app->language('en'));

// create a changes version in the other language
$changes->save([
'title' => 'Translated Test',
], $this->app->language('de'));

$this->assertContentFileExists('en', $latest->id());
$this->assertContentFileDoesNotExist('en', $changes->id());
$this->assertContentFileExists('de', $latest->id());
$this->assertContentFileExists('de', $changes->id());
}

/**
* @covers ::create
*/
Expand Down

0 comments on commit 1995ec2

Please sign in to comment.