Good day! I am trying to make a website in two languages, in Russian and English.

Russian by default. Configured localization, in the end, instead of the address /registration/ , /ru/registration/ and /en/registration/

The title placed in h1 is translated - it means the translation works, but the name of the form fields placed in has remained in Russian in both language urls. I use the latest Django 1.9, Python 3.

Below brought the clippings from the main files, help to understand.

settings.py

 LANGUAGE_CODE = 'ru' LANGUAGES = ( ('ru', _('Russian')), ('en', _('English')), ) 

root urls.py

 urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^i18n/', include('django.conf.urls.i18n')), ] urlpatterns += i18n_patterns( url(r'^$', views.home_page), url(r'', include('account.urls')), ) 

forms.py

 from django.utils.translation import ugettext as _ class UserRegistration(forms.Form): ... last_name = forms.CharField(label=_(u'Ѐамилия')) 

templates / account / registration.html

 {% load i18n %} <h1>{% trans "РСгистрация Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ" %}</h1> <form action="." method="post"> {{ form.as_p }} {% csrf_token %} <p><input type="submit" value="Log-in"></p> </form> 

locale / en / LC_MESSAGES / django.po

 #: account/forms.py:21 msgid "Ѐамилия" msgstr "Last name" #: templates/account/registration.html:22 msgid "РСгистрация Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ" msgstr "Register new user" 
  • Did you run compilemessages? - Dmitry Kalinin
  • Offtopic: usually like translations are done from English to Russian, and not vice versa - andreymal
  • As _ what function is used? - andreymal
  • Added a function call in the forms.py file. It often happens that at first the site is done in Russian, and then, for example, the authorities may want to add support for English, so I immediately decided to go this way. - wedoca
  • @DmitryKalinin - yes of course I started, first mekemessages -l en .. then compilemessages respectively - wedoca

2 answers 2

The ugettext function translates a phrase at the time it is called. And you call it when you create a class with a form. And the class with the form is created during the import of forms.py during the initialization of Dzhangi and still BEFORE accepting any requests from the user. As a result, the current language remains undefined, and there is nothing left for the function but to translate nothing.

In order not to perform the translation immediately, but to postpone it until the translation of the translated phrase is ugettext_lazy , there is a function ugettext_lazy - it returns not a translated string, but a special proxy object from which the translation is taken only at the moment of use (in this case, in the template, drawn when processing the request).

 from django.utils.translation import ugettext_lazy as _ class UserRegistration(forms.Form): ... last_name = forms.CharField(label=_(u'Ѐамилия')) 

PS In case you have to use a proxy object somewhere manually, you can get a translation from it by casting to the unicode string:

 >>> t = _('Log in') >>> print(t) <django.utils.functional.__proxy__ object at 0x7f2ae5b97b50> >>> print(str(t)) # unicode(t) для Python 2 Π’ΠΎΠΉΡ‚ΠΈ 

Read more in the documentation.

  • Oh yes - it worked! Thank you for your help .. I will go further into the study !!! - wedoca
  • @wedoca is worth noting the answers that helped you, as accepted, so that those who look for the answer to the same question can find it easier. - Sergey Gornostaev
  • I apologize, I recently began to use this service, how can I accept the question? - wedoca
  • @wedoca to the left under the arrows tick - andreymal
  • I saw .. thanks .. and I constantly tried to press the arrow, which is up ... - wedoca

For form fields, use ugettext_lazy instead of ugettext .