From ee2bc391100e28d87806de3be0f606d226e395c5 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:51:50 +0530 Subject: [PATCH 1/4] Keep a merged entry obsolete only when both sides are mergeWith() copied the other side's disabled flag, so a string that was used again stayed obsolete after merging a scan with a PO and its translation never reached the .mo. --- src/Translation.php | 4 +++- tests/MergeTest.php | 17 +++++++++++++++++ tests/snapshots/testNoStrategy.php | 2 +- tests/snapshots/testScanAndLoadStrategy.php | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Translation.php b/src/Translation.php index cdcf580d..71b20675 100644 --- a/src/Translation.php +++ b/src/Translation.php @@ -289,7 +289,9 @@ public function mergeWith(Translation $translation, int $strategy = 0): Translat $merged->pluralTranslations = $translation->pluralTranslations; } - $merged->disable($translation->isDisabled()); + // An entry stays obsolete only when it is obsolete on both sides: a string + // that is used again comes back with its translation + $merged->disable($merged->isDisabled() && $translation->isDisabled()); return $merged; } diff --git a/tests/MergeTest.php b/tests/MergeTest.php index 4248e7b5..5ca43c6c 100644 --- a/tests/MergeTest.php +++ b/tests/MergeTest.php @@ -126,6 +126,23 @@ 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)->find(null, 'Obsolete'); + + $this->assertFalse($merged->isDisabled()); + $this->assertSame('Obsolète', $merged->getTranslation()); + } + private function assertSnapshot(string $name, Translations $translations, bool $forceCreate = false): void { $file = __DIR__."/snapshots/{$name}.php"; diff --git a/tests/snapshots/testNoStrategy.php b/tests/snapshots/testNoStrategy.php index f26af6aa..61f0cc9e 100644 --- a/tests/snapshots/testNoStrategy.php +++ b/tests/snapshots/testNoStrategy.php @@ -44,7 +44,7 @@ 'translation' => 'Intro', 'plural' => null, 'pluralTranslations' => [], - 'disabled' => true, + 'disabled' => false, 'previousContext' => null, 'previousOriginal' => null, 'previousPlural' => null, diff --git a/tests/snapshots/testScanAndLoadStrategy.php b/tests/snapshots/testScanAndLoadStrategy.php index 91e970ad..3e8765eb 100644 --- a/tests/snapshots/testScanAndLoadStrategy.php +++ b/tests/snapshots/testScanAndLoadStrategy.php @@ -38,7 +38,7 @@ 'translation' => 'Intro', 'plural' => null, 'pluralTranslations' => [], - 'disabled' => true, + 'disabled' => false, 'previousContext' => null, 'previousOriginal' => null, 'previousPlural' => null, From eb11e9b44620f619ad9dd16b58f15a6e30d5802e Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Tue, 1 Dec 2026 09:11:54 +0530 Subject: [PATCH 2/4] Let TRANSLATIONS_OVERRIDE keep the other side's obsolete state --- src/Translation.php | 9 +++++++-- tests/MergeTest.php | 17 ++++++++++++++++- tests/snapshots/testScanAndLoadStrategy.php | 2 +- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Translation.php b/src/Translation.php index 71b20675..75117034 100644 --- a/src/Translation.php +++ b/src/Translation.php @@ -289,9 +289,14 @@ public function mergeWith(Translation $translation, int $strategy = 0): Translat $merged->pluralTranslations = $translation->pluralTranslations; } - // An entry stays obsolete only when it is obsolete on both sides: a string + // With TRANSLATIONS_OVERRIDE the other side decides whether the entry is obsolete. + // Otherwise it stays obsolete only when it is obsolete on both sides, so a string // that is used again comes back with its translation - $merged->disable($merged->isDisabled() && $translation->isDisabled()); + if ($override) { + $merged->disable($translation->isDisabled()); + } else { + $merged->disable($merged->isDisabled() && $translation->isDisabled()); + } return $merged; } diff --git a/tests/MergeTest.php b/tests/MergeTest.php index 5ca43c6c..f510028f 100644 --- a/tests/MergeTest.php +++ b/tests/MergeTest.php @@ -137,12 +137,27 @@ public function testReaddedStringIsNoLongerObsolete(): void $translation->disable(); $po->add($translation); - $merged = $scanned->mergeWith($po, Merge::SCAN_AND_LOAD)->find(null, 'Obsolete'); + $merged = $scanned->mergeWith($po)->find(null, 'Obsolete'); $this->assertFalse($merged->isDisabled()); $this->assertSame('Obsolète', $merged->getTranslation()); } + public function testOverrideKeepsTheOtherSideObsolete(): void + { + $old = Translations::create(); + $old->add(Translation::create(null, 'Obsolete')); + + $new = Translations::create(); + $translation = Translation::create(null, 'Obsolete'); + $translation->disable(); + $new->add($translation); + + $merged = $old->mergeWith($new, Merge::TRANSLATIONS_OVERRIDE)->find(null, 'Obsolete'); + + $this->assertTrue($merged->isDisabled()); + } + private function assertSnapshot(string $name, Translations $translations, bool $forceCreate = false): void { $file = __DIR__."/snapshots/{$name}.php"; diff --git a/tests/snapshots/testScanAndLoadStrategy.php b/tests/snapshots/testScanAndLoadStrategy.php index 3e8765eb..91e970ad 100644 --- a/tests/snapshots/testScanAndLoadStrategy.php +++ b/tests/snapshots/testScanAndLoadStrategy.php @@ -38,7 +38,7 @@ 'translation' => 'Intro', 'plural' => null, 'pluralTranslations' => [], - 'disabled' => false, + 'disabled' => true, 'previousContext' => null, 'previousOriginal' => null, 'previousPlural' => null, From 57163aee096fa4a3144e74b6268a1c505f2cf33b Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Wed, 2 Dec 2026 00:25:53 +0530 Subject: [PATCH 3/4] Add Merge::DISABLED_OURS and use it in SCAN_AND_LOAD --- README.md | 1 + src/Merge.php | 5 ++++- src/Translation.php | 7 +------ tests/MergeTest.php | 12 ++++++------ tests/snapshots/testNoStrategy.php | 2 +- tests/snapshots/testScanAndLoadStrategy.php | 2 +- 6 files changed, 14 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 4e178b49..63a7808d 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/Merge.php b/src/Merge.php index e69fb908..fad68094 100644 --- a/src/Merge.php +++ b/src/Merge.php @@ -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 @@ -36,5 +38,6 @@ final class Merge | Merge::EXTRACTED_COMMENTS_OURS | Merge::REFERENCES_OURS | Merge::FLAGS_THEIRS - | Merge::COMMENTS_THEIRS; + | Merge::COMMENTS_THEIRS + | Merge::DISABLED_OURS; } diff --git a/src/Translation.php b/src/Translation.php index 75117034..1293851b 100644 --- a/src/Translation.php +++ b/src/Translation.php @@ -289,13 +289,8 @@ public function mergeWith(Translation $translation, int $strategy = 0): Translat $merged->pluralTranslations = $translation->pluralTranslations; } - // With TRANSLATIONS_OVERRIDE the other side decides whether the entry is obsolete. - // Otherwise it stays obsolete only when it is obsolete on both sides, so a string - // that is used again comes back with its translation - if ($override) { + if (!($strategy & Merge::DISABLED_OURS)) { $merged->disable($translation->isDisabled()); - } else { - $merged->disable($merged->isDisabled() && $translation->isDisabled()); } return $merged; diff --git a/tests/MergeTest.php b/tests/MergeTest.php index f510028f..49cbfb43 100644 --- a/tests/MergeTest.php +++ b/tests/MergeTest.php @@ -117,7 +117,8 @@ public function testScanAndLoadStrategy(): void | Merge::EXTRACTED_COMMENTS_OURS // Keep only the extracted comments | Merge::REFERENCES_OURS // Keep only the scanned references | Merge::FLAGS_THEIRS // Keep the flags in PO - | Merge::COMMENTS_THEIRS; // Keep the comments in PO + | Merge::COMMENTS_THEIRS // Keep the comments in PO + | Merge::DISABLED_OURS; // A scanned entry is in use, even if the PO has it obsolete $this->assertSame($strategy, Merge::SCAN_AND_LOAD); @@ -137,13 +138,13 @@ public function testReaddedStringIsNoLongerObsolete(): void $translation->disable(); $po->add($translation); - $merged = $scanned->mergeWith($po)->find(null, 'Obsolete'); + $merged = $scanned->mergeWith($po, Merge::SCAN_AND_LOAD)->find(null, 'Obsolete'); $this->assertFalse($merged->isDisabled()); $this->assertSame('Obsolète', $merged->getTranslation()); } - public function testOverrideKeepsTheOtherSideObsolete(): void + public function testDisabledStateComesFromTheOtherSideByDefault(): void { $old = Translations::create(); $old->add(Translation::create(null, 'Obsolete')); @@ -153,9 +154,8 @@ public function testOverrideKeepsTheOtherSideObsolete(): void $translation->disable(); $new->add($translation); - $merged = $old->mergeWith($new, Merge::TRANSLATIONS_OVERRIDE)->find(null, 'Obsolete'); - - $this->assertTrue($merged->isDisabled()); + $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 diff --git a/tests/snapshots/testNoStrategy.php b/tests/snapshots/testNoStrategy.php index 61f0cc9e..f26af6aa 100644 --- a/tests/snapshots/testNoStrategy.php +++ b/tests/snapshots/testNoStrategy.php @@ -44,7 +44,7 @@ 'translation' => 'Intro', 'plural' => null, 'pluralTranslations' => [], - 'disabled' => false, + 'disabled' => true, 'previousContext' => null, 'previousOriginal' => null, 'previousPlural' => null, diff --git a/tests/snapshots/testScanAndLoadStrategy.php b/tests/snapshots/testScanAndLoadStrategy.php index 91e970ad..3e8765eb 100644 --- a/tests/snapshots/testScanAndLoadStrategy.php +++ b/tests/snapshots/testScanAndLoadStrategy.php @@ -38,7 +38,7 @@ 'translation' => 'Intro', 'plural' => null, 'pluralTranslations' => [], - 'disabled' => true, + 'disabled' => false, 'previousContext' => null, 'previousOriginal' => null, 'previousPlural' => null, From abd08cf0be9dcab75e15bac0e361611621a3c86e Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Thu, 3 Dec 2026 09:44:24 +0530 Subject: [PATCH 4/4] Keep SCAN_AND_LOAD unchanged; DISABLED_OURS is opt-in --- src/Merge.php | 3 +-- tests/MergeTest.php | 5 ++--- tests/snapshots/testScanAndLoadStrategy.php | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Merge.php b/src/Merge.php index fad68094..fb7e5b1e 100644 --- a/src/Merge.php +++ b/src/Merge.php @@ -38,6 +38,5 @@ final class Merge | Merge::EXTRACTED_COMMENTS_OURS | Merge::REFERENCES_OURS | Merge::FLAGS_THEIRS - | Merge::COMMENTS_THEIRS - | Merge::DISABLED_OURS; + | Merge::COMMENTS_THEIRS; } diff --git a/tests/MergeTest.php b/tests/MergeTest.php index 49cbfb43..10e03e53 100644 --- a/tests/MergeTest.php +++ b/tests/MergeTest.php @@ -117,8 +117,7 @@ public function testScanAndLoadStrategy(): void | Merge::EXTRACTED_COMMENTS_OURS // Keep only the extracted comments | Merge::REFERENCES_OURS // Keep only the scanned references | Merge::FLAGS_THEIRS // Keep the flags in PO - | Merge::COMMENTS_THEIRS // Keep the comments in PO - | Merge::DISABLED_OURS; // A scanned entry is in use, even if the PO has it obsolete + | Merge::COMMENTS_THEIRS; // Keep the comments in PO $this->assertSame($strategy, Merge::SCAN_AND_LOAD); @@ -138,7 +137,7 @@ public function testReaddedStringIsNoLongerObsolete(): void $translation->disable(); $po->add($translation); - $merged = $scanned->mergeWith($po, Merge::SCAN_AND_LOAD)->find(null, 'Obsolete'); + $merged = $scanned->mergeWith($po, Merge::SCAN_AND_LOAD | Merge::DISABLED_OURS)->find(null, 'Obsolete'); $this->assertFalse($merged->isDisabled()); $this->assertSame('Obsolète', $merged->getTranslation()); diff --git a/tests/snapshots/testScanAndLoadStrategy.php b/tests/snapshots/testScanAndLoadStrategy.php index 3e8765eb..91e970ad 100644 --- a/tests/snapshots/testScanAndLoadStrategy.php +++ b/tests/snapshots/testScanAndLoadStrategy.php @@ -38,7 +38,7 @@ 'translation' => 'Intro', 'plural' => null, 'pluralTranslations' => [], - 'disabled' => false, + 'disabled' => true, 'previousContext' => null, 'previousOriginal' => null, 'previousPlural' => null,