1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from optparse import make_option
8 from django.conf import settings
9 from django.core.management.base import BaseCommand
10 from django.db import models
13 from modeltranslation.translator import translator, NotRegistered
15 from wolnelektury.utils import makedirs
18 def metadata(language=''):
19 """get metadata for PO, given language code"""
20 t = time.strftime('%Y-%m-%d %H:%M%z')
23 'Project-Id-Version': '1.0',
24 'Report-Msgid-Bugs-To': settings.CONTACT_EMAIL,
25 'POT-Creation-Date': '%s' % t,
26 'PO-Revision-Date': '%s' % t,
27 'Last-Translator': 'you <you@example.com>',
28 'Language-Team': '%s' % dict(settings.LANGUAGES).get(language, language),
29 'MIME-Version': '1.0',
30 'Content-Type': 'text/plain; charset=utf-8',
31 'Content-Transfer-Encoding': '8bit',
35 def make_po(language=''):
36 """Create new POFile object for language code"""
38 po.metadata = metadata(language)
42 def get_languages(langs):
44 return settings.LANGUAGES
45 langs = langs.split(',')
46 lm = dict(settings.LANGUAGES)
47 return map(lambda l: (l, lm.get(l, l)), langs)
50 class Command(BaseCommand):
51 option_list = BaseCommand.option_list + (
52 make_option('-d', '--directory', help='Specify which directory should hold generated PO files',
53 dest='directory', default=''),
54 make_option('-l', '--load', help='load locales back to source', action='store_true', dest='load',
56 make_option('-L', '--language', help='locales to load', dest='lang', default=None),
57 make_option('-n', '--poname', help='name of the po file [no extension]', dest='poname', default=None),
58 make_option('-k', '--keep-running', help='keep running even when missing an input file', dest='keep_running',
59 default=False, action='store_true'),
61 help = 'Export models from app to po files'
64 def get_models(self, app):
65 for mdname in dir(app.models):
68 md = getattr(app.models, mdname)
70 assert issubclass(md, models.Model)
71 except (AssertionError, TypeError):
75 opts = translator.get_options_for_model(md)
81 def handle(self, appname, **options):
82 if not options['poname']:
83 options['poname'] = appname
84 app = __import__(appname)
89 for md, opts in self.get_models(app):
90 if md.__name__ not in objects:
91 objects[md.__name__] = {}
94 languages = get_languages(options['lang'])
96 for lng in zip(*languages)[0]:
97 pofile = os.path.join(options['directory'], lng, options['poname'] + '.po')
98 if not os.path.exists(pofile):
99 if options['keep_running']:
102 raise OSError('%s po file: %s not found' % (appname, pofile))
103 po = polib.pofile(pofile)
105 loc, _ignored = entry.occurrences[0]
106 _appname, modelname, fieldname, pk = loc.split('/')
108 obj = objects[modelname][pk]
110 obj = modmod['model'].objects.get(pk=pk)
111 objects[modelname][pk] = obj
112 setattr(obj, fieldname, entry.msgstr)
114 for mod, objcs in objects.items():
115 for o in objcs.values():
120 for md, opts in self.get_models(app):
121 for obj in md.objects.all().order_by('pk'):
122 for fld, locflds in opts.local_fields.items():
123 k = getattr(obj, '%s_%s' % (fld, settings.LANGUAGE_CODE)) or ''
124 for locfld in locflds:
125 cur_lang = locfld.language
127 po = pofiles[cur_lang]
129 po = make_po(cur_lang)
130 pofiles[cur_lang] = po
131 v = locfld.value_from_object(obj) or ''
132 entry = polib.POEntry(
135 occurrences=[('%s/%s/%s/%s' % (appname, md.__name__, locfld.name, str(obj.pk)), 0)])
138 directory = options['directory']
139 for lng, po in pofiles.items():
140 makedirs(os.path.join(directory, lng))
141 po.save(os.path.join(directory, lng, '%s.po' % options['poname']))