⬆️ ⬇️

Internationalization of the local django project

Well, when developing a project under django, the project developers initially attended to its internationalization.



Minimal effort, the project adapts to different languages. Django has a rich set of tools, enough for almost automatically adding new languages, correcting and adding translations of individual sections of text, and so on.



The situation looks quite different if the project was not originally intended for internationalization. A huge array of files in which local lines are scattered in utter confusion, with many of them duplicated in different files. Collecting all these lines in one file, sorting, replacing their contents in the source files with message identifiers is a great and ungrateful job.

')

Having met this task, I realized that by performing it manually, I would die of boredom, if before that I did not vomit. At the same time, I was surprised to find that there are still no tools that would facilitate my life at least by half. Four days of work - and the django-make-i18n project was born, which I present to you.



All the work is done by one fairly lengthy python script. In addition to the python (I have 2.7), the script itself depends only on polib and does not require the presence of the django environment.



By launching the script for the first time and launching it on the project directory, you get a stub file django.po, which collected all the detected lines in the local (by default - Russian) language. The captured lines are placed in msgstr sections, and msgid is filled with the initial value “NEEDS TO BE EDITED [nnn]”.



The source files are not changed, instead a parallel one is created next to the source project, with the extension .i18n added to the directory name.



Next, you have to go through the django.po file and perform a “reverse translation”, that is, replace the initial msgid values ​​with some intelligible equivalent in English. It is only necessary to ensure that all the lines of msgid values ​​remain unique.



The second run of the script analyzes the django.po file and performs substitution of calls to the translation subsystem instead of the original local strings. For example, suppose you have a views.py file in which the following code is placed:



if request.method == 'POST': # If the form has been submitted... post = True logging.info('POST  ') request_form = RequestVideoForm(request.POST) if request_form.is_valid(): # All validation rules pass logging.info(' ') ok = True send_request(request,request_form,request.region.urlname,'feedback@doroga.tv') message = u'   ' request_form = None # clean form else: message = u'   ' 




After the first run of the script, the following lines will appear in django.po like this:



 msgid "NEEDS TO BE EDITED [333]" msgstr "POST  " msgid "NEEDS TO BE EDITED [334]" msgstr " " msgid "NEEDS TO BE EDITED [335]" msgstr "   " msgid "NEEDS TO BE EDITED [336]" msgstr "   " 




If you do not change anything in the django.po file, then after the second run of the script, a copy of the views.py file will be created, containing approximately the following lines:



  if request.method == 'POST': # If the form has been submitted... post = True logging.info(_('NEEDS TO BE EDITED [333]')) request_form = RequestVideoForm(request.POST) if request_form.is_valid(): # All validation rules pass logging.info(_('NEEDS TO BE EDITED [334]')) ok = True send_request(request,request_form,request.region.urlname,'feedback@doroga.tv') message = _('NEEDS TO BE EDITED [335]') request_form = None # clean form else: message = _('NEEDS TO BE EDITED [336]') else: logging.info(_('NEEDS TO BE EDITED [337]')) request_form = RequestVideoForm(auto_id=True) 




Let's try to fill in the lines in django.po as follows:



 msgid "POST request has been found" msgstr "POST  " msgid "Form is valid" msgstr " " msgid "Your request has just been sent" msgstr "   " msgid "Fix form data" msgstr "   " 




Run the script again - and we get a quite tolerable code of the following form:



  if request.method == 'POST': # If the form has been submitted... post = True logging.info(_('POST request has been found')) request_form = RequestVideoForm(request.POST) if request_form.is_valid(): # All validation rules pass logging.info(_('Form is valid')) ok = True send_request(request,request_form,request.region.urlname,'feedback@doroga.tv') message = _('Your request has just been sent') request_form = None # clean form else: message = _('Fix form data') 




Additionally, the script adds the internationalization package import code to the beginning of the file:



 from django.utils.translation import ugettext as _ 




in case you accidentally forgot to do it.



The same operations, taking into account the specifics of the language and the use of django, are performed on the html and js files. This creates a djangojs.po file for JavaScript.



It remains to compile the messages:



 python manage.py compilemessages --all 




Voila - your project is prepared for internationalization. Of course, there are still all sorts of details of the very internationalization under django, but they are beyond the scope of this article.

Source: https://habr.com/ru/post/156879/



All Articles