Skip to content

Instantly share code, notes, and snippets.

@xitij2000
Created October 7, 2019 09:37
Show Gist options
  • Save xitij2000/84cad39f0e826405d51a892b0d7f56ef to your computer and use it in GitHub Desktop.
Save xitij2000/84cad39f0e826405d51a892b0d7f56ef to your computer and use it in GitHub Desktop.
from pathlib import Path
from babel.messages import pofile
from babel.messages.catalog import Catalog
missing = Catalog("ar")
changed = Catalog("ar")
added = Catalog("ar")
asu_django = pofile.read_po(Path("asu/django.po").open())
edx_django = pofile.read_po(Path("edx/django.po").open())
edx_dict = {message.id: message.string for message in edx_django}
def add_to_catalog(catalog, message):
catalog.add(
message.id,
message.string,
message.locations,
message.flags,
message.auto_comments,
message.user_comments,
message.previous_id,
message.lineno,
message.context,
)
for message in asu_django:
# Just skip blank translations
if not message.string:
continue
if message.id in edx_dict:
edx_msg = edx_dict[message.id]
if not any(edx_msg):
# edX doesn't have this translation at all
add_to_catalog(missing, message)
elif edx_msg != message.string:
# edX has this translation, but it is different
print("Conflicting translation", message.id)
print("edx version", edx_msg)
print("asu version", message.string)
add_to_catalog(changed, message)
else:
# edX this translation and it is the same as ASU, skip
continue
else:
# edx doesn't even have this message id, so it's probably for a
# string only used by ASU's branch
add_to_catalog(added, message)
pofile.write_po(Path("missing_django.po").open("wb"), missing)
pofile.write_po(Path("changed_django.po").open("wb"), changed)
pofile.write_po(Path("added_django.po").open("wb"), added)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment