usrbinenv python helper script to update sampleproject s translation catalogs when a bug has been identified related to i18n this helps capture the issue by using catalogs created from management commands example the string two three renders differently using translate and blocktranslate this issue is difficult to debug it could be a problem with extraction interpolation or both how this script helps add translate two three and blocktranslate equivalent to templates run this script test extraction verify the new msgid in sampleproject s django po add a translation to sampleproject s django po run this script test interpolation verify templatetag rendering test each in a template that is rendered using an activated language from sampleproject s locale tests should fail issue captured fix issue run this script tests all pass run makemessages and compilemessages in sampleproject from django core management import callcommand prevcwd os getcwd os chdirprojdir callcommandmakemessages callcommandcompilemessages keep the diff friendly remove potcreationdate pofile os path joinprojdir locale fr lcmessages django po with openpofile as f content f read content re subr potcreationdate s content flagsre multiline with openpofile w as f f writecontent os chdirprevcwd if name main updatetranslationcatalogs usr bin env python helper script to update sampleproject s translation catalogs when a bug has been identified related to i18n this helps capture the issue by using catalogs created from management commands example the string two three renders differently using translate and blocktranslate this issue is difficult to debug it could be a problem with extraction interpolation or both how this script helps add translate two three and blocktranslate equivalent to templates run this script test extraction verify the new msgid in sampleproject s django po add a translation to sampleproject s django po run this script test interpolation verify templatetag rendering test each in a template that is rendered using an activated language from sampleproject s locale tests should fail issue captured fix issue run this script tests all pass run makemessages and compilemessages in sampleproject keep the diff friendly remove pot creation date
import os
import re
import sys

proj_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.abspath(os.path.join(proj_dir, "..", "..", "..")))


def update_translation_catalogs():
    
    from django.core.management import call_command

    prev_cwd = os.getcwd()

    os.chdir(proj_dir)
    call_command("makemessages")
    call_command("compilemessages")

    
    pofile = os.path.join(proj_dir, "locale", "fr", "LC_MESSAGES", "django.po")

    with open(pofile) as f:
        content = f.read()
    content = re.sub(r'^"POT-Creation-Date.+$\s', "", content, flags=re.MULTILINE)
    with open(pofile, "w") as f:
        f.write(content)

    os.chdir(prev_cwd)


if __name__ == "__main__":
    update_translation_catalogs()