django-modeltranslation
. I chose it among others for the following reasons:Django 1.3
JOIN
mptt
)South
- installed and working.django
).modeltranslation
with the pip install django-modeltranslation
.python setup.py install
. From the unpacked directory, you can immediately modeltranslation/static/modeltranslation
directory into the folder for static files.'modeltranslation'
to the INSTALLED_APPS
settings file settings.py
.MYPROJECT
, you need to replace it with the name of your project (it’s the name of the folder in which it is located), and translation
— with the name of the .py
file itself without an extension; by default, translation.py
. This file is one for the entire project and is placed, respectively, in the root of the project. LANGUAGES = ( ('ru', 'Russian'), ('en', 'English'), ) MODELTRANSLATION_DEFAULT_LANGUAGE = 'ru' MODELTRANSLATION_TRANSLATION_REGISTRY = 'MYPROJECT.translation'
'name'
and 'description'
) to translate and bind these classes to translated models (here, 'Modelka'
) of your applications (here, 'app'
) appropriately: # -*- coding: utf-8 -*- from modeltranslation.translator import translator, TranslationOptions from app.models import Modelka class ModelkaTranslationOptions(TranslationOptions): """ Modelka. """ fields = ('name', 'description',) translator.register(Modelka, ModelkaTranslationOptions)
python manage.py schemamigration app --auto
.South
discover new fields for localization and generate a migration to add them. By the way, if you disable the application 'modeltranslation'
- then you can also generate and migration to remove them. In addition, a separate convenience is that the modeltranslation
binds the default language to the original fields of the model, i.e. this localization field and the translated original field of the model will be identical.python manage.py migrate app
.admin.py
file for each application !!! And not in the model file, as I admit honestly, in the beginning I tried to do.mptt
admin mptt
: # -*- coding: utf-8 -*- from django.contrib import admin from mptt.admin import MPTTModelAdmin from modeltranslation.admin import TranslationAdmin from models import Modelka class ModelkaAdmin(MPTTModelAdmin, TranslationAdmin): """ Modelka. """ list_display = ('name',) class Media: js = ( '/static/modeltranslation/js/force_jquery.js', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js', '/static/modeltranslation/js/tabbed_translation_fields.js', ) css = { 'screen': ('/static/modeltranslation/css/tabbed_translation_fields.css',), } fieldsets = [ (None, { 'fields': [ 'name', 'slug', 'type', 'description', 'parent', ] }), (u'', { 'fields': [ 'author', 'editor', ], 'classes': ['collapse'] }) ] admin.site.register(Modelka, ModelkaAdmin)
python manage.py collectstatic
( for more information about static files ). Accordingly, do not forget to correct the paths in the nested Meta
class, if they are different.set_language
using set_language
. Those. the model will return field values in the current application language returned by get_lang
.Source: https://habr.com/ru/post/128273/
All Articles