Skip to content

Instantly share code, notes, and snippets.

@rixx
Last active June 11, 2025 08:34
Show Gist options
  • Save rixx/9f5e93d8d286f683f0b7efc7edd82961 to your computer and use it in GitHub Desktop.
Save rixx/9f5e93d8d286f683f0b7efc7edd82961 to your computer and use it in GitHub Desktop.
polib example for gnu gettext pofile editing
def defuzzy(message):
import pathlib, polib
pofiles = list(pathlib.Path(".").glob("pretalx/**/**/django.po"))
for path in pofiles:
po = polib.pofile(path)
entry = po.find(message)
if entry and entry.fuzzy:
entry.flags.remove("fuzzy")
entry.previous_msgid = None
po.save()
def rename_source_string(message, new_message):
import pathlib, polib
pofiles = list(pathlib.Path(".").glob("pretalx/**/**/django.po"))
for path in pofiles:
po = polib.pofile(path)
entry = po.find(message)
if entry:
entry.msgid = new_message
po.save()
def replace_in_source_string(message, replacements):
"""
Replace strings in source and translated strings, e.g. markup or variable names.
replace_in_source_string(
"<b>Important</b> message",
[("<b>", "<strong>"), ("</b>", "</strong>")]
)
"""
import pathlib, polib
pofiles = list(pathlib.Path(".").glob("pretalx/**/**/django.po"))
for path in pofiles:
po = polib.pofile(path)
entry = po.find(message)
if entry:
for (a, b) in replacements:
entry.msgid = entry.msgid.replace(a, b)
if entry.msgstr:
entry.msgstr = entry.msgstr.replace(a, b)
po.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment