4

polib appears to be THE library of choice for working with gettext/po files in Python. The docs show how to iterate through message strings, save po and mo files, etc. However, it's not clear to me, how can one edit a specific entry?

Let's say, I iterate over all messages in an existing po file and display them in an HTML form with textareas. By submitting the form, I get - as an example - the original msgid = "Hello World" and the via textarea translated msgstr = "Hallo Welt"

The original part inside the po file may look like this:

#: .\accounts\forms.py:26 .\accounts\registration\forms.py:48
msgid "Hello World"
msgstr ""

or with fuzzy flag set:

#: .\accounts\forms.py:26 .\accounts\registration\forms.py:48
#, fuzzy
msgid "Hello World"
msgstr "Hallo"

Now how do I update this particular translation in the actual po file? And in case this message was marked as "fuzzy", how do I remove this flag?

Any help appreciated ...

Paolo Melchiorre
  • 5,716
  • 1
  • 33
  • 52
Simon Steinberger
  • 6,605
  • 5
  • 55
  • 97

1 Answers1

10

Ok, after reading through the source code of polib, I found this way to achieve, what I want:

entry = po.find('Email address')
if entry:
    entry.msgstr = 'E-Mail-Adresse'
    if 'fuzzy' in entry.flags:
        entry.flags.remove('fuzzy')

This seems to be the way to go ...

In the case of pluralisation - just as an example:

entry = po.find('%s hour ago')
if entry and entry.msgid_plural:
    entry.msgstr_plural['0'] = 'Vor %s Stunde'
    entry.msgstr_plural['1'] = 'Vor %s Stunden'

The docs of polib should definitively be updated. Otherwise great tool.

Simon Steinberger
  • 6,605
  • 5
  • 55
  • 97
  • just so there is no confusion for someone else - in order to ADD a fuzzy flag you'd write: entry.flags.append('fuzzy') and not entry.flags.add('fuzzy') – Paul von Hoesslin Jul 11 '22 at 12:43