Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
django-modeltranslation po file with makemessages and compilemessages
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
import os
import django
import polib
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.core.management.commands import compilemessages as django_compilemessages
from django.template.loader import render_to_string
from django.db.models.loading import get_model
from modeltranslation.translator import translator
class Command(django_compilemessages.Command):
if django.VERSION < (1, 7):
requires_model_validation = True
else:
requires_system_checks = True
def handle(self, *args, **options):
help = "Runs over the entire source tree of the current directory and "\
"pulls all the string, including the one from django-modeltranslation"
# check if locale options are set, if no, do run over all languages
# in the settings.LANGAUGES
if options.get('locale'):
languages = options.get('locale')
else:
mainlanguage = settings.LANGUAGE_CODE
languages = [lang for lang, trans_obj
in settings.LANGUAGES
if lang is not mainlanguage]
super(Command, self).handle(*args, **options)
verbosity = int(options.get('verbosity'))
for lang in languages:
try:
path = os.path.join(settings.LOCALE_PATHS[0],
lang, 'LC_MESSAGES/models.po')
pofile = polib.pofile(path)
for entry in pofile:
value = entry.msgstr
for occurrence in entry.occurrences:
pk = occurrence[1]
app, model, field = occurrence[0].split('.')
obj = get_model(app, model).objects.get(pk=pk)
field_lang = field+'_'+lang
setattr(obj, field+'_'+lang, value)
if verbosity > 1:
self.stdout.write(obj.__class__.__name__\
+'//'+field_lang+'//'+str(obj)+'//'+value)
obj.save()
except:
if verbosity>0:
self.stdout.write(lang+' did not work for models')
return
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
import os
import re
from optparse import make_option
import django
import polib
from django.core.management.base import BaseCommand, CommandError
from django.core.management.commands import makemessages as django_makemessages
from django.template.loader import render_to_string
from django.conf import settings
from modeltranslation.translator import translator
METADATAS = {
'Project-Id-Version': '1.1',
'Report-Msgid-Bugs-To': 'jerome@theatreinterface.ch',
'POT-Creation-Date': '2016-01-12 22:16+0100',
'PO-Revision-Date': '2016-01-12-22:17+0100',
'Last-Translator': 'Jérôme <jerome@theatreinterface.ch>',
'Language-Team': 'INTERFACE <jerome@theatreinterface.ch>',
'MIME-Version': '1.0',
'Content-Type': 'text/plain; charset=utf-8',
'Content-Transfer-Encoding': '8bit',
}
p = re.compile(' +', re.IGNORECASE)
class Command(django_makemessages.Command):
# option_list = django_makemessages.Command.option_list + (
# make_option('--no-models', default=True, action='store_false',
# dest='with-models',
# help="usual makemessages, doesn't localise the models objects."),
# )
if django.VERSION < (1, 7):
requires_model_validation = True
else:
requires_system_checks = True
def handle(self, *args, **options):
help = "Runs over the entire source tree of the current directory and "\
"pulls all the string, including the one from django-modeltranslation"
verbosity = int(options.get('verbosity'))
# check if locale options are set, if no, do run over all languages
# in the settings.LANGAUGES
if options.get('locale'):
languages = options.get('locale')
else:
mainlanguage = settings.LANGUAGE_CODE
languages = [lang for lang, trans_obj
in settings.LANGUAGES
if lang is not mainlanguage]
# main loop
for lang in languages:
if verbosity > 0:
self.stdout.write('working on language ' + lang)
try:
path = os.path.join(settings.LOCALE_PATHS[0],
lang, 'LC_MESSAGES/models.po')
# if the file already exists, we use it
try:
pofile = polib.pofile(path)
os.remove(path)
except OSError:
if verbosity > 1:
self.stdout.write('language file for '+lang+' not existing')
finally:
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
pofile = polib.POFile()
pofile.metadata = METADATAS
pofile.save(path)
pofile = polib.pofile(path)
po_occurrences = [entry.occurrences for entry in pofile]
if verbosity > 1:
self.stdout.write(str(po_occurrences))
# we get the models registered in django-modeltranslations
for model in translator.get_registered_models():
self.stdout.write(str(model))
fields = translator.get_options_for_model(model).get_field_names()
# we gonna first iterate on fields, to get the uniques field values
for field in fields:
base_list = [(object.pk, getattr(object, field+"_fr"))
for object in model.objects.all()]
# this part uniquify the values (cause we won't translate the same
# shit 20 time)
base_list.sort()
self.stdout.write(field)
unique_dict = {}
unique_list = []
for pk, value in base_list:
occurrence = [(model._meta.app_label+'.'+str(model.__name__)+'.'+field, str(pk)),]
msgstr = getattr(model.objects.get(pk=pk), field+"_"+lang) or ''
if value in unique_dict.values():
unique_list[:] = [(occurrences + occurrence, msgstr, value) if val == value \
else (occurrences, msgstr, val) \
for occurrences, msgstr, val in unique_list]
else:
unique_list += [(occurrence, msgstr, value)]
unique_dict[pk]=value
for occurrences, msgstr, value in unique_list:
if value:
formated_value = value.replace("\'", "'")\
.replace('''"''', '''\\"''')\
.replace('''\\\\\\"''', """'""")\
.replace("""\\'""", """'""")\
.replace('''\\"''', """'""")\
.replace('\\t', '')
formated_value = re.sub(p, " ", formated_value)
entry = polib.POEntry(
occurrences=occurrences,
msgid=formated_value,
msgstr='',
#msgstr=msgstr,
)
pofile.append(entry)
if verbosity>1:
self.stdout.write(str(occurrences))
else:
if verbosity>1:
self.stdout.write('empty occurence : '+str(occurences))
pofile.save(path)
except:
pass
self.stdout.write('models exported with success')
return super(Command, self).handle(*args, **options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment