Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify/update an entry in a po file with polib in Python

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 ...

like image 560
Simon Steinberger Avatar asked Feb 18 '23 19:02

Simon Steinberger


1 Answers

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.

like image 138
Simon Steinberger Avatar answered Feb 20 '23 11:02

Simon Steinberger