📜 ⬆️ ⬇️

Multilingual Django Starter Models

Introductory


I work, like all adequate programmers, on a social portal that will conquer all from infants to marazmatik. And since the goals of Napoleon - you need to take into account the possibility of using the service even by those representatives of humanity who do not own the great and powerful. I know that many have long struggled with the question of the localization of content (translation of individual fields of the model into different languages), because localization of the interface, within the generally accepted, is solved in Django. The essence of the problem is that the same model (this is important! For example, the points of the catalog common to all languages) have translations of their fields into different languages. Those who have long been “on the needle” of this framework have probably already found the most appropriate method for solving this problem, but I want to suggest a solution and sketch out a procedure for beginners so that they do not run off to the ruby ​​mines.

Excuse


Most recently, I turned up a lucky chance to start turning from a PHP-PHP's larva into a Python'oid beetle. So don't kick your feet right away. This article (not the article) is HowTo for the same beginners.

Materials used


For localization of models we will use the application for Django - django-modeltranslation . I chose it among others for the following reasons:

You will also need South - installed and working.
Actually everything (not counting the working / developed site on django ).

Howto


Install the modeltranslation with the pip install django-modeltranslation .
')
Or download the latest version of django-modeltranslation . Unpack, go to the unpacked directory and install python setup.py install . From the unpacked directory, you can immediately modeltranslation/static/modeltranslation directory into the folder for static files.

Further we work already with our project.

Add 'modeltranslation' to the INSTALLED_APPS settings file settings.py .

In the same file we add the description of the required localizations, the default language and the imported file of the registration of the models being localized. In the last 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' 


In this file, we need to create classes containing instructions on which fields (here it's '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) 


At this point, you can generate a migration 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.

Don't forget to apply the migration later: python manage.py migrate app .

And finally, set up the admin panel.

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

For the sample, I will immediately give an example of using a localized admin panel with the 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) 


Please note that the example immediately includes scripts for displaying translations on different tabs. These are the files that I initially suggested copying to the directory for static content. If you installed the modeltranslation via pip, do not forget to build static files with the 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.



The screenshot shows language tabs for the model's translated fields (screenshot from the application being developed, therefore 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 already existing code (the best site display language can be determined either by the framework itself or by third-party modules, but this is a separate topic) if you do not need to declare the used language 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