Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • ec325a8
  • /
  • byceps
  • /
  • util
  • /
  • l10n.py
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:7633dc6b00413bc6a4e0a198a1c6113b3eb464ae
directory badge
swh:1:dir:311f5825a93c38f2f525a744a4e68987da6cc405

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
l10n.py
"""
byceps.util.l10n
~~~~~~~~~~~~~~~~

Localization.

:Copyright: 2014-2025 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from collections.abc import Iterator
from contextlib import contextmanager

from babel import Locale
from flask import current_app, g, request
from flask_babel import force_locale, format_currency, get_locale
from moneyed import Money
from wtforms import Form

from byceps.services.user.models.user import User


def get_current_user_locale() -> str | None:
    """Return the locale for the current user, if available."""
    # Look for a locale on the current user object.
    user = g.user
    if (user is not None) and (user.locale is not None):
        return user.locale

    if request:
        # Try to match user agent's accepted languages.
        languages = [locale.language for locale in g.locales]
        return request.accept_languages.best_match(languages)

    return None


@contextmanager
def force_user_locale(user: User) -> Iterator[None]:
    """Execute code with the user's preferred locale."""
    locale = get_user_locale(user)
    with force_locale(locale):
        yield


def get_default_locale() -> str:
    """Return the application's default locale."""
    return current_app.config['LOCALE']


def get_user_locale(user: User) -> str:
    """Return the user's preferred locale.

    If no preference is set for the user, return the app's default
    locale.
    """
    return user.locale or get_default_locale()


BASE_LOCALE = Locale('en')


def get_locales() -> list[Locale]:
    """List available locales."""
    return [BASE_LOCALE] + current_app.babel_instance.list_translations()


def get_locale_str() -> str | None:
    """Return the current locale as a string.

    Return `None` outside of a request.
    """
    locale = get_locale()
    if locale is None:
        return None

    locale_str = locale.language
    if locale.territory:
        locale_str += '_' + locale.territory

    return locale_str


class LocalizedForm(Form):
    def __init__(self, *args, **kwargs):
        locale = current_app.config['LOCALE']
        kwargs['meta'] = {'locales': [locale]}
        super().__init__(*args, **kwargs)


def format_money(money: Money) -> str:
    """Format monetary value with amount and currency."""
    return format_currency(money.amount, money.currency.code)

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API