transifex.py 1.98 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 18
    for locale in CONFIGURATION.locales:
        if locale != CONFIGURATION.source_locale:
            execute('tx pull -l %s' % locale)
19 20 21 22 23 24 25 26
    clean_translated_locales()


def clean_translated_locales():
    """
    Strips out the warning from all translated po files
    about being an English source file.
    """
27 28
    for locale in CONFIGURATION.locales:
        if locale != CONFIGURATION.source_locale:
29 30 31 32 33 34 35 36 37 38
            clean_locale(locale)
        
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'):
39
        clean_file(dirname.joinpath(filename))
40 41 42 43 44 45 46 47 48 49 50 51

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()
52

53 54 55 56 57 58
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
59 60 61 62 63 64 65 66 67 68 69 70

if __name__ == '__main__':
    if len(sys.argv)<2:
        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)