make_dummy.py 2.12 KB
Newer Older
1
#!/usr/bin/env python
Steve Strassmann committed
2 3 4

# Generate test translation files from human-readable po files.
# 
5 6 7 8 9 10
# Dummy language is specified in configuration file (see config.py)
# two letter language codes reference:
# see http://www.loc.gov/standards/iso639-2/php/code_list.php
#
# Django will not localize in languages that django itself has not been
# localized for. So we are using a well-known language (default='fr').
Steve Strassmann committed
11 12 13 14 15 16 17 18
#
# po files can be generated with this:
# django-admin.py makemessages --all --extension html -l en

# Usage:
#
# $ ./make_dummy.py <sourcefile>
#
19
# $ ./make_dummy.py ../conf/locale/en/LC_MESSAGES/django.po
Steve Strassmann committed
20 21
#
# generates output to
22
#    mitx/conf/locale/fr/LC_MESSAGES/django.po
Steve Strassmann committed
23 24

import os, sys
25
import polib
Steve Strassmann committed
26
from dummy import Dummy
27 28
from config import CONFIGURATION
from execute import create_dir_if_necessary
Steve Strassmann committed
29

30
def main(file, locale):
31
    """
Steve Strassmann committed
32
    Takes a source po file, reads it, and writes out a new po file
33
    in :param locale: containing a dummy translation.
34 35 36 37
    """
    if not os.path.exists(file):
        raise IOError('File does not exist: %s' % file)
    pofile = polib.pofile(file)
Steve Strassmann committed
38
    converter = Dummy()
39 40
    converter.init_msgs(pofile.translated_entries())
    for msg in pofile:
Steve Strassmann committed
41
        converter.convert_msg(msg)
42
    new_file = new_filename(file, locale)
Steve Strassmann committed
43
    create_dir_if_necessary(new_file)
44
    pofile.save(new_file)
Steve Strassmann committed
45

46 47
def new_filename(original_filename, new_locale):
    """Returns a filename derived from original_filename, using new_locale as the locale"""
Steve Strassmann committed
48 49 50
    orig_dir = os.path.dirname(original_filename)
    msgs_dir = os.path.basename(orig_dir)
    orig_file = os.path.basename(original_filename)
51 52 53 54 55
    return os.path.abspath(os.path.join(orig_dir,
                                        '../..',
                                        new_locale,
                                        msgs_dir,
                                        orig_file))
Steve Strassmann committed
56 57

if __name__ == '__main__':
58
    # required arg: file
Steve Strassmann committed
59 60
    if len(sys.argv)<2:
        raise Exception("missing file argument")
61
    # optional arg: locale
62
    if len(sys.argv)<3:
63
        locale = CONFIGURATION.get_dummy_locale()
64 65 66
    else:
        locale = sys.argv[2]
    main(sys.argv[1], locale)