python - django permissions and contenttypes translations -
i've been using django-modeltranslation translate models in django while. straightforward , works on apps i've been developing, all model translated content gets inserted forms final user.
eg: inputs: content, content_en, content_pt, ...
i have build application need translate 'built-in' model strings generated django, 'auth.permission.name' or 'contenttypes.contenttype.name' and add them translation django.po files.
i came solution works fine,
which uses post_migration signals create file lists of ugettext_lazy elements, new strings, new contenttype.name example, added 'django.po' dynamically , loaded database.
yet, bit weird having create file ugettext calls
in order register strings, didn't find way of registering , adding them dynamically django.po file, need help
here's have done:
1. created app named 'tools', last 1 on installed_apps, migrations naturally last ones called. app not have models, runs migrations, has django-modeltranslation translation.py file , application config post_migration signal call.
# translations.py modeltranslation.translator import translator, translationoptions django.contrib.auth.models import permission django.contrib.contenttypes.models import contenttype class permissiontranslationoptions(translationoptions): fields = ('name',) class contenttypetranslationoptions(translationoptions): fields = ('name',) translator.register(permission, permissiontranslationoptions) translator.register(contenttype, contenttypetranslationoptions)
2. running 'manage.py makemigrations' creates migrations on 'auth' , 'contenttypes' applications 'name_*' fields.
3. app has application config has post_migrate signal
# __init__.py default_app_config = 'apps.tools.config.systemconfig'
# config.py django.apps import appconfig django.db.models.signals import post_migrate apps.tools.translations.exporter import make_translations apps.tools.translations.importer import load_translations def run_translations(sender, **kwargs): # creates translations make_translations() # loads the translations db load_translations() class systemconfig(appconfig): name = 'apps.tools' verbose_name = 'tools' def ready(self): # call post migration operations post_migrate.connect(run_translations, sender=self)
4. make_translations() called after migrations , generates file lists of uggettext_lazy calls.
this bit change. need create file?
# exporter import os django.contrib.auth.models import permission django.contrib.contenttypes.models import contenttype django.utils import translation django.contrib.contenttypes.management import update_all_contenttypes # todo # has got way def make_translations(): # lets go default translation.activate("en") update_all_contenttypes() try: f = open(os.path.join(os.path.realpath(os.path.dirname(__file__)), 'translations.py'), 'w') # write file f.write("from django.utils.translation import ugettext_lazy _\n\n") # permissions lazy text f.write('permissions = {\n') perm in permission.objects.all().order_by('id'): f.write(' "'+str(perm.id)+'": _("'+perm.name+'"),\n') f.write('}\n\n') # content types lazy text f.write('content_types = {\n') content in contenttype.objects.all().order_by('id'): f.write(' "'+str(content.id)+'": _("'+content.name+'"),\n') f.write('}\n\n') # closing file f.close() # importing file registered ugettext_lazy try: apps.tools.translations import translations except: print('could not import file') pass except: print('could not create file') pass
the above results in file this:
from django.utils.translation import ugettext_lazy _ permissions = { "1": _("can add permission"), "2": _("can change permission"), "3": _("can delete permission"), "4": _("can add group"), ... } content_types = { "1": _("group"), "2": _("user"), "3": _("permission"), "4": _("content type"), "5": _("session"), ... }
5. running 'makemessages' add strings 'django.po' files, yet, post_migration signal not stop here, , loads existing compiled strings in database
# importer django.contrib.auth.models import permission django.contrib.contenttypes.models import contenttype django.conf import settings django.utils import translation def load_translations(): try: apps.tools.translations.translations import permissions, content_types except: # file not exists print('translations not loaded') return # each language lang in settings.languages: # activate language translation.activate(lang[0]) # loading translated permissions all_permissions = permission.objects.all() permission in all_permissions: permission.name = unicode(permissions[str(permission.id)]) permission.save() # loading translated content_types all_contenttypes = contenttype.objects.all() contenttype in all_contenttypes: contenttype.name = unicode(content_types[str(contenttype.id)]) contenttype.save()
how can replace 'make_translations()' without creating file , register strings ugettext_lazy?
thanks help
Comments
Post a Comment