Skip to content

Add Merge::DISABLED_OURS to keep a re-added string active - #319

Merged
oscarotero merged 4 commits into
php-gettext:masterfrom
jayhemnani9910:fix-merge-reenable-obsolete
Dec 3, 2026
Merged

oscarotero merged 4 commits into
php-gettext:masterfrom
jayhemnani9910:fix-merge-reenable-obsolete

Conversation

@jayhemnani9910

@jayhemnani9910 jayhemnani9910 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

When a string comes back into the code after it was removed, scanning and loading the existing PO keeps it obsolete, so its translation never reaches the .mo:

# fr.po
#~ msgid "Obsolete"
#~ msgstr "Obsolète"

# messages.pot (the string is used again)
#: src/d.php:1
msgid "Obsolete"
msgstr ""

msgmerge fr.po messages.pot brings it back active with "Obsolète".

This adds an opt-in Merge::DISABLED_OURS: keep whether $translation1 is disabled instead of taking it from $translation2. Nothing changes without it, SCAN_AND_LOAD included. For the case above, use Merge::SCAN_AND_LOAD | Merge::DISABLED_OURS (testReaddedStringIsNoLongerObsolete). testDisabledStateComesFromTheOtherSideByDefault checks that the default is unchanged. README lists the flag. phpunit (112 tests) and phpstan pass.

@oscarotero

Copy link
Copy Markdown
Member

I'm not sure about this.
It makes impossible to disable a translation from one of the merged translations. For example, a old .po file has a translation enabled but a newer version has it disabled.
Maybe this could take into account the value of Merge::TRANSLATIONS_OVERRIDE?
Additionally, we can create a new flag for this use case.

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.
@jayhemnani9910
jayhemnani9910 force-pushed the fix-merge-reenable-obsolete branch from e4e8667 to eb11e9b Compare December 1, 2026 03:42
@jayhemnani9910

Copy link
Copy Markdown
Contributor Author

Good point. In eb11e9b the obsolete state follows Merge::TRANSLATIONS_OVERRIDE: with it, the other side decides, so a newer file can still disable a translation. Without it, an entry stays obsolete only when both sides are. There's a test for each.

One catch: SCAN_AND_LOAD includes TRANSLATIONS_OVERRIDE, so a scan merged with a PO that way still keeps a re-added string obsolete. Only the default strategy brings it back now. If you want the scan case covered too, I can add a flag for it (say Merge::DISABLED_OURS, like the other *_OURS flags) and put it in SCAN_AND_LOAD. Or I can leave it as is.

@oscarotero

oscarotero commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

SCAN_AND_LOAD was created to first scan the translations from the code (i.e. PHP) and then merge the existing tranlations (i.e. PO). I guess the PO is the only part that enable or disable a merge (afaik is not possible to disable a translation from the code). So, in theory, the TRANSLATIONS_OVERRIDE option should be enough.

But there may be other cases in which we are mergin two PO files, and both sides can enable or disable comments.
Maybe Merge::DISABLED_* can be useful to make this behavior more explicit and avoid magic or undesired behaviors.
To me, that an entry stays obsolete only when both sides are absolete is problematic and I would avoid it.

@jayhemnani9910 jayhemnani9910 changed the title Keep a merged entry obsolete only when both sides are Add Merge::DISABLED_OURS so a re-added string is no longer obsolete Dec 1, 2026
@jayhemnani9910

Copy link
Copy Markdown
Contributor Author

Makes sense, I dropped the "both sides" rule. In 57163ae:

  • New Merge::DISABLED_OURS: keep whether $translation1 is disabled. Without the flag nothing changes, and $translation2 decides, as before this PR.
  • SCAN_AND_LOAD includes it, so a string found in the code is active even if the PO has it obsolete. Strings that are only in the PO keep their state.

I added only an _OURS flag, since "theirs" is already the default. Happy to add DISABLED_THEIRS too if you want the pair to match the other flags. If you'd rather keep SCAN_AND_LOAD as it is and only ship the flag, I can take it out of there.

@oscarotero

Copy link
Copy Markdown
Member

Great, thank you!
I don't want to introduce any breaking change, so maybe is better to don't add it to SCAN_AND_LOAD by default. It's easier to add it later with SCAN_AND_LOAD | DISABLED_OURS.

@jayhemnani9910 jayhemnani9910 changed the title Add Merge::DISABLED_OURS so a re-added string is no longer obsolete Add Merge::DISABLED_OURS to keep a re-added string active Dec 3, 2026
@jayhemnani9910

Copy link
Copy Markdown
Contributor Author

Done in abd08cf. SCAN_AND_LOAD is back to what it was, and DISABLED_OURS is opt-in. The test for the re-added string uses SCAN_AND_LOAD | DISABLED_OURS, and nothing else changes.

@oscarotero
oscarotero merged commit a1e0687 into php-gettext:master Dec 3, 2026
11 checks passed
@oscarotero

Copy link
Copy Markdown
Member

Perfect. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants