Skip to content

Add a --check flag to makemessages#573

Open
BJacksonONS wants to merge 18 commits intomainfrom
CMS-789-makemessages-linter
Open

Add a --check flag to makemessages#573
BJacksonONS wants to merge 18 commits intomainfrom
CMS-789-makemessages-linter

Conversation

@BJacksonONS
Copy link
Copy Markdown

@BJacksonONS BJacksonONS commented Apr 9, 2026

What is the context of this PR?

Relates to CMS-789.

Add a --check flag to makemessages management command to warn if there would be any updates to .po files. Intended to be used in pre-commit and CI to ensure translations aren't missed.

How to review

Try running make make messages-check without any code changes.
Try running after changing a string in a call to gettext and sense check returns correct value.

Deployment Safety

Bleed and Sandbox deploy automatically on merge, so PRs should be safe to deploy immediately.

Please select one:

  • Safe to auto-deploy
  • Not safe to auto-deploy

Follow-up Actions

Needs to be added to pre commit and CI.

Might want to add line numbers to original code files at some point.

@BJacksonONS BJacksonONS marked this pull request as ready for review April 9, 2026 12:46
@BJacksonONS BJacksonONS requested a review from a team as a code owner April 9, 2026 12:46
@MaciekBaron MaciekBaron changed the title override makemessages with command that includes a --check flag to warn of changes to the .po files Override makemessages with command that includes a --check flag to warn of changes to the .po files Apr 9, 2026
@BJacksonONS BJacksonONS added the DX Developer eXperience label Apr 9, 2026
Copy link
Copy Markdown
Contributor

@MebinAbraham MebinAbraham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good first PR, just a few comments. 🙌

Comment thread cms/locale/management/commands/makemessages.py
Comment thread cms/locale/management/commands/makemessages.py Outdated
Comment thread cms/locale/management/commands/makemessages.py Outdated
Comment thread cms/locale/management/commands/makemessages.py Outdated
Comment thread cms/locale/management/commands/makemessages.py Outdated
@MebinAbraham MebinAbraham added the component: i18n 🌍 Internationalization / Localization label Apr 10, 2026
Comment thread Makefile Outdated
Copy link
Copy Markdown
Contributor

@MaciekBaron MaciekBaron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just one typo + wording suggestion

Comment thread cms/locale/management/commands/makemessages.py Outdated
Copy link
Copy Markdown
Contributor

@MebinAbraham MebinAbraham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add this new command to our CI.yml (GitHub Actions) and pre-commit.

Comment on lines +35 to +39
return (
f"{'msgctxt: {self.msgctxt}\n' if self.msgctxt != '' else ''}"
f"msgid: {self.msgid}"
f"{'\nmsgid_plural: {self.msgid_plural}' if self.msgid_plural != '' else ''}"
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The msgid_plural and msgctxt are not being interpolated properly, and also we can't have backslashes in fstrings.

Suggested change
return (
f"{'msgctxt: {self.msgctxt}\n' if self.msgctxt != '' else ''}"
f"msgid: {self.msgid}"
f"{'\nmsgid_plural: {self.msgid_plural}' if self.msgid_plural != '' else ''}"
)
return (
(f"msgctxt: {self.msgctxt}\n" if self.msgctxt else "")
+ f"msgid: {self.msgid}"
+ (f"\nmsgid_plural: {self.msgid_plural}" if self.msgid_plural else "")
)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for path in self._modified_po_files:
self.stderr.write(f" {path}\n\n")
if len(self._modified_po_files[path]) > 0:
self.stderr.write(" The following msgids have changed\n\n")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.stderr.write(" The following msgids have changed\n\n")
self.stderr.write(" The following msgids have changed:\n\n")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if len(self._modified_po_files[path]) > 0:
self.stderr.write(" The following msgids have changed\n\n")
for item in self._modified_po_files[path]:
self.stderr.write(f" {item}\n\n")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The formatting off this when you have ctx, and plural seems a bit off. It would be good to make it consistent.

Currently you get:

    The following msgids have changed                                                                                                                                                                    
                                                                                                                                                                                                         
    msgctxt: button label                                                                                                                                                                                
msgid: Open new                                                                                                                                                                                          
                                                                                                                                                                                                         
    msgid: Open New                                                                                                                                                                                      
msgid_plural: Open New   

Would be good to have something like:

- msgctxt: button label
  msgid: Open new

- msgid: Open New
  msgid_plural: Open News

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have some improved formatting here, do we want to also insert indentation for multiline strings or leave them in a similar format to the .po file?

- msgid: 
If the web address is correct, or you selected a link or button, <a 
href="%(contact_url)s">contact us</a>.

vs

- msgid: 
  If the web address is correct, or you selected a link or button, <a 
  href="%(contact_url)s">contact us</a>.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm easy either way, up to you.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to leave it as is then

"""Extracts groups of msgid, msgctxt and msgid_plural from pofile contents and returns
a set of unique groups keyed with a tuple of those values in the order (msgid, msgctxt and msgid_plural).

msgctxt and msgid_plural will default to an empty string if not present.
Copy link
Copy Markdown
Contributor

@MebinAbraham MebinAbraham Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity:

        msgctxt and msgid_plural will default to an empty string if not present.
        
        Used by --check mode to detect changes that affect translation behaviour
        only. This intentionally ignores header metadata, comments, source
        references, ordering, wrapping, and other non-functional PO churn.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread README.md Outdated
make makemessages-check
```

Will warn if .po files will change without applying changes to disk.
Copy link
Copy Markdown
Contributor

@MebinAbraham MebinAbraham Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Will warn if .po files will change without applying changes to disk.
Will error if `.po` files will change that affect translation behaviour. It ignores non-functional changes such as headers, comments, source references, entry ordering, and line wrapping.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, warn > error since we exit 1.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread cms/locale/tests/test_makemessages.py Outdated
Comment on lines +315 to +317
content_before = Path(self.potfile).read_text(encoding="utf-8")
self.command.write_po_file(self.potfile, "cy")
content_after = Path(self.potfile).read_text(encoding="utf-8")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be using po file?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for path in self._modified_po_files:
self.stderr.write(f" {path}\n\n")
if len(self._modified_po_files[path]) > 0:
self.stderr.write(" The following msgids have changed\n\n")
Copy link
Copy Markdown
Contributor

@MebinAbraham MebinAbraham Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it might not be msgid that have changed, maybe The following translation keys have changed: or The following translation items have changed:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MebinAbraham MebinAbraham changed the title Override makemessages with command that includes a --check flag to warn of changes to the .po files Add a --check flag to makemessages Apr 15, 2026
@BJacksonONS
Copy link
Copy Markdown
Author

@MebinAbraham
Added to lint step in CI and pre-commit.
034e5dc
ccb7619

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

Labels

component: i18n 🌍 Internationalization / Localization DX Developer eXperience

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants