Package: enmerkar
Tools for using Babel with DjangoThis package contains various utilities for integration ofBabelinto theDjangoweb framework:A message extraction plugin for Django templates.A middleware class that adds the BabelLocaleobject to requests.A set of template tags for date and number formatting.Extracting MessagesBabel provides a message extraction framework similar to GNUxgettext, but
more extensible and geared towards Python applications. While Django does
providewrapper scriptsfor making the use ofxgettextmore
convenient, the extraction functionality is rather limited. For example, you
can’t use template files with an extension other than.html, and everything
needs to be in your project package directory.Extraction Method MappingSo enmerkar comes with an extraction method plugin that can extract
localizable messages from Django template files. Python is supported out of the
box by Babel. To use this extraction functionality, create a file calledbabel.cfgin your project directory (the directory above your project
package), with the content:[django: templates/**.*][django: mypkg/*/templates/**.*][python: mypkg/**.py]This instructs Babel to look for any files in the top-leveltemplatesdirectory, or any files in applicationtemplatesdirectories, and use the
extraction method named “django” to extract messages from those template files.
You’ll need to adjust those glob patterns to wherever you my be storing your
templates.Also, any files with the extension.pyinside your package directory (replace
“mypkg” with the actual name of your Django project package) are processed by
the “python” extraction method.If you don’t use setuptools, or for some reason haven’t installed enmerkar
using setuptools/pip, you’ll need to define what function the extraction method
“django” maps to. This is done in an extra section at the top of the
configuration file:[extractors]django=enmerkar.extract:extract_djangoThe encoding of the templates is assumed to be UTF-8. If you are using a
different encoding, you will need to specify it in the configuration. For
example:[django: templates/**.*]encoding=iso-8859-1Running the Extraction ProcessOnce you’ve set up the configuration file, the actual extraction is performed
by executing the command-line programpybabelwhich is installed alongside
the Babel package:$cdprojectdir$pybabelextract-Fbabel.cfg-omypkg/locale/django.pot.This creates the PO file template inmypkg/locale/django.pot.Creating and Updating Translations CatalogsIf you don’t already have translation catalogs, you need to create them. This
is done using thepybabel initcommand:$pybabelinit-Ddjango-imypkg/locale/django.pot-dmypkg/locale-len_US$pybabelinit-Ddjango-imypkg/locale/django.pot-dmypkg/locale-lde_DEThis should create two files:mypkg/locale/en_US/django.poandmypkg/locale/de_DE/django.po. These files are where you put the actual
translations.When you modify your Python source files or your templates, you genereally need
to sync the translation catalogs. For that, you first perform a fresh
extraction as described in the previous section, so that thedjango.potfile
gets updated.Then, you run thepybabel updatecommand to merge the changes into the
translation catalogs:`bash $ pybabel update-Ddjango-imypkg/locale/django.pot-dmypkg/locale `This will update all the.pofiles found in themypkg/localedirectory.Compiling Translations CatalogsFinally, you need to compile those.pofiles to binary.mofiles. Use thepybabel compilecommand for that:$pybabelcompile-Ddjango-dmypkg/localeAdd the--statisticsoption to get information about the completeness of your
translations:$pybabelcompile-Ddjango-dmypkg/locale--statisticsUsingsetup.pyMuch of the above process can be automated if you add asetup.pyscript to
your project and use the distutils/setuptools commands that come with Babel.
This is described atDistutils/Setuptools Integration.Using the MiddlewareTo use the enmerkar middleware, add it to the list ofMIDDLEWARE_CLASSESin your
settings module. If you’re also using Django’s ownLocaleMiddlewareto vary
the locale based on user preference, the enmerkar middleware must be inserted
after the Django one:MIDDLEWARE_CLASSES=(...'django.middleware.locale.LocaleMiddleware','enmerkar.middleware.LocaleMiddleware',...)This adds alocaleattribute to the request object, which is an instance of
the BabelLocaleclass. You can access the locale viarequest.localewhen
the request object is available, or otherwise use thedjango_babel.middleware.get_current_locale()function to get the current
locale from a thread-local cache.Using the Template TagsThe template filters provided by enmerkar allow formatting of date/time and
number values in a locale-sensitive manner, providing much more powerful
alternatives to thedate,time, andfloatformatfilters that come with
Django.To make the template filters/tags available, you need to add enmerkar to
the list ofINSTALLED_APPSin your settings module:INSTALLED_APPS=(...'enmerkar',...)And in every template you want to use the filters, you need to explicitly load
the library:{%loadbabel%}General information on date/time and number formatting can be found atDate FormattingandNumber Formatting.The following filters are made available. The examples assume a locale ofen_US.datefmtRenders a string representation of a date.Input:datetime.date,datetime.datetime, or a float/int timestampParameters:  the format name or pattern (optional)Assuming thatbook.pubdatereturns adatetime.dateordatetime.datetimeobject:{{book.pubdate|datefmt:"short"}}would render:4/1/07, and{{book.pubdate|datefmt:"E, MMM dd yyyy GGG"}}would render:Sun, Apr 01 2007 ADdatetimefmtRenders a string representation of a date and time.Input:datetime.datetime, or a float/int timestampParameters:  the format name or pattern (optional)Examples:{{book.pubdate|datetimefmt:"short"}}would render:4/1/07 3:30 PM, and{{book.pubdate|datetimefmt:"E, MMM dd yyyy GGG' - 'HH:mm:ss'"}}would render:Sun, Apr 01 2007 AD - 15:30:00timefmtRenders a string representation of a time.Input:datetime.datetime,datetime.time, or a float/int timestampParameters:  the format name or pattern (optional)Examples:{{book.pubdate|timefmt:"short"}}would render:3:30 PM, and{{book.pubdate|timefmt:"h 'o''clock' a'"}}would render:3 o’clock PMdecimalfmtRenders a string representation of a decimal number.Input:  aDecimalobject, or a float/int/long valueParameters:  the format name or pattern (optional)Examples:{{book.pagecount|decimalfmt}}would render:1,234, and{{book.pagecount|decimalfmt:"#,##0.00"}}would render:1,234.00currencyfmtRenders a number formatted as a currency value.Input:  aDecimalobject, or a float/int/long valueParameters:  the currency codeExamples:{{book.price|currencyfmt:"USD"}}would render:$49.90percentfmtRenders a string representation of a number as a percentage.Input:  aDecimalobject, or a float/int/long valueParameters:  the format name or pattern (optional)Examples:Assumingbook.rebatewould return0.15,{{book.rebate|percentfmt}}would render15%, and{{book.rebate|percentfmt:"#,##0.00%"}}would render15.00%.scientificfmtRenders a string representation of a number using scientific notation.Input:  aDecimalobject, or a float/int/long valueParameters:  noneExamples:Assumingbook.numsoldwould return 1.000.000,{{book.numsold|scientificfmt}}would render10E5.Changelog0.7.1 (2019-12-02)compatibility with Django 3.0drop support for Python < 3.5, Django < 2.20.7.0 (2019-10-21)forked from django-babeldrop support for Python < 3.4, Django < 2.0compatibility with Django 2.1 and 2.2 added
From @tsouvarev work :https://github.com/python-babel/django-babel/pull/450.6.2 (2017-12-18)compatibility with Django 2.0 added0.6.1 (2017-12-18)compatibility with Django 1.11 added0.6.0 - 2017-04-25compatibility with unsupported Django versions (<1.8) is droppedcompatibility with Django 1.10+ middlewares was added0.5.1 - 2016-03-30make imports absolute in babel templatetagsstrip quotes from translations via _()fix links in docsAdd support for “trimmed” blocktrans content0.5.0 - 2016-02-29Add compatibility for Django-1.90.4.0 - 2015-04-22Add compatibility for Django 1.8Add compatibility for latest django masterVarious python 3 fixes0.3.9 - 2014-12-24Fix dependencies on Django/Babel to use lower-case egg names.0.3.8 - 2014-10-14Fix old reference tobabeldjangomodule in entry points.0.3.7 - 2014-10-14Fix Python 3.x compatibility inbabel makemessagescommand.0.3.6 - 2014-10-05Django 1.7 compatibility0.3.5 - 2014-09-10Create .po and .pot files if not existing, plus it’s specific base directories.0.3.4 - 2014-05-25Fixed django compatibility0.3.3 - 2014-04-22Fixed release builds0.3.2 - 2014-04-22Initial testing infrastructureAdd management commandbabelwithmakemessagesandcompilemessageslabels. Mimics django’smakemessagesandcompilemessagescommands.Various unicode fixes0.3.1 - 2013-12-11fix relative import in template tags0.3.0 - 2013-12-11Rename package to django_babel0.2.3 - 2013-12-11Rename package on PyPIUse GitHub as source control