Skip to content

Commit

Permalink
Only save catalog if things changed
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Jul 8, 2024
1 parent ac4c901 commit 3d5d1a3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
23 changes: 15 additions & 8 deletions projects/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __init__(self, *args, **kwargs):
)
self.entry_rows[-1]["msgstr"].append(self[name])

def update(self, po, *, request):
def update(self, catalog, *, request):
updates = 0

for index in range(ENTRIES_PER_PAGE):
Expand All @@ -97,7 +97,7 @@ def update(self, po, *, request):
# Better be safe than sorry -- do not modify the entries in
# self.entries, find the entry in the current version of the pofile
# instead.
for entry in po:
for entry in catalog.po:
if entry.msgid_with_context == msgid_with_context:
old = copy.deepcopy(entry)
entry.msgstr = translators.fix_nls(entry.msgid, msgstr)
Expand All @@ -109,21 +109,24 @@ def update(self, po, *, request):
)
if fuzzy and not entry.fuzzy:
entry.fuzzy = True
if not fuzzy and entry.fuzzy:
updates += 1
elif not fuzzy and entry.fuzzy:
entry.fuzzy = False

if old != entry:
updates += 1
elif old != entry:
updates += 1
break

if updates:
po.metadata["Last-Translator"] = "{} {} <{}>".format(
catalog.po.metadata["Last-Translator"] = "{} {} <{}>".format(
getattr(request.user, "first_name", "Anonymous"),
getattr(request.user, "last_name", "User"),
getattr(request.user, "email", "[email protected]"),
)
po.metadata["X-Translated-Using"] = "traduire 0.0.1"
po.metadata["PO-Revision-Date"] = localtime().strftime("%Y-%m-%d %H:%M%z")
catalog.po.metadata["X-Translated-Using"] = "traduire 0.0.1"
catalog.po.metadata["PO-Revision-Date"] = localtime().strftime(
"%Y-%m-%d %H:%M%z"
)

messages.success(
request,
Expand All @@ -133,6 +136,10 @@ def update(self, po, *, request):
updates,
).format(count=updates),
)

catalog.pofile = str(catalog.po)
catalog.save()

else:
messages.info(request, _("No changes detected."))

Expand Down
4 changes: 1 addition & 3 deletions projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ def catalog(request, project, language_code, domain):
form = EntriesForm(*data, entries=entries, language_code=language_code)

if form.is_valid():
form.update(catalog.po, request=request)
catalog.pofile = str(catalog.po)
catalog.save()
form.update(catalog, request=request)

return http.HttpResponseRedirect(
query_string(
Expand Down

0 comments on commit 3d5d1a3

Please sign in to comment.