[Answered ]-Translating text from database

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:

  1. Install the package with: pip install django-modeltranslation
  2. Add modeltranslation to your INSTALLED_APPS in your settings.py
  3. Set USE_I18N = True in settings.py.
  4. Configure your LANGUAGES in settings.py.
  5. Create a translation.py in your app directory and register TranslationOptions for every model you want to translate.
  6. 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.

Leave a comment