Chirp internationalization (i18n).
Locale detection, message translation, and basic formatting.
Usage::
app = App(AppConfig(
i18n_enabled=True,
i18n_supported_locales=("en", "es", "ja"),
))
In templates::
<h1>{{ t("Welcome to Chirp") }}</h1>
<p>{{ t("Hello, {name}!", name=user.name) }}</p>
In handlers::
from chirp.i18n import get_locale, t
locale = get_locale() # "es"
message = t("Hello, {name}!", name="Alice")
i18n
| Name | Type | Default | Description |
|---|---|---|---|
type
|
|
— | |
qualified_name
|
|
— | |
element_type
|
|
— | |
description
|
|
— | |
source_file
|
|
— | |
line_number
|
|
— | |
is_autodoc
|
|
— | |
autodoc_element
|
|
— | |
_autodoc_template
|
|
— | |
_autodoc_url_path
|
|
— | |
_autodoc_page_type
|
|
— | |
title
|
|
— | |
doc_content_hash
|
|
— |
Symbols on this page
Initialize the global message catalog.
Return the global message catalog.
Return the current request locale.
Returns "en" if no locale has been set (outside request context).
Set the locale for the current context.
Translate a key using the current locale.
Template global:{{ t("Hello, {name}!", name=user.name) }}
Returns the key itself if no catalog is configured or…
init_catalog
function
def init_catalog(directory: str = 'locales') -> MessageCatalog
Initialize the global message catalog.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
directory
|
str
|
'locales'
|
get_catalog
function
def get_catalog() -> MessageCatalog | None
Return the global message catalog.
No parameters.
get_locale
function
def get_locale() -> str
Return the current request locale.
Returns "en" if no locale has been set (outside request context).
No parameters.
set_locale
function
def set_locale(locale: str) -> None
Set the locale for the current context.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
locale
|
str
|
— |
t
function
def t(key: str, **kwargs) -> str
Translate a key using the current locale.
Template global:{{ t("Hello, {name}!", name=user.name) }}
Returns the key itself if no catalog is configured or key is missing.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
key
|
str
|
— | |
**kwargs
|
|
— |
View source · /home/runner/work/chirp/chirp/site/../src/chirp/i18n/__init__.py:1