Skip to content

Dependency

Dependency function to determine the localization based on the Accept-Language header.

Parameters:

Name Type Description Default
accept_language Annotated[Union[str, None], Header()]

The Accept-Language header value.

None

Returns:

Type Description
Callable[[str, dict], str]

A partially applied gettext function with the selected locale.

Source code in fastapi_l10n/dependency.py
async def localization_dependency(
    accept_language: Annotated[Union[str, None], Header()] = None,
) -> Callable[[str, dict], str]:
    """
    Dependency function to determine the localization based on the Accept-Language header.

    :param accept_language: The Accept-Language header value.
    :return: A partially applied gettext function with the selected locale.
    """
    locale = localization.default_locale

    if accept_language is not None:
        language_preferences = accept_language_parser(accept_language=accept_language)
        for language_preference in language_preferences:
            if language_preference.language in localization.allowed_locales:
                locale = language_preference.language
                logger.debug(f"Selected {locale=}")
                break
        else:
            logger.debug(f"Using default {locale=}")
    else:
        logger.debug(f"Using default {locale=}")
    return partial(localization.gettext, locale=locale)

Dependency to determine the localization based on the Accept-Language header.

Examples:

from fastapi import FastAPI
from fastapi_l10n import L10nDepends

app = FastAPI()

@app.get("/")
async def root(_: L10nDepends):
    return _("hello-message", args={"username": "John"})