update_catalogs.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python
  2. """
  3. Helper script to update sampleproject's translation catalogs.
  4. When a bug has been identified related to i18n, this helps capture the issue
  5. by using catalogs created from management commands.
  6. Example:
  7. The string "Two %% Three %%%" renders differently using translate and
  8. blocktranslate. This issue is difficult to debug, it could be a problem with
  9. extraction, interpolation, or both.
  10. How this script helps:
  11. * Add {% translate "Two %% Three %%%" %} and blocktranslate equivalent to
  12. templates.
  13. * Run this script.
  14. * Test extraction - verify the new msgid in sampleproject's django.po.
  15. * Add a translation to sampleproject's django.po.
  16. * Run this script.
  17. * Test interpolation - verify templatetag rendering, test each in a template
  18. that is rendered using an activated language from sampleproject's locale.
  19. * Tests should fail, issue captured.
  20. * Fix issue.
  21. * Run this script.
  22. * Tests all pass.
  23. """
  24. import os
  25. import re
  26. import sys
  27. proj_dir = os.path.dirname(os.path.abspath(__file__))
  28. sys.path.append(os.path.abspath(os.path.join(proj_dir, '..', '..', '..')))
  29. def update_translation_catalogs():
  30. """Run makemessages and compilemessages in sampleproject."""
  31. from django.core.management import call_command
  32. prev_cwd = os.getcwd()
  33. os.chdir(proj_dir)
  34. call_command('makemessages')
  35. call_command('compilemessages')
  36. # keep the diff friendly - remove 'POT-Creation-Date'
  37. pofile = os.path.join(proj_dir, 'locale', 'fr', 'LC_MESSAGES', 'django.po')
  38. with open(pofile) as f:
  39. content = f.read()
  40. content = re.sub(r'^"POT-Creation-Date.+$\s', '', content, flags=re.MULTILINE)
  41. with open(pofile, 'w') as f:
  42. f.write(content)
  43. os.chdir(prev_cwd)
  44. if __name__ == "__main__":
  45. update_translation_catalogs()