Update the docs to show how badly are changes needed.
[wolnelektury.git] / apps / modeltranslation / models.py
1 # -*- coding: utf-8 -*-
2 import sys
3
4 from django.conf import settings
5 from django.core.exceptions import ImproperlyConfigured
6 from django.db import models
7
8 from modeltranslation.translator import translator
9
10 # Every model registered with the modeltranslation.translator.translator
11 # is patched to contain additional localized versions for every
12 # field specified in the model's translation options.
13
14 # Import the project's global "translation.py" which registers model
15 # classes and their translation options with the translator object.
16 if getattr(settings, 'TRANSLATION_REGISTRY', False):
17     try:
18         __import__(settings.TRANSLATION_REGISTRY, {}, {}, [''])
19     except ImportError:
20         sys.stderr.write("modeltranslation: Can't import module '%s'.\n"
21                          "(If the module exists, it's causing an "
22                          "ImportError somehow.)\n" %\
23                          settings.TRANSLATION_REGISTRY)
24         # For some reason ImportErrors raised in translation.py or in modules
25         # that are included from there become swallowed. Work around this
26         # problem by printing the traceback explicitly.
27         import traceback
28         traceback.print_exc()
29
30     # After importing all translation modules, all translation classes are
31     # registered with the translator.
32     if settings.DEBUG:
33         try:
34             if sys.argv[1] in ('runserver', 'runserver_plus'):
35                 translated_model_names = ', '.join(
36                     t.__name__ for t in translator._registry.keys())
37                 print('modeltranslation: Registered %d models for '
38                       'translation (%s).' % (len(translator._registry),
39                                              translated_model_names))
40         except IndexError:
41             pass
42 else:
43     raise ImproperlyConfigured("You haven't set the TRANSLATION_REGISTRY "
44                                "setting yet.")