File: update_catalogs.py

package info (click to toggle)
python-django 3%3A4.2.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 58,592 kB
  • sloc: python: 334,729; javascript: 18,754; xml: 215; makefile: 178; sh: 27
file content (60 lines) | stat: -rwxr-xr-x 1,796 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/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.
"""

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():
    """Run makemessages and compilemessages in sampleproject."""
    from django.core.management import call_command

    prev_cwd = os.getcwd()

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

    # keep the diff friendly - remove 'POT-Creation-Date'
    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()