Anonymous avatar Anonymous committed d0538a2

Reverted removal of TRANSLATION_REGISTRY. Handled ImportError.

Comments (0)

Files changed (3)

+  FIXED: Added workaround for swallowed ImportErrors by printing a traceback
+         explicitly.
+         (resolves issue 17)
   FIXED: Only print debug statements to stdout if the runserver or
          runserver_plus management commands are used.
-         Resolves issue 16.
+         (resolves issue 16)
   FIXED: Removed print statements so that modeltranslation is usable with
          mod_wsgi.
-         Resolves issue 7.
+         (resolves issue 7)
   FIXED: Broken admin fields and fieldsets.
-         Resolves issue 9.
+         (thanks simoncelen, resolves issue 9)
   FIXED: Creation of db fields with invalid python language code.
-         Resolves issue 4.
+         (resolves issue 4)
   FIXED: Tests to run from any project.
-         Resolves issue 6.
-  FIXED: Removed unused dependency to content type which can break syncdb. 
-         Resolves issue 1.
-    
-CHANGED: Removed TRANSLATION_REGISTRY setting.
+         (thanks carl.j.meyer, resolves issue 6)
+  FIXED: Removed unused dependency to content type which can break syncdb.
+         (thanks carl.j.meyer, resolves issue 1)
 
-  ADDED: Initial support for south.
-         Resolves issue 11.
-  ADDED: Support for admin inlines. 
-         Resolves issue 12.
+  ADDED: Preserve the formfield widget of the translated field.
+         (thanks piquadrat)
+  ADDED: Initial support for django-south.
+         (thanks andrewgodwin, resolves issue 11)
+  ADDED: Support for admin inlines, common and generic.
+         (resolves issue 12 and issue 18)
 
 
 v0.1

docs/modeltranslation/modeltranslation.txt

 3. Create a ``translation.py`` in your project directory and register
    ``TranslationOptions`` for every model you want to translate.
 
-4. Sync the database using ``manage.py syncdb`` (note that this only applies 
+4. Configure the ``TRANSLATION_REGISTRY`` variable in your ``settings.py``.
+
+5. Sync the database using ``manage.py syncdb`` (note that this only applies 
    if the models registered in the ``translations.py`` did not have been 
    synced to the database before. If they did - read further down what to do
    in that case.
 (statically) translate the verbose names of the languages using the standard
 ``i18n`` solution.
 
+**settings.TRANSLATION_REGISTRY**
+
+In order to be able to import the project's ``translation.py`` registration
+file the ``TRANSLATION_REGISTRY`` must be set to a value in the form
+``<PROJECT_MODULE>.translation``. E.g. if your project is located in a folder
+named ``myproject`` the ``TRANSLATION_REGISTRY`` must be set like this::
+
+    TRANSLATION_REGISTRY = "myproject.translation"
+
+
 Registering models and their fields for translation
 ---------------------------------------------------
 The ``modeltranslation`` app can translate ``CharField`` and ``TextField`` 

modeltranslation/models.py

 import sys
 
 from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
 from django.db import models
 
 from modeltranslation.translator import translator
 # field specified in the model's translation options.
 
 # Import the project's global "translation.py" which registers model 
-# classes and their translation options with the translator object. 
-try: 
-    import translation
-except ImportError:
-    sys.stderr.write("modeltranslation: Can't import the file 'translation.py'"
-                     "from your project root.\n(If the file does indeed exist,"
-                     "it's causing an ImportError somehow.)\n")
-    # 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()
-    sys.exit(1)
+# classes and their translation options with the translator object.
+if getattr(settings, 'TRANSLATION_REGISTRY', False):
+    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()
 
-# 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
+    # 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.")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.