Merge branch 'master' of github.com:fnp/wolnelektury
[wolnelektury.git] / apps / modeltranslation / management / commands / translation2po.py
1 import polib
2
3 import os
4 import sys
5 import time
6 from optparse import make_option
7 from django.conf import settings
8 from django.core.management.base import BaseCommand
9 from django.core.management.color import color_style
10
11 import modeltranslation.models
12 from modeltranslation.translator import translator, NotRegistered
13
14
15 def metadata(language=''):
16     "get metadata for PO, given language code"
17     t = time.strftime('%Y-%m-%d %H:%M%z')
18
19     return {
20         'Project-Id-Version': '1.0',
21         'Report-Msgid-Bugs-To': 'marcin.koziej@nowoczesnapolska.org.pl',
22         'POT-Creation-Date': '%s' % t,
23         'PO-Revision-Date': '%s' % t,
24         'Last-Translator': 'you <you@example.com>',
25         'Language-Team': '%s <yourteam@example.com>' % language,
26         'MIME-Version': '1.0',
27         'Content-Type': 'text/plain; charset=utf-8',
28         'Content-Transfer-Encoding': '8bit',
29         }
30
31
32 def lang(field_name):
33     "Get the language code from localized field name"
34     return field_name.split('_')[-1]
35
36
37 def make_po(language=''):
38     "Create new POFile object for language code"
39     po = polib.POFile()
40     po.metadata = metadata(language)
41     return po
42
43
44 class Command(BaseCommand):
45     option_list = BaseCommand.option_list + (
46         make_option('-d', '--directory', help='Specify which directory should hold generated PO files', dest='directory'),
47         )
48     help = 'Export models from app to po files'
49     args = 'app'
50
51     def handle(self, appname, **options):
52         app = __import__(appname)
53         pofiles = {}
54         for mdname in dir(app.models):
55             if mdname[0] == '_': continue
56             md = getattr(app.models, mdname)
57             try:
58                 opts = translator.get_options_for_model(md)
59             except NotRegistered:
60                 continue
61
62             for obj in md.objects.all().order_by('pk'):
63                 for fld in opts.fields:
64                     for locfld in opts.localized_fieldnames[fld]:
65                         cur_lang = lang(locfld)
66                         try:
67                             po = pofiles[cur_lang]
68                         except:
69                             po = make_po(cur_lang)
70                             pofiles[cur_lang] = po
71
72                         entry = polib.POEntry(
73                             msgid=getattr(obj, '%s_%s' % (fld, settings.LANGUAGE_CODE)),
74                             msgstr=getattr(obj, locfld),
75                             occurrences=[('%s/%s/%s' % (appname, mdname, locfld), obj.id)])
76                         po.append(entry)
77
78         directory = options['directory']
79         for lng, po in pofiles.items():
80             try: os.makedirs(os.path.join(directory, lng))
81             except OSError: pass
82             po.save(os.path.join(directory, lng, '%s.po' % appname))