Search code examples
pythondjangodjango-modeltranslation

What are you supposed to do with the original field that you translate with django-modeltranslation?


I specify in settings.py the languages the fields of the models should be available in:

gettext = lambda s: s
LANGUAGES = (
    ('en', gettext('English')),
    ('zh-cn', gettext('Simplified Chinese')),
    ('zh-tw', gettext('Traditional Chinese')),
)

But when I apply the migrations, this will mean that apart from the original field, it will create these additional three fields. If the original field was "name", I will now have "name", "name_en", "name_zh_cn", and "name_zh_tw".

What is one supposed to do with the original field? Ignore it? Delete it? Should I just not put English in LANGUAGES and treat the original one as the English translation?

When I looked for solutions on their Github page, someone said that you can just set the default to the language you want, and not include it in your TRANSLATIONMODEL_LANGUAGES, https://github.com/deschler/django-modeltranslation/issues/488#issuecomment-457427502 but this isn't true (at least for the most recent release of translationmodel), because it will give you a django.core.exceptions.ImproperlyConfigured: MODELTRANSLATION_DEFAULT_LANGUAGE not in LANGUAGES setting. error.


Solution

  • You can achieve not having the additional english field with the following settings:

    gettext = lambda s: s
    LANGUAGES = (
        ('en', gettext('English')),
        ('zh-cn', gettext('Simplified Chinese')),
        ('zh-tw', gettext('Traditional Chinese')),
    )
    MODELTRANSLATION_LANGUAGES = ('zh-cn', 'zh-tw')
    

    This isn't necessarily a best practice since you are expected to use the original value: https://django-modeltranslation.readthedocs.io/en/latest/usage.html#rules