Skip to content

jakhongirs/model-translation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Model Translation

This is a simple example of how to translate a model from one language to another.

Setup

  1. pip install django-modeltranslation
  2. Add 'modeltranslation' to INSTALLED_APPS
  3. In settings file we have to set USE_I18N = True
  4. Add these codes to settings.py
MODELTRANSLATION_DEFAULT_LANGUAGE = "en-us"
MODELTRANSLATION_LANGUAGES = ("en-us", "uz", "ru")
MODELTRANSLATION_FALLBACK_LANGUAGES = ("en-us", "uz", "ru")


def gettext(s):
    return s


LANGUAGES = (
    ("en-us", gettext("English")),
    ("ru", gettext("Русский")),
    ("uz", gettext("O'zbekcha")),
)

LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"),)
  1. Create translation.py in the app in which we are storing the models.py
  2. Add these codes to translation.py
from modeltranslation.translator import translator, TranslationOptions
from .models import Person

# for Person model
class PersonTranslationOptions(TranslationOptions):
    fields = ('name', 'surname', 'profession')

translator.register(Person, PersonTranslationOptions)
  1. Make migrations and migrate
   python manage.py makemigrations
   python manage.py migrate
  1. Add to Middleware in settings.py
'django.middleware.locale.LocaleMiddleware',
  1. Add to urls.py
from django.conf.urls.i18n import i18n_patterns

urlpatterns = [
    path("i18n/", include("django.conf.urls.i18n")),
]

urlpatterns += i18n_patterns(path("admin/", admin.site.urls))
  1. Create makemesages and compilemessages
python manage.py makemessages -l uz --ignore=venv
python manage.py makemessages -l ru --ignore=venv
python manage.py compilemessages

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages