New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce the use of a single string formatting mechanism for translation source strings #9377
Closed
loicteixeira
wants to merge
19
commits into
wagtail:main
from
loicteixeira:7111-enforce-translation-string-formatting
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
61ab22c
Add & configure semgrep
loicteixeira 448cdea
Update source strings
loicteixeira d176767
Fix unrelated Flake8 error
loicteixeira 85a9621
Update source strings used in JS
loicteixeira e10b16c
Update new strings after rebase
loicteixeira 032d5b2
Remove useless translation
loicteixeira 8fe43c6
Update mentions of blocktrans/trans to blocktranslate/translate to fo…
loicteixeira 5a53a93
Document desired formatting and link from semgrep error message
loicteixeira 49ac1f0
Use consistent wording
loicteixeira 8c9fb47
Upgrade pattern to work with ngettext which accepts multiple arguments
loicteixeira e901445
Update missed ngettext string
loicteixeira 9315a0c
Add ngettext_lazy to list of functions to catch
loicteixeira 623f43d
Run semgrep rules in CI and make lint
loicteixeira 2903c82
Avoid escaping placeholders
loicteixeira ef71da8
Run applicables rules on JavaScript files
loicteixeira cf1fa5c
Update javascript files as well
loicteixeira 65b996b
Fix conflict between multiple 'Edit this %s' strings
loicteixeira 6d9476d
Use more generic placeholder names for snippets in anticipation for m…
loicteixeira 868c239
Normalize placeholder name for instance/object
loicteixeira File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
rules: | ||
- id: translation-no-new-style-formatting | ||
patterns: | ||
- pattern: $FUNC("$STRING_ID", ...) | ||
- metavariable-regex: | ||
metavariable: $FUNC | ||
regex: '_|gettext|gettext_lazy|ngettext|ngettext_lazy' | ||
- metavariable-regex: | ||
metavariable: $STRING_ID | ||
regex: ".*({(\\d*|[\\w_]*)}).*" | ||
message: | | ||
Do not use str.format style formatting for translations. | ||
Use printf style formatting with named placeholders instead. | ||
For example, do `_("Hello %(name)s") % {"name": "Wagtail"}` | ||
instead of `_("Hello {name}").format(name="Wagtail")`. | ||
See https://docs.wagtail.org/en/latest/contributing/translations.html#marking-strings-for-translation for more information. | ||
languages: [python, javascript, typescript] | ||
severity: ERROR | ||
- id: translation-no-f-strings | ||
patterns: | ||
- pattern: $FUNC(f"...", ...) | ||
- metavariable-regex: | ||
metavariable: $FUNC | ||
regex: '_|gettext|gettext_lazy|ngettext|ngettext_lazy' | ||
message: > | ||
Do not use formatted string literals for translations. | ||
Use printf style formatting with named placeholders instead. | ||
For example, do `_("Hello %(name)s") % {"name": "Wagtail"}` | ||
instead of `_(f"Hello {name}")`. | ||
See https://docs.wagtail.org/en/latest/contributing/translations.html#marking-strings-for-translation for more information. | ||
languages: [python] | ||
severity: ERROR | ||
- id: translation-no-anonymous-arguments | ||
patterns: | ||
- pattern: $FUNC("$STRING_ID", ...) | ||
- metavariable-regex: | ||
metavariable: $FUNC | ||
regex: '_|gettext|gettext_lazy|ngettext|ngettext_lazy' | ||
- metavariable-regex: | ||
metavariable: $STRING_ID | ||
regex: ".*%\\w.*" | ||
message: > | ||
Do not use anonymous placeholders for translations. | ||
Use printf style formatting with named placeholders instead. | ||
For example, do `_("Hello %(name)s") % {"name": "Wagtail"}` | ||
instead of `_("Hello %s") % "Wagtail"`. | ||
See https://docs.wagtail.org/en/latest/contributing/translations.html#marking-strings-for-translation for more information. | ||
languages: [python, javascript, typescript] | ||
severity: ERROR | ||
- id: translation-no-format-within-gettext-python | ||
patterns: | ||
- pattern: $FUNC("..." % ..., ...) | ||
- metavariable-regex: | ||
metavariable: $FUNC | ||
regex: '_|gettext|gettext_lazy|ngettext|ngettext_lazy' | ||
message: > | ||
Do not format string before translations | ||
or the interpolated value will be part of the key. | ||
Instead, interpolate after the call to gettext. | ||
For example, do `_("Hello %(name)s") % {"name": "Wagtail"}` | ||
instead of `_("Hello %(name)s" % {"name": "Wagtail"} )`. | ||
See https://docs.wagtail.org/en/latest/contributing/translations.html#marking-strings-for-translation for more information. | ||
languages: [python] | ||
severity: ERROR | ||
- id: translation-no-format-within-gettext-javascript | ||
patterns: | ||
- pattern: $FUNC("...".replace(...), ...) | ||
- metavariable-regex: | ||
metavariable: $FUNC | ||
regex: '_|gettext|gettext_lazy|ngettext|ngettext_lazy' | ||
message: > | ||
Do not format string before translations | ||
or the interpolated value will be part of the key. | ||
Instead, interpolate after the call to gettext. | ||
For example, do `_("Hello %(name)s") % {"name": "Wagtail"}` | ||
instead of `_("Hello %(name)s" % {"name": "Wagtail"} )`. | ||
See https://docs.wagtail.org/en/latest/contributing/translations.html#marking-strings-for-translation for more information. | ||
languages: [javascript, typescript] | ||
severity: ERROR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should really point the developer to a docs page here, hopefully I can get #9431 in and we can have this end docs file as a source of truth here.
The nuances of the various types of string formatting was a bit for me to understand, and there is the temptation to use the 'newer' thing. Having the docs as a place where can explain this a bit better would be useful.
Also, this means adding a docs section also I think as part of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, now that #9431 is merged, I'll rebase my branch
tomorrowFriday and piggyback on it 👍