transifex.py 2.02 KB
Newer Older
1
#!/usr/bin/env python
2

3 4 5 6
import os, sys
from polib import pofile
from config import CONFIGURATION
from extract import SOURCE_WARN
7 8
from execute import execute

9 10 11
TRANSIFEX_HEADER = 'Translations in this file have been downloaded from %s'
TRANSIFEX_URL = 'https://www.transifex.com/projects/p/edx-studio/'

12 13 14 15
def push():
    execute('tx push -s')

def pull():
16 17
    for locale in CONFIGURATION.locales:
        if locale != CONFIGURATION.source_locale:
18
            print "Pulling %s from transifex..." % locale
19
            execute('tx pull -l %s' % locale)
20 21 22 23 24 25 26 27
    clean_translated_locales()


def clean_translated_locales():
    """
    Strips out the warning from all translated po files
    about being an English source file.
    """
28 29
    for locale in CONFIGURATION.locales:
        if locale != CONFIGURATION.source_locale:
30
            clean_locale(locale)
31

32 33 34 35 36 37 38 39
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)
    for filename in ('django-partial.po', 'djangojs.po', 'mako.po'):
40
        clean_file(dirname.joinpath(filename))
41 42 43 44 45 46 47 48 49 50 51 52

def clean_file(file):
    """
    Strips out the warning from a translated po file about being an English source file.
    Replaces warning with a note about coming from Transifex.
    """
    po = pofile(file)
    if po.header.find(SOURCE_WARN) != -1:
        new_header = get_new_header(po)
        new = po.header.replace(SOURCE_WARN, new_header)
        po.header = new
        po.save()
53

54 55 56 57 58 59
def get_new_header(po):
    team = po.metadata.get('Language-Team', None)
    if not team:
        return TRANSIFEX_HEADER % TRANSIFEX_URL
    else:
        return TRANSIFEX_HEADER % team
60 61

if __name__ == '__main__':
62
    if len(sys.argv) < 2:
63 64 65 66 67 68 69 70
        raise Exception("missing argument: push or pull")
    arg = sys.argv[1]
    if arg == 'push':
        push()
    elif arg == 'pull':
        pull()
    else:
        raise Exception("unknown argument: (%s)" % arg)