summaryrefslogtreecommitdiffstats
path: root/askbot/bin/mergelocales.py
blob: 2c49cb254b29b6b87a44ddfbc5842c5b47f9358e (plain)
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
61
import os
import sys
import shutil
import subprocess

DIR1 = sys.argv[1]
DIR2 = sys.argv[2]
DEST_DIR = sys.argv[3]

def get_locale_list(path):
    """return names of directories within a locale dir"""
    items = os.listdir(path)
    result = list()
    for item in items:
        if os.path.isdir(os.path.join(path, item)):
            result.append(item)
    return result

def copy_locale_from(localeno, name = None):
    """copy entire locale without merging"""
    if localeno == 1:
        src = os.path.join(DIR1, name)
    elif localeno == 2:
        src = os.path.join(DIR2, name)
    shutil.copytree(src, os.path.join(DEST_DIR, name))

def merge_locales(name):
    """runs msgcat command on specified files
    and a locale name in DIR1 and DIR2"""
    run_msgcat(name, 'django.po')
    run_msgcat(name, 'djangojs.po')

def run_msgcat(locale_name, file_name):
    """run msgcat in locale on file name"""
    file_path = os.path.join(locale_name, 'LC_MESSAGES', file_name)
    dest_file = os.path.join(DEST_DIR, file_path)
    dest_dir = os.path.dirname(dest_file)
    if not os.path.exists(dest_dir):
        os.makedirs(os.path.dirname(dest_file))
    subprocess.call((
        'msgcat',
        os.path.join(DIR1, file_path),
        os.path.join(DIR2, file_path),
        '-o',
        dest_file
    ))

LOCALE_LIST1 = get_locale_list(DIR1)
LOCALE_LIST2 = get_locale_list(DIR2)

for locale in LOCALE_LIST1:
    print locale
    if locale not in LOCALE_LIST2:
        copy_locale_from(1, name = locale)
    else:
        merge_locales(locale)
        LOCALE_LIST2.remove(locale)

for locale in LOCALE_LIST2:
    print locale
    copy_locale_from(2, name = locale)