+ return r
+
+ def handle(self, appname, **options):
+ app = __import__(appname)
+ if options['load']:
+ objects = {}
+ modmod = {}
+ for md, opts in self.get_models(app):
+ if not md.__name__ in objects:
+ objects[md.__name__] = {}
+ modmod['model'] = md
+
+ for lng in zip(*settings.LANGUAGES)[0]:
+ pofile = os.path.join(options['directory'], lng, appname + '.po')
+ po = polib.pofile(pofile)
+ for entry in po:
+ loc, pk = entry.occurrences[0]
+ _appname, modelname, fieldname = loc.split('/')
+ try:
+ obj = objects[modelname][pk]
+ except KeyError:
+ obj = modmod['model'].objects.get(pk=pk)
+ objects[modelname][pk] = obj
+ setattr(obj, fieldname, entry.msgstr)
+
+ for mod, objcs in objects.items():
+ for o in objcs.values():
+ o.save()
+
+ else:
+ pofiles = {}
+ for md, opts in self.get_models(app):
+ for obj in md.objects.all().order_by('pk'):
+ for fld in opts.fields:
+ for locfld in opts.localized_fieldnames[fld]:
+ cur_lang = lang(locfld)
+ try:
+ po = pofiles[cur_lang]
+ except:
+ po = make_po(cur_lang)
+ pofiles[cur_lang] = po
+
+ entry = polib.POEntry(
+ msgid=getattr(obj, '%s_%s' % (fld, settings.LANGUAGE_CODE)),
+ msgstr=getattr(obj, locfld),
+ occurrences=[('%s/%s/%s' % (appname, md.__name__, locfld), obj.id)])
+ po.append(entry)