Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ Constant | Description
`Merge::FLAGS_THEIRS` | Use only the flags of `$translation2`
`Merge::REFERENCES_OURS` | Use only the references of `$translation1`
`Merge::REFERENCES_THEIRS` | Use only the references of `$translation2`
`Merge::DISABLED_OURS` | Keep whether `$translation1` is disabled (obsolete), instead of taking it from `$translation2`

Use the second argument to configure the merging strategy:

Expand Down
2 changes: 2 additions & 0 deletions src/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final class Merge
public const REFERENCES_OURS = 1 << 12;
public const REFERENCES_THEIRS = 1 << 13;

public const DISABLED_OURS = 1 << 14;

//Merge strategies
public const SCAN_AND_LOAD =
Merge::HEADERS_OVERRIDE
Expand Down
4 changes: 3 additions & 1 deletion src/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ public function mergeWith(Translation $translation, int $strategy = 0): Translat
$merged->pluralTranslations = $translation->pluralTranslations;
}

$merged->disable($translation->isDisabled());
if (!($strategy & Merge::DISABLED_OURS)) {
$merged->disable($translation->isDisabled());
}

return $merged;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/MergeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,37 @@ public function testScanAndLoadStrategy(): void
$this->assertSnapshot(__FUNCTION__, $merged);
}

public function testReaddedStringIsNoLongerObsolete(): void
{
$scanned = Translations::create();
$scanned->add(Translation::create(null, 'Obsolete'));

$po = Translations::create();
$translation = Translation::create(null, 'Obsolete');
$translation->translate('Obsolète');
$translation->disable();
$po->add($translation);

$merged = $scanned->mergeWith($po, Merge::SCAN_AND_LOAD | Merge::DISABLED_OURS)->find(null, 'Obsolete');

$this->assertFalse($merged->isDisabled());
$this->assertSame('Obsolète', $merged->getTranslation());
}

public function testDisabledStateComesFromTheOtherSideByDefault(): void
{
$old = Translations::create();
$old->add(Translation::create(null, 'Obsolete'));

$new = Translations::create();
$translation = Translation::create(null, 'Obsolete');
$translation->disable();
$new->add($translation);

$this->assertTrue($old->mergeWith($new)->find(null, 'Obsolete')->isDisabled());
$this->assertFalse($old->mergeWith($new, Merge::DISABLED_OURS)->find(null, 'Obsolete')->isDisabled());
}

private function assertSnapshot(string $name, Translations $translations, bool $forceCreate = false): void
{
$file = __DIR__."/snapshots/{$name}.php";
Expand Down
Loading