Habr, ótima hora do dia para todos! O curso Python Web-Developer começará em breve no OTUS: nós o convidamos para a lição de demonstração gratuita “Page Controller e Front Controller Patterns: Implementation in Django” e publicamos a tradução do artigo de Nicolle Cysneiros - Full Stack Developer (Labcodes).

, 360 . , , , , – . 4,67% . – , , , , .
, . , Python Django. , , .
— , .
— . , () .
Django: , – .
, . , , :
;
;
;
(. );
, .


.
Python?
GNU gettext
, Python. GNU gettext, Translation Project. :
, ;
, ;
, , .
– Hello World app.py, gettext Python (gettext.translation) , . gettext ( gettext ), , , «Hello World!», .
import gettext
gettext.bindtextdomain("app", "/locale")
gettext.textdomain("app")
t = gettext.translation("app", localedir="locale", languages=['en_US'])
t.install()
_ = t.gettext
greeting = _("Hello, world!")
print(greeting) , GNU xgettext. PO-, .
xgettext -d app app.py
PO- ( Portable Object) , :
# translator-comments
#. extracted-comments
#: reference…
#, flag…
#| msgid previous-untranslated-string
msgid untranslated-string
msgstr translated-string , . ID (msgid), , (msgstr) – .
xgettext , app.py , PO-:
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-05-03 13:23-0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: app.py:7
msgid "Hello, world!"
msgstr "", . «Hello World!» ID . , ID .
PO- . , GNU gettext PO- (<localedir>/<languagecode>/LCMESSAGES/<domain>.po), , , PO-.
|-- app.py
|-- locale
|-- en_US
| |-- LC_MESSAGES
| |-- app.po
|-- pt_BR
|-- LC_MESSAGES
| |-- app.poPO- :
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-05-03 13:23-0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: app.py:7
msgid "Hello, world!"
msgstr "Olá, mundo!" , PO- MO- msgfmt.
msgfmt -o app.mo app.po
MO- , , . , «Olá, mundo!»:
import gettext
gettext.bindtextdomain("app", "/locale")
gettext.textdomain("app")
t = gettext.translation("app", localedir="locale", languages=['pt_BR'])
t.install()
_ = t.gettext
greeting = _("Hello, world!")
print(greeting)locale
(locale) POSIX, , . locale:
import datetime
import locale
locale.setlocale(locale.LC_ALL, locale='en_US')
local_conv = locale.localeconv()
now = datetime.datetime.now()
some_price = 1234567.89
formatted_price = locale.format('%1.2f', some_price, grouping=True)
currency_symbol = local_conv['currency_symbol']
print(now.strftime('%x'))
print(f'{currency_symbol}{formatted_price}') , US English . locale.format . %x , . .
Python. , Month/Day/Year, – , – , .
$ python format_example.py
05/03/2019
$1,234,567.89 , Portuguese Brazil, , : Month/Day/Year, , , R$ , .
import datetime
import locale
locale.setlocale(locale.LC_ALL, locale='pt_BR')
local_conv = locale.localeconv()
now = datetime.datetime.now()
some_price = 1234567.89
formatted_price = locale.format('%1.2f', some_price, grouping=True)
currency_symbol = local_conv['currency_symbol']
print(now.strftime('%x'))
print(f'{currency_symbol}{formatted_price}')Django?
Django. GNU gettext , Accept-Language, . , Python, , django utils, gettext:
from django.http import HttpResponse
from django.utils.translation import gettext as _
def my_view(request):
greetings = _('Hello, World!')
return HttpResponse(greetings) , Python ( ). trans template , blocktrans , .
<p>{% trans "Hello, World!" %}</p>
<p>{% blocktrans %}This string will have {{ value }} inside.{% endblocktrans %}</p> gettext Django : , , , . help_text verbose_name Django.
GNU, django admin , . , , django admin makemessages , . locale , PO- .
PO-, django admin compilemessages. PO- , django-admin compilemessages --locale=pt_BR. , Django, .
Django Accept-Language , . DateField DecimalField. , , , localize True .
from django import forms
class DatePriceForm(forms.Form):
date = forms.DateField(localize=True)
price = forms.DecimalField(max_digits=10, decimal_places=2, localize=True)?
. . PO-.
QA. QA , , , , .
? «Python Web-Developer» Demo- OTUS!