Django Multilingual Starter Models


    I work, like all adequate programmers, on a social portal that will conquer everyone from infants to seniors. And since the goals are Napoleonic - it is necessary to take into account the possibility of using the service even by those representatives of humanity who do not own the great and the mighty. I know that many have long struggled with the issue of localizing content specifically (translation of individual model fields into different languages), because localization of the interface, within the generally accepted, in Django solved. The essence of the problem is that the same model (this is important! For example, items of a catalog that is the same for all languages) has translations of its fields into different languages. Those who have long been “on the needle” of this framework have probably already found for themselves the most suitable method for solving this problem, but I want to offer a solution and outline the procedure for beginners,


    Most recently, I was lucky enough to begin the conversion from the larva-PHP'shnika into the bug-Python'oid. So do not kick right away. This article (not an article) is HowTo for the same beginners.

    Materials used

    To localize the models, we will use the application for Django - django-modeltranslation. I chose it among others for the following reasons:
    • works with Django 1.3
    • adds translatable fields for languages ​​to the same table, i.e. there will be no extra JOIN's
    • does not require entering any code into existing models
    • turns on and off almost painlessly
    • in the admin panel, the localized model integrates with other modules (I was worried mptt)

    Still need South- installed and working.
    Actually everything (not counting the working / development site on django).


    Install the modeltranslationteam pip install django-modeltranslation.

    Or download the latest version of django-modeltranslation . Unpack, go to the unpacked directory and install python install. You can immediately drop the directory from the unpacked directory with your hands into the folder for static files modeltranslation/static/modeltranslation.

    Next, we are already working with our project.

    Add 'modeltranslation'to INSTALLED_APPSthe settings file

    In the same file we add the description of the required localizations, the default language and the imported file for registering localized models. In the latter 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 translationwith the name of the .pyfile itself without the extension, by default it is This file is one for the entire project and is placed, respectively, in the root of the project.

        ('ru', 'Russian'),
        ('en', 'English'),

    In this same file we need to create classes contain an indication of which fields (here 'name'and 'description') need to translate and map these classes to transferable models (here it is 'Modelka') your application (here 'app') sotvetstvtenno:

    # -*- 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)

    At this point, migration can be generated python schemamigration app --auto.

    Southit will detect 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 a migration to remove them. In addition, a separate convenience is that it modeltranslationbinds the default language to the original fields of the model, i.e. this localization field and the translated original model field will be identical.

    Do not forget to apply the migration: python migrate app.

    And finally, configure the admin panel.

    It is very important that the admin panel is described in a separate file admin.pyfor each application !!! And not in the model file, as I honestly admit, at first I tried to do it.

    For a sample, I’ll immediately give an example of using a localized admin panel with an admin panel 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 = (
            css = {
                'screen': ('/static/modeltranslation/css/tabbed_translation_fields.css',),
        fieldsets = [
            (None, {
                'fields': [
            (u'Лог', {
                'fields': [
                'classes': ['collapse']
        ], ModelkaAdmin)

    I draw your attention to the fact that the example immediately includes scripts for displaying translations on different tabs. These are just the files that I proposed at the beginning to copy to the directory for static content. If you installed modeltranslation via pip - do not forget to collect the static files with the command python collectstatic( more about static files ). Accordingly, do not forget to correct the paths in the nested Metaclass, if they differ from you.

    The screenshots show the tabs of the languages ​​for the translated fields of the model (a screenshot from the application being developed, because there are more languages ​​than in the example)

    Work with a localized model

    Actually, the work itself is absolutely transparent and also does not affect the existing code (the best language for displaying a site can be determined both by the framework itself and third-party modules, but this is a separate topic) if you do not need to declare the language used through somewhere with your hands set_language. Those. the model will return the field values ​​in the current application language returned get_lang.

    Also popular now: