transifex.py 2.53 KB
Newer Older
1
#!/usr/bin/env python
2
from __future__ import print_function
3
import sys
4
from polib import pofile
5
import argparse
6 7 8

from i18n.config import CONFIGURATION
from i18n.execute import execute
9
from i18n.extract import EDX_MARKER
10

11
TRANSIFEX_HEADER = u'edX community translations have been downloaded from {}'
12
TRANSIFEX_URL = 'https://www.transifex.com/projects/p/edx-platform/'
13

14

15 16 17
def push():
    execute('tx push -s')

18

19
def pull():
20
    print("Pulling languages from transifex...")
21 22
    # Pull translations from all languages where there is
    # at least 10% reviewed translations
23
    execute('tx pull --mode=reviewed --all')
24 25 26 27 28 29 30 31
    clean_translated_locales()


def clean_translated_locales():
    """
    Strips out the warning from all translated po files
    about being an English source file.
    """
32 33 34
    for locale in CONFIGURATION.translated_locales:
        clean_locale(locale)

35

36 37 38 39 40 41 42
def clean_locale(locale):
    """
    Strips out the warning from all of a locale's translated po files
    about being an English source file.
    Iterates over machine-generated files.
    """
    dirname = CONFIGURATION.get_messages_dir(locale)
43
    for filename in ('django-partial.po', 'djangojs-partial.po', 'mako.po'):
44
        clean_file(dirname.joinpath(filename))
45

46 47

def clean_file(filename):
48 49 50 51
    """
    Strips out the warning from a translated po file about being an English source file.
    Replaces warning with a note about coming from Transifex.
    """
52 53 54 55 56
    try:
        po = pofile(filename)
    except Exception as exc:
        # An exception can occur when a language is deleted from Transifex.
        # Don't totally fail here.
57
        print("Encountered error {} with filename {} - language project may no longer exist on Transifex".format(exc, filename))
58
        return
59
    if po.header.find(EDX_MARKER) != -1:
60
        new_header = get_new_header(po)
61
        new = po.header.replace(EDX_MARKER, new_header)
62 63
        po.header = new
        po.save()
64

65

66 67 68
def get_new_header(po):
    team = po.metadata.get('Language-Team', None)
    if not team:
69
        return TRANSIFEX_HEADER.format(TRANSIFEX_URL)
70
    else:
71
        return TRANSIFEX_HEADER.format(team)
72

73

74
if __name__ == '__main__':
David Baumgold committed
75
    # pylint: disable=invalid-name
76 77 78 79
    parser = argparse.ArgumentParser()
    parser.add_argument("command", help="push or pull")
    parser.add_argument("--verbose", "-v")
    args = parser.parse_args()
David Baumgold committed
80
    # pylint: enable=invalid-name
81 82

    if args.command == "push":
83
        push()
84
    elif args.command == "pull":
85 86
        pull()
    else:
87
        raise Exception("unknown command ({cmd})".format(cmd=args.command))