Locale support for Starlette
Project description
Starlette Babel
Provides translations, formatters, and timezone support for Starlette application by integrating Babel library.
Installation
Install starlette_babel using pip or uv:
pip install starlette_babel
# or
uv add starlette_babel
Features
- Locale middleware
- Multi-domain translations
- Locale selectors
- Timezone middleware
- Timezone selectors
- Locale-aware formatters (dates, times, numbers, currencies, percentages)
- Locale negotiation via
negotiate_locale - Number parsing via
parse_number/parse_decimal - Jinja2 integration
Quick start
See example application in examples/ directory of this repository.
Setting up translator and locale features
Configure Starlette application
To start using locale aware formatters, text translation and other components you have to set up a translator and add middleware to your Starlette application.
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_babel import get_translator, LocaleMiddleware
supported_locales = ['be', 'en', 'pl']
shared_translator = get_translator() # process global instance
shared_translator.load_from_directories(['/path/to/locales/']) # one or multiple locale directories
app = Starlette(
middleware=[
Middleware(LocaleMiddleware, locales=supported_locales, default_locale='en'),
]
)
Getting locale information
From request object
The LocaleMiddleware adds two state options to the request: locale and language.
from babel import Locale
def index_view(request):
current_locale: Locale = request.state.locale
current_language: str = request.state.language
Using get_locale helper
Alternatively, use get_locale to get the locale information
from babel import Locale
from starlette_babel import get_locale
locale: Locale = get_locale()
Locale selectors
LocaleMiddleware uses locale selectors to detect the locale from the request object.
The selector is a callable that accepts HTTPConnection object and returns either a locale code as a string or
None. The first locale selector that returns non-None value wins.
If all selectors fail then the middleware sets locale from default_locale option.
The detected locale should be in the list defined by the locales option otherwise it won't be accepted.
The default selector order is:
- from
localequery parameter - from
languagecookie - from
get_preferred_languageuser method (will userequest.user, if available) - from
accept-languageheader - fallback to configured default locale
Customizing locale selectors or changing their order
If you want to customize the way the middleware detects the locale, pass selectors option to the middleware:
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_babel import LocaleFromHeader, LocaleFromQuery, LocaleMiddleware
app = Starlette(
middleware=[
Middleware(LocaleMiddleware, selectors=[
LocaleFromQuery(), LocaleFromHeader(),
])
]
)
In this example we use only two selectors. They will be called in the order they are defined.
Custom locale selectors
You can define your own selector by writing a function or a callable object:
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import HTTPConnection
from starlette_babel import LocaleMiddleware
def my_locale_selector(conn: HTTPConnection) -> str | None:
return 'be_BY'
app = Starlette(
middleware=[
Middleware(LocaleMiddleware, selectors=[
my_locale_selector,
])
]
)
Mark translatable strings
At this point your application is translatable and each request contain locale information that you can use. Let's define some translatable strings
Please note, that we did not write any translations and the example below won't actually translate anything. This is an example of how to mark strings for translation. We will cover message extraction a bit later.
from starlette.responses import PlainTextResponse
from starlette_babel import gettext_lazy as _
welcome_message = _('Welcome')
def index_view(request):
return PlainTextResponse(welcome_message)
Extract translatable strings from the source code
Strings marked as translatable won't translate themselves. They should be extracted into .po files and compiled into
machine-readable .mo files. This topic is out of the scope if this documentation and is well-documented by the
official Babel documentation.
A brief hint on what to do is:
- configure
pybabeltool viapybabel.ini - create directories for each supported locale
- extract strings from the source code using
pybabel extractcommand - update locale specific message catalogues (
messages.po) usingpybabel updatecommand - compile these catalogues into machine-readable format (
messages.mo) usingpybabel compilecommand.
These commands are documented at https://babel.pocoo.org/en/latest/cmdline.html
Locales directory structure
The locales directory is a directory where we keep our translation files. Usually, this directory called locales.
The structure is like this: locales/_code_/LC_MESSAGES/messages.po where _code_ is a locale code.
Example:
your_app_package_name/
locales/
en/
LC_MESSAGES/
messages.po
de/
LC_MESSAGES/
messages.po
messages.pot
If the directory format does not match the expectation, translator would not be able to load messages and will fail silently. You can use the examples/locales for a reference.
Enable Jinja2 plugin
If you use Jinja2 templates you can integrate translator and formatters provided by this library with Jinja.
import jinja2
from starlette_babel.contrib.jinja import configure_jinja_env
jinja_env = jinja2.Environment()
configure_jinja_env(jinja_env)
The configure_jinja_env makes the following utilities available in the templates:
Global functions
_- alias forgettext_p- alias forngettext
<time>{{ _('Welcome') }}</time>
Filters
- datetime
- date
- time
- timedelta
- number
- currency
- percent
- scientific
All these filters are locale-aware and will format passed data using locale defined format.
<time>your local time is {{ now|datetime }}</time>
Manually setting the locale
You can set the current locale manually
from starlette_babel import set_locale
set_locale('pl')
Temporary setting the locale
You can switch locale temporary for a code block using switch_locale context manager. When the manager exits the
previous locale gets restored. This utility is very useful in unit tests.
from starlette_babel import switch_locale, set_locale
set_locale('pl')
# all speak Polish here
with switch_locale('be'):
# all speak Belarussian here
...
# all speak Polish here again
Manually translating strings
You can translate messages using translator.gettext and translator.ngettext directly in the code of the view
function:
from starlette_babel import Translator
translator = Translator(['/path/to/locales'])
def index_view(request):
translated = translator.gettext('Hello', locale='en')
Translation domains
This is advanced topic. Most apps don't need this but library developers may need it.
A translation domain is like a namespace. Same message can have different translations depending on the context (aka
domain). This library natively supports domains. We infer domain name from the .po file name, dropping the extension.
For the file like locales/en/LC_MESSAGES/errors.po the domain is errors.
The default translation domain is messages.
from starlette_babel import Translator
translator = Translator(['/path/to/locales'])
hello_message = translator.gettext('Hello', locale='en') # uses default `messages` domain
shopping_hello_message = translator.gettext('Hello', locale='en', domain='shopping') # uses `shopping` domain
Directory structure
The structure is exactly the same as stated above.
your_app_package_name/
locales/
en/
LC_MESSAGES/
messages.po
shopping.po # <-- new file. defines "shopping" domain
Formatters
The library integrates formatting utilities from the Babel package. Our version automatically applies current locale/timezone without defining them manually.
Here is the list of adapted formatters:
- format_datetime
- format_date
- format_time
- format_timedelta
- format_interval
- format_number
- format_currency
- format_percent
- format_scientific
- format_compact_decimal
Consult Babel documentation for more information.
Usage
import datetime
from starlette_babel import format_datetime, set_locale, set_timezone
set_locale('be')
set_timezone('Europe/Minsk')
now = datetime.datetime.now()
local_time = format_datetime(now) # <-- this
Jinja integration
There formatters are automatically exposed to templates after applying configure_jinja_env on Jinja environment.
Timezone support
To enable timezone support add TimezoneMiddleware. The middleware behaves much like LocaleMiddleware and shares same
concepts.
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_babel import TimezoneMiddleware
app = Starlette(
middleware=[
Middleware(TimezoneMiddleware, fallback='Europe/London')
]
)
By default, the middleware will try these selectors:
- from
tzquery parameter - from
timezonecookie - from
get_timezoneuser method
Retrieving timezone information
Reading timezone from request object
import datetime
def index_view(request):
timezone: datetime.tzinfo = request.state.timezone
Using get_timezone helper
Use get_timezone helper to get the timezone information set by the middleware.
If middleware not used it will return UTC.
import datetime
from starlette_babel import get_timezone
tz: datetime.tzinfo = get_timezone()
Customizing selectors or changing their order
You can change selectors set or the order they are defined by configuring selectors option of the middleware:
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_babel import TimezoneFromCookie, TimezoneFromQuery, TimezoneMiddleware
app = Starlette(
middleware=[
Middleware(TimezoneMiddleware, fallback='Europe/London', selectors=[
TimezoneFromQuery(), TimezoneFromCookie(),
])
]
)
Custom timezone selectors
A selector is a callable that accepts HTTPConnection and returns timezone code as a string:
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_babel import TimezoneMiddleware
def my_timezone_selector(conn):
return 'Europe/Minsk'
app = Starlette(
middleware=[
Middleware(TimezoneMiddleware, fallback='Europe/London', selectors=[
my_timezone_selector,
])
]
)
Setting timezone manually
Use set_timezone to set the timezone.
from starlette_babel import set_timezone
set_timezone('Europe/Minsk')
Temporary switch timezone
Use set_timezone to set the timezone.
from starlette_babel import switch_timezone
set_timezone('Europe/Minsk')
# time in +03
with switch_timezone('Europe/Warsaw'):
# time in +02
...
# time in +03 again
Convert datetime into user local time
You can apply currently active timezone to any datetime instance using to_user_timezone helper.
import datetime
from starlette_babel import to_user_timezone, set_timezone
set_timezone('Europe/Minsk')
now = datetime.datetime.now(datetime.timezone.utc) # time in UTC
user_now = to_user_timezone(now) # time in Europe/Minsk
Convert user local time to UTC
You can also convert datetime instance back to UTC using to_utc helper.
import datetime
from starlette_babel import set_timezone, to_user_timezone, to_utc
set_timezone('Europe/Minsk')
now = datetime.datetime.now() # time in UTC
user_now = to_user_timezone(now) # time in Europe/Minsk
utc_now = to_utc(user_now) # time in UTC again
Getting current time in user timezone
To get current user time use now helper.
from starlette_babel import set_timezone, now
set_timezone('Europe/Minsk')
user_now = now() # time in Europe/Minsk
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file starlette_babel-1.1.0.tar.gz.
File metadata
- Download URL: starlette_babel-1.1.0.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8376ac25a8fef2ed018e14005fa4ea2fde8e30d26dc2090f2ab0d221512542ba
|
|
| MD5 |
17e05dc36d21b513fe0217c76cbb5a8d
|
|
| BLAKE2b-256 |
dfa01480f1b699b351bc2405ff25c9d2f6e6b8d492caf26f1a2cb6a62a91d45e
|
File details
Details for the file starlette_babel-1.1.0-py3-none-any.whl.
File metadata
- Download URL: starlette_babel-1.1.0-py3-none-any.whl
- Upload date:
- Size: 15.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b08da9537522c50ed2005cb641120b1bfb630f58f09617bf6825e7223f4dcc5
|
|
| MD5 |
45e9d5ec693b3681a1c427e51775c48e
|
|
| BLAKE2b-256 |
1c53f2bb6f5976740c56bac8fd6208c6ddd7cf87c6f032df825a79809513636c
|