2👍
To translate your model there is a very useful package I recommend you to check:
With this package you can mark your models for translation, you should follow the tutorial is pretty easy
Anyway the most important steps are:
- Install the package with:
pip install django-modeltranslation
- Add
modeltranslation
to yourINSTALLED_APPS
in yoursettings.py
- Set
USE_I18N = True
in settings.py. - Configure your LANGUAGES in settings.py.
- Create a translation.py in your app directory and register
TranslationOptions
for every model you want to translate. - Sync the database using
python manage.py syncdb
Examples
LANGUAGES in your settings.py:
LANGUAGES = (
('de', gettext('German')),
('en', gettext('English')),
)
translation.py
example:
# -*- coding: utf-8 -*-
# Model Translation
from modeltranslation.translator import translator, TranslationOptions
from models import *
class MyModelTranslationOptions(TranslationOptions):
fields = ('name', 'description') # Select here the fields you want to translate
translator.register(MyModel, MyModelTranslationOptions)
# You can add as many models as you want to translate here
After you define everything and sync the database, the models you choose to translate will have their fields translated.
If you have a field called description
and you mark it as translations, if you’re using English and German, django model translation will create the fields description_en
and description_de
where you can add the translations.
Source:stackexchange.com