Internationalization of a local django project
Well, when developing a project for django, the developers of the project initially took care of its internationalization.
With minimal effort, the project adapts to various languages. Django has a rich set of tools, sufficient to almost automatically add new languages, fix and add translations of individual sections of text, and so on.
The situation looks completely different if the project was not originally intended for internationalization. A huge array of files in which local lines are scattered in a complete mess, while many of them are duplicated in different files. Collecting all these lines into a single file, sorting, replacing their contents in the source files with message identifiers is a huge and ungrateful job.
Faced with this task, I realized that doing it manually, I would die of boredom, if before that I would not feel sick. At the same time, I was surprised to find that there are still no tools that would make my life at least half easier. Four days of work - and the django-make-i18n project was born , which I present to you.
All work is done by one fairly voluminous script in python. In addition to python (I have 2.7), the script itself depends only on polib and does not require the django environment.
By launching the script for the first time and releasing it to the project directory, you get a draft of the django.po file, which contains all the detected lines in the local (by default, Russian) language. Caught strings are located 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 .i18n extension added to the directory name.
Next, you have to go through the django.po file and make a “reverse translation”, that is, replace the initial msgid values with some intelligible equivalent in English. It is only necessary to ensure that all msgid lines remain unique values.
The second run of the script analyzes the django.po file and performs the substitution of calls to the translation subsystem instead of the original local lines. For example, suppose you have a views.py file that contains the following code:
After the first run of the script, django.po will display lines similar to the following:
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:
Let's try filling in the lines in django.po as follows:
Run the script again - and get a pretty tolerable code of the following form:
Additionally, the script adds the import code of the internationalization package to the beginning of the file:
in case you accidentally forget to do this.
The same operations, taking into account the specifics of the language and the use of django, are performed on html and js files. At the same time, the djangojs.po file is created for JavaScript.
It remains to compile the messages:
Voila - your project is prepared for internationalization. Of course, there are still many details of the internationalization itself under django, but they are beyond the scope of this article.
With minimal effort, the project adapts to various languages. Django has a rich set of tools, sufficient to almost automatically add new languages, fix and add translations of individual sections of text, and so on.
The situation looks completely different if the project was not originally intended for internationalization. A huge array of files in which local lines are scattered in a complete mess, while many of them are duplicated in different files. Collecting all these lines into a single file, sorting, replacing their contents in the source files with message identifiers is a huge and ungrateful job.
Faced with this task, I realized that doing it manually, I would die of boredom, if before that I would not feel sick. At the same time, I was surprised to find that there are still no tools that would make my life at least half easier. Four days of work - and the django-make-i18n project was born , which I present to you.
All work is done by one fairly voluminous script in python. In addition to python (I have 2.7), the script itself depends only on polib and does not require the django environment.
By launching the script for the first time and releasing it to the project directory, you get a draft of the django.po file, which contains all the detected lines in the local (by default, Russian) language. Caught strings are located 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 .i18n extension added to the directory name.
Next, you have to go through the django.po file and make a “reverse translation”, that is, replace the initial msgid values with some intelligible equivalent in English. It is only necessary to ensure that all msgid lines remain unique values.
The second run of the script analyzes the django.po file and performs the substitution of calls to the translation subsystem instead of the original local lines. For example, suppose you have a views.py file that contains the following code:
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, django.po will display lines similar to the following:
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 filling 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 get a pretty 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 import code of the internationalization package to the beginning of the file:
from django.utils.translation import ugettext as _
in case you accidentally forget to do this.
The same operations, taking into account the specifics of the language and the use of django, are performed on html and js files. At the same time, the djangojs.po file is created 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 many details of the internationalization itself under django, but they are beyond the scope of this article.