Skip to content

Commit 5e2c716

Browse files
committed
Add convenient 'remove-django-translators' hook
1 parent 9305d68 commit 5e2c716

6 files changed

Lines changed: 53 additions & 19 deletions

File tree

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[bumpversion]
2-
current_version = 1.3.0
2+
current_version = 1.3.1
33

44
[bumpversion:file:setup.cfg]

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
description: Checks for untranslated messages in PO files
1111
files: \.po$
1212
language: python
13+
- id: remove-django-translators
14+
name: remove-django-translators
15+
entry: lreplace-extracted-comments-hook --django-translators
16+
description: Removes the "Translator" string prepended by Django extracting messages con xgettext
17+
files: \.po$
18+
language: python
1319
- id: lreplace-extracted-comments
1420
name: lreplace-extracted-comments
1521
entry: lreplace-extracted-comments-hook

.yamllint

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ignore: |
22
venv
33
build
4+
.pre-commit-hooks.yaml
45

56
rules:
67
braces:

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ Hooks for pre-commit useful working with PO files.
1515
hooks:
1616
- id: obsolete-messages
1717
- id: untranslated-messages
18-
- id: lreplace-extracted-comments
19-
args:
20-
- -m
21-
- 'Translators: '
22-
- -r
23-
- ''
18+
- id: remove-django-translators
2419
- id: check-metadata
2520
args:
2621
- -h
@@ -41,9 +36,7 @@ Checks for untranslated messages printing their line numbers if found.
4136

4237
### **`lreplace-extracted-comments`**
4338

44-
Replaces a matching string at the beginning of the extracted comments.
45-
This can be used to remove the string "Translators: " introduced by Django (see
46-
more about this problem in [django-rosetta#245][django-rosetta-lstrip]).
39+
Replaces a matching string at the beginning of extracted comments.
4740

4841
#### Parameters
4942

@@ -54,6 +47,13 @@ more about this problem in [django-rosetta#245][django-rosetta-lstrip]).
5447
- `-d/--dry-run`: Don't do the replacements, only writes to stderr the locations
5548
of the extracted comments to be replaced.
5649

50+
### **`remove-django-translators`**
51+
52+
Same as [`lreplace-extracted-comments`][lreplace-extracted-comments-link]
53+
passing `--match "Translators: " --replacement ""`. Useful to remove the string
54+
prepended by Django extracting messages with gettext (see more about this
55+
problem in [django-rosetta#245][django-rosetta-lstrip]).
56+
5757
### **`check-metadata`**
5858
5959
Check that metadata fields matches a set of regular expressions.
@@ -78,4 +78,5 @@ Check that metadata fields matches a set of regular expressions.
7878
[tests-image]: https://img.shields.io/github/workflow/status/mondeja/pre-commit-po-hooks/CI?logo=github&label=tests
7979
[tests-link]: https://github.com/mondeja/pre-commit-po-hooks/actions?query=workflow%CI
8080
81+
[lreplace-extracted-comments-link]: https://github.com/mondeja/pre-commit-po-hooks#lreplace-extracted-comments
8182
[django-rosetta-lstrip]: https://github.com/mbi/django-rosetta/pull/245

hooks/lreplace_extracted_comments.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717

1818
def lreplace_extracted_comments(
19-
filenames, match, replacement, dry_run=False, quiet=False
19+
filenames,
20+
match=None,
21+
replacement=None,
22+
django_translators=False,
23+
dry_run=False,
24+
quiet=False,
2025
):
2126
"""Replace the beginning of the extracted comments which starts with the
2227
string passed in the argument ``match``.
@@ -28,12 +33,16 @@ def lreplace_extracted_comments(
2833
filenames : list
2934
Set of file names to replace.
3035
31-
match : str
36+
match : str, optional
3237
The start of the extracted comment content to be replaced.
3338
34-
replacement : str
39+
replacement : str, optional
3540
The replacement for the beginning of the matching extracted comments.
3641
42+
django_translators : bool, optional
43+
Convenient parameter to pass ``Translators: `` as ``-m`` parameter and
44+
an empty string as replacement.
45+
3746
dry_run : bool, optional
3847
Don't do the replacements, just writes to stderr the location of them.
3948
You can use it with ``quiet=True`` to check if the strings has been
@@ -48,7 +57,14 @@ def lreplace_extracted_comments(
4857
4958
int: 0 if no a extracted comment has been replaced, 1 otherwise.
5059
"""
51-
regex = re.compile(rf"^#\.\s{re.escape(match)}")
60+
if not django_translators and not match and not replacement:
61+
raise ValueError("You need to specify a match and replacement")
62+
63+
if django_translators:
64+
regex = re.compile(r"^#\.\sTranslators:\s")
65+
replacement = ""
66+
else:
67+
regex = re.compile(rf"^#\.\s{re.escape(match)}")
5268

5369
exitcode = 0
5470
for filename in filenames:
@@ -105,7 +121,7 @@ def main():
105121
"-m",
106122
"--match",
107123
dest="match",
108-
required=True,
124+
required=False,
109125
metavar="MATCH",
110126
help="The string to match at the beginning of the extracted comments to"
111127
" replace it.",
@@ -114,16 +130,26 @@ def main():
114130
"-r",
115131
"--replacement",
116132
dest="replacement",
117-
required=True,
133+
required=False,
118134
metavar="REPL",
119135
help="The substitution to be used to replace the matching string.",
120136
)
137+
parser.add_argument(
138+
"--django-translators",
139+
action="store_true",
140+
dest="django_translators",
141+
required=False,
142+
help=(
143+
"Pass 'Translators: ' as '-m' argument and an empty string as replacement."
144+
),
145+
)
121146
args = parser.parse_args()
122147

123148
return lreplace_extracted_comments(
124149
args.filenames,
125-
args.match,
126-
args.replacement,
150+
match=args.match,
151+
replacement=args.replacement,
152+
django_translators=args.django_translators,
127153
dry_run=args.dry_run,
128154
quiet=args.quiet,
129155
)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = pre_commit_po_hooks
3-
version = 1.3.0
3+
version = 1.3.1
44
description = pre-commit hooks for PO files
55
long_description = file: README.md
66
long_description_content_type = text/markdown

0 commit comments

Comments
 (0)