Debian Codegraph @

tests/i18n/sampleproject/update_catalogs.py

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