+ return r
+
+ def handle(self, appname, **options):
+ if not options['poname']: options['poname'] = appname
+ 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
+
+ languages = get_languages(options['lang'])
+
+ for lng in zip(*languages)[0]:
+ pofile = os.path.join(options['directory'], lng, options['poname'] + '.po')
+ if not os.path.exists(pofile):
+ if options['keep_running']:
+ continue
+ else:
+ raise OSError('%s po file: %s not found' % (appname, pofile))
+ 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
+
+ k = getattr(obj, '%s_%s' % (fld, settings.LANGUAGE_CODE))
+ if k is None: k = ''
+ v = getattr(obj, locfld)
+ if v is None: v = ''
+ entry = polib.POEntry(
+ msgid=k,
+ msgstr=v,
+ occurrences=[('%s/%s/%s' % (appname, md.__name__, locfld), obj.id)])
+ po.append(entry)