1

I'm new with modeltranslation and I have a problem. When I do the manage.py syncdb command after creating my model and registering the fields to translate in translation.py the modeltranslation app doesn't add the translated field to the model. The fields are in the table though. So if I create an object in the python shell I can't access display_en because it raises an error

AttributeError: 'Content' object has no attribute 'display_en'

My settings.py :

    DEBUG = True
    TEMPLATE_DEBUG = DEBUG

    ADMINS = (
         # ('Your Name', 'your_email@example.com'),
    )

    MANAGERS = ADMINS

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': 'test_db',                      # Or path to database file if using sqlite3.
            'USER': 'postgres',                      # Not used with sqlite3.
            'PASSWORD': 'admin',                  # Not used with sqlite3.
            'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
            'PORT': '5432',                      # Set to empty string for default. Not used with sqlite3.
          }
     }


     SITE_ID = 1

     TIME_ZONE = 'UTC'
     LANGUAGE_CODE = 'fr-fr'

     ugettext = lambda s: s

     LANGUAGES = ( 
          ('fr', ugettext('French')),
          ('en', ugettext('English')),
          ('ja', ugettext('Japanese')),
     )


     USE_I18N = True


     TEMPLATE_CONTEXT_PROCESSORS = (
     'django.core.context_processors.auth',
     'django.core.context_processors.debug',
     'django.core.context_processors.i18n',
     )

     USE_L10N = True
     USE_TZ = True

     STATICFILES_FINDERS = (
       'django.contrib.staticfiles.finders.FileSystemFinder',
       'django.contrib.staticfiles.finders.AppDirectoriesFinder',
       # 'django.contrib.staticfiles.finders.DefaultStorageFinder',
    )

   # List of callables that know how to import templates from various sources.
   TEMPLATE_LOADERS = (
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    )

   MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
   )

   ROOT_URLCONF = 'mysite.urls'


   TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
  )

   INSTALLED_APPS = (
       'django.contrib.auth',
       'django.contrib.contenttypes',
       'django.contrib.sessions',
       'django.contrib.sites',
       'django.contrib.messages',
       'django.contrib.staticfiles',
       'tagging',
       #'social_auth',
       'south',
       'django.contrib.admin',
       'sorl.thumbnail',
       'modeltranslation',
       'myapp',
       # Uncomment the next line to enable admin documentation:
       # 'django.contrib.admindocs',
     )

     TRANSLATION_REGISTRY = "myapp.translation" 

My models.py :

    from django.db import models
    from django.utils.translation import ugettext_lazy as _
    from django.conf import settings

    class Test(models.Model):
        display = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('test.display'))
        url = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('test.url'))

My translation.py :

    from modeltranslation.translator import translator, TranslationOptions

    from myapp.models import Test


    class TestTranslationOptions(TranslationOptions):
        fields = ('display')

    translator.register(Test, TestTranslationOptions)
Nathaniel
  • 666
  • 4
  • 15

2 Answers2

1

Does modeltranslation load at all? Using the development server manage.py runserver prints debug information to stdout.

Validating models...

modeltranslation: Registered 2 models for translation (Foo, Bar) [pid:12345].
0 errors found
[...]

If you don't see this (and you haven't deactivated DEBUG) I suppose something goes wrong at import time.

Also which modeltranslation version do you use?

The TRANSLATION_REGISTRY setting is deprecated since 0.3, which switched to consistent prefixes, so it became MODELTRANSLATION_TRANSLATION_REGISTRY.

In 0.4 per-app level translation files were introduced making this setting completely optional. Instead each app is now automatically searched for a translation.py in its root directory. At the same time MODELTRANSLATION_TRANSLATION_FILES was added. Also being optional it allows to extend the list of translation files.

While support for MODELTRANSLATION_TRANSLATION_REGISTRY is kept even in the latest development version to retain backwards compatibility, it is no longer recommended to use it. Instead just put the translation.py into the directory of the app you want to translate.

Finally, the fields attribute in your translation.py is supposed to be a tuple (or list), where you are defining a string. Python will only recognize it as a tuple when you add a comma like this:

class TestTranslationOptions(TranslationOptions):
    fields = ('display',)

I suppose it's a typo in your question, because I expect modeltranslation to bail out if it can't find the fields.

Vladir Parrado Cruz
  • 2,301
  • 21
  • 27
Dirk Eschler
  • 2,539
  • 1
  • 21
  • 41
1

You are using south, so You have to do ./manage.py migrate to translate your fields . Hope this help

DaschPyth
  • 195
  • 1
  • 2
  • 9