make_dummy.py 1.91 KB
Newer Older
Steve Strassmann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/python

# Generate test translation files from human-readable po files.
# 
#
# po files can be generated with this:
# django-admin.py makemessages --all --extension html -l en

# Usage:
#
# $ ./make_dummy.py <sourcefile>
#
# $ ./make_dummy.py mitx/conf/locale/en/LC_MESSAGES/django.po
#
# generates output to
#    mitx/conf/locale/vr/LC_MESSAGES/django.po

import os, sys
19
import polib
Steve Strassmann committed
20
from dummy import Dummy
21
from execute import create_dir_if_necessary
Steve Strassmann committed
22

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

39 40
def new_filename(original_filename, new_locale):
    """Returns a filename derived from original_filename, using new_locale as the locale"""
Steve Strassmann committed
41 42 43
    orig_dir = os.path.dirname(original_filename)
    msgs_dir = os.path.basename(orig_dir)
    orig_file = os.path.basename(original_filename)
44 45 46 47 48
    return os.path.join(orig_dir,
                        '/../..',
                        new_locale,
                        msgs_dir,
                        orig_file)
Steve Strassmann committed
49 50


51 52 53 54 55 56 57 58
# Dummy language 
# 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: 'fr'.

DEFAULT_LOCALE = 'fr'
Steve Strassmann committed
59 60 61 62

if __name__ == '__main__':
    if len(sys.argv)<2:
        raise Exception("missing file argument")
63 64 65 66 67
    if len(sys.argv)<2:
        locale = DEFAULT_LOCALE
    else:
        locale = sys.argv[2]
    main(sys.argv[1], locale)