1

我是模型翻译的新手,我有一个问题。当我manage.py syncdb在创建模型并在模型翻译translation.py应用程序中注册要翻译的字段后执行命令时,不会将翻译后的字段添加到模型中。这些字段在表中。因此,如果我在 python shell 中创建一个对象,我将无法访问display_en,因为它会引发错误

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

我的 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" 

我的模型.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'))

我的翻译.py:

    from modeltranslation.translator import translator, TranslationOptions

    from myapp.models import Test


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

    translator.register(Test, TestTranslationOptions)
4

2 回答 2

1

你正在使用南,所以你必须做 ./manage.py 迁移来翻译你的领域。希望这有帮助

于 2013-08-29T18:06:30.967 回答
1

模型翻译是否加载?使用开发服务器将manage.py runserver调试信息打印到stdout.

Validating models...

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

如果您没有看到这个(并且您没有停用DEBUG),我想在导入时出了点问题。

另外modeltranslation你用的是哪个版本?

TRANSLATION_REGISTRY设置从 0.3 开始被弃用,它切换到一致的前缀,所以它变成了MODELTRANSLATION_TRANSLATION_REGISTRY.

在 0.4 per-app level 中引入了翻译文件,使此设置完全可选。translation.py相反,现在每个应用程序都会自动在其根目录中搜索 a 。同时MODELTRANSLATION_TRANSLATION_FILES被添加。也是可选的,它允许扩展翻译文件列表。

尽管MODELTRANSLATION_TRANSLATION_REGISTRY即使在最新的开发版本中也保留了对 的支持以保持向后兼容性,但不再建议使用它。相反,只需将其translation.py放入您要翻译的应用程序的目录中。

最后,fields您的属性translation.py应该是一个元组(或列表),您在其中定义一个字符串。当您添加这样的逗号时,Python 只会将其识别为元组:

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

我想这是您的问题中的一个错字,因为modeltranslation如果找不到这些字段,我希望能够退出。

于 2013-01-19T16:03:32.357 回答