Add pz field
[wolnelektury.git] / src / wolnelektury / management / commands / translation2po.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 import os
5 import time
6 from django.conf import settings
7 from django.core.management.base import BaseCommand
8 from django.db import models
9 import polib
10 from modeltranslation.translator import translator, NotRegistered
11 from wolnelektury.utils import makedirs
12
13
14 def metadata(language=''):
15     """get metadata for PO, given language code"""
16     t = time.strftime('%Y-%m-%d %H:%M%z')
17
18     return {
19         'Project-Id-Version': '1.0',
20         'Report-Msgid-Bugs-To': settings.CONTACT_EMAIL,
21         'POT-Creation-Date': '%s' % t,
22         'PO-Revision-Date': '%s' % t,
23         'Last-Translator': 'you <you@example.com>',
24         'Language-Team': '%s' % dict(settings.LANGUAGES).get(language, language),
25         'MIME-Version': '1.0',
26         'Content-Type': 'text/plain; charset=utf-8',
27         'Content-Transfer-Encoding': '8bit',
28         }
29
30
31 def make_po(language=''):
32     """Create new POFile object for language code"""
33     po = polib.POFile()
34     po.metadata = metadata(language)
35     return po
36
37
38 def get_languages(langs):
39     if not langs:
40         return settings.LANGUAGES
41     langs = langs.split(',')
42     lm = dict(settings.LANGUAGES)
43     return map(lambda l: (l, lm.get(l, l)), langs)
44
45
46 class Command(BaseCommand):
47     help = 'Export models from app to po files'
48
49     def add_arguments(self, parser):
50         parser.add_argument(
51                 '-d', '--directory', dest='directory', default='',
52                 help='Specify which directory should hold generated PO files')
53         parser.add_argument(
54                 '-l', '--load', help='load locales back to source',
55                 action='store_true', dest='load', default=False)
56         parser.add_argument(
57                 '-L', '--language', help='locales to load',
58                 dest='lang', default=None)
59         parser.add_argument(
60                 '-n', '--poname', help='name of the po file [no extension]',
61                 dest='poname', default=None)
62         parser.add_argument(
63                 '-k', '--keep-running', dest='keep_running',
64                 help='keep running even when missing an input file',
65                 default=False, action='store_true')
66         parser.add_argument('app')
67
68     def get_models(self, app):
69         for mdname in dir(app.models):
70             if mdname[0] == '_':
71                 continue
72             md = getattr(app.models, mdname)
73             try:
74                 assert issubclass(md, models.Model)
75             except (AssertionError, TypeError):
76                 continue
77
78             try:
79                 opts = translator.get_options_for_model(md)
80             except NotRegistered:
81                 continue
82             else:
83                 yield (md, opts)
84
85     def handle(self, **options):
86         appname = options['app']
87         if not options['poname']:
88             options['poname'] = appname
89         app = __import__(appname)
90
91         if options['load']:
92             objects = {}
93             modmod = {}
94             for md, opts in self.get_models(app):
95                 if md.__name__ not in objects:
96                     objects[md.__name__] = {}
97                     modmod['model'] = md
98
99             languages = get_languages(options['lang'])
100
101             for lng in zip(*languages)[0]:
102                 pofile = os.path.join(options['directory'], lng, options['poname'] + '.po')
103                 if not os.path.exists(pofile):
104                     if options['keep_running']:
105                         continue
106                     else:
107                         raise OSError('%s po file: %s not found' % (appname, pofile))
108                 po = polib.pofile(pofile)
109                 for entry in po:
110                     loc, _ignored = entry.occurrences[0]
111                     _appname, modelname, fieldname, pk = loc.split('/')
112                     try:
113                         obj = objects[modelname][pk]
114                     except KeyError:
115                         obj = modmod['model'].objects.get(pk=pk)
116                         objects[modelname][pk] = obj
117                     setattr(obj, fieldname, entry.msgstr)
118
119             for mod, objcs in objects.items():
120                 for o in objcs.values():
121                     o.save()
122
123         else:
124             pofiles = {}
125             for md, opts in self.get_models(app):
126                 for obj in md.objects.all().order_by('pk'):
127                     for fld, locflds in opts.local_fields.items():
128                         k = getattr(obj, '%s_%s' % (fld, settings.LANGUAGE_CODE)) or ''
129                         for locfld in locflds:
130                             cur_lang = locfld.language
131                             try:
132                                 po = pofiles[cur_lang]
133                             except KeyError:
134                                 po = make_po(cur_lang)
135                                 pofiles[cur_lang] = po
136                             v = locfld.value_from_object(obj) or ''
137                             entry = polib.POEntry(
138                                 msgid=k,
139                                 msgstr=v,
140                                 occurrences=[('%s/%s/%s/%s' % (appname, md.__name__, locfld.name, str(obj.pk)), 0)])
141                             po.append(entry)
142
143             directory = options['directory']
144             for lng, po in pofiles.items():
145                 makedirs(os.path.join(directory, lng))
146                 po.save(os.path.join(directory, lng, '%s.po' % options['poname']))