Enmerkar
Enmerkar is a Python library providing utilities for integrating Babel, a powerful internationalization and localization toolkit, with Django web applications. It is a fork of `django-babel` and is currently at version 0.7.1, offering compatibility with modern Django and Python versions. Releases are infrequent but target essential Django updates.
Common errors
-
ModuleNotFoundError: No module named 'django_babel'
cause The library was renamed from `django-babel` to `enmerkar` starting from version 0.7.0.fixUninstall `django-babel`, install `enmerkar`, and update `INSTALLED_APPS`, middleware entries, and any direct Python imports in your code to use `enmerkar` instead of `django_babel`. -
django.core.exceptions.ImproperlyConfigured: The BABEL_DEFAULT_LOCALE setting must be configured.
cause The required `BABEL_DEFAULT_LOCALE` (and possibly `BABEL_DEFAULT_TIMEZONE`) setting is missing from your Django `settings.py`.fixAdd `BABEL_DEFAULT_LOCALE = 'en_US'` and `BABEL_DEFAULT_TIMEZONE = 'UTC'` (or your desired values) to your Django project's `settings.py` file. -
django.core.exceptions.ImproperlyConfigured: 'enmerkar.middleware.LocaleMiddleware' must be in MIDDLEWARE after 'django.contrib.sessions.middleware.SessionMiddleware'
cause Enmerkar's `LocaleMiddleware` is placed incorrectly in the `MIDDLEWARE` list, requiring session data to determine the locale.fixEnsure `enmerkar.middleware.LocaleMiddleware` is listed after `django.contrib.sessions.middleware.SessionMiddleware` (and `django.middleware.locale.LocaleMiddleware`, if used) in your `settings.py`.
Warnings
- breaking Enmerkar dropped support for Python < 3.5 and Django < 2.2 in version 0.7.1.
- breaking The package was renamed from `django-babel` to `enmerkar` in version 0.7.0.
- gotcha The `BABEL_DEFAULT_LOCALE` and `BABEL_DEFAULT_TIMEZONE` settings are mandatory for Enmerkar.
Install
-
pip install enmerkar
Imports
- LocaleMiddleware
from django_babel.middleware import LocaleMiddleware
from enmerkar.middleware import LocaleMiddleware
Quickstart
# In your Django settings.py
INSTALLED_APPS = [
# ... other apps
'enmerkar',
]
MIDDLEWARE = [
# ... other middleware, e.g., SessionMiddleware
'enmerkar.middleware.LocaleMiddleware',
]
# Required Babel settings
BABEL_DEFAULT_LOCALE = 'en_US'
BABEL_DEFAULT_TIMEZONE = 'Europe/London'
# In a Django template (e.g., mytemplate.html)
# {% load babel %}
# <p>Formatted Date: {{ datetime_obj|babeldate:'full' }}</p>
# <p>Formatted Time: {{ datetime_obj|babeltime:'long' }}</p>
# <p>Formatted Number: {{ 12345.67|babelnumber }}</p>