django-modeltranslation/modeltranslation/models.py

46 lines
1.9 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import sys
2010-03-02 22:17:39 +00:00
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
2010-03-02 22:17:39 +00:00
from django.db import models
2009-02-17 21:08:19 +00:00
from modeltranslation.translator import translator
# Every model registered with the modeltranslation.translator.translator
2010-04-19 10:52:57 +00:00
# is patched to contain additional localized versions for every
2009-02-17 21:08:19 +00:00
# field specified in the model's translation options.
2010-04-19 10:52:57 +00:00
# Import the project's global "translation.py" which registers model
# classes and their translation options with the translator object.
# TODO: Rename setting to MODELTRANSLATION_TRANSLATION_REGISTRY.
if getattr(settings, 'TRANSLATION_REGISTRY', False):
2010-04-19 10:52:57 +00:00
try:
__import__(settings.TRANSLATION_REGISTRY, {}, {}, [''])
except ImportError:
sys.stderr.write("modeltranslation: Can't import module '%s'.\n"
"(If the module exists, it's causing an "
"ImportError somehow.)\n" %\
settings.TRANSLATION_REGISTRY)
# For some reason ImportErrors raised in translation.py or in modules
# that are included from there become swallowed. Work around this
# problem by printing the traceback explicitly.
import traceback
traceback.print_exc()
2009-02-17 21:08:19 +00:00
2010-04-19 10:52:57 +00:00
# After importing all translation modules, all translation classes are
# registered with the translator.
if settings.DEBUG:
try:
if sys.argv[1] in ('runserver', 'runserver_plus'):
translated_model_names = ', '.join(
t.__name__ for t in translator._registry.keys())
print('modeltranslation: Registered %d models for '
'translation (%s).' % (len(translator._registry),
translated_model_names))
except IndexError:
pass
else:
raise ImproperlyConfigured("You haven't set the TRANSLATION_REGISTRY "
"setting yet.")