Skip to content

Internationalisation

Activate, get and deactivate translations.

activate(locale, path=None)

Activate internationalisation.

Set locale as current locale. Search for locale in directory path.

Parameters:

Name Type Description Default
locale str | None

Language name, e.g. en_GB. If None, defaults to no translation. Similar to calling deactivate().

required
path str | Path

Path to search for locales.

None

Returns:

Name Type Description
dict NullTranslations

Translations.

Raises:

Type Description
FileNotFoundError

If humanize cannot find the locale folder.

Source code in .tox/docs/lib/python3.14/site-packages/humanize/i18n.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def activate(
    locale: str | None, path: str | os.PathLike[str] | None = None
) -> gettext_module.NullTranslations:
    """Activate internationalisation.

    Set `locale` as current locale. Search for locale in directory `path`.

    Args:
        locale (str | None): Language name, e.g. `en_GB`. If `None`, defaults to no
            translation. Similar to calling ``deactivate()``.
        path (str | pathlib.Path): Path to search for locales.

    Returns:
        dict: Translations.

    Raises:
        FileNotFoundError: If humanize cannot find the locale folder.
    """
    if locale is None or locale.startswith("en"):
        _CURRENT.locale = None
        return _TRANSLATIONS[None]

    if path is None:
        path = _get_default_locale_path()

    if path is None:
        msg = (
            "Humanize cannot determinate the default location of the 'locale' folder. "
            "You need to pass the path explicitly."
        )
        raise FileNotFoundError(msg)
    if locale not in _TRANSLATIONS:
        translation = gettext_module.translation("humanize", path, [locale])
        _TRANSLATIONS[locale] = translation
    _CURRENT.locale = locale
    return _TRANSLATIONS[locale]

deactivate()

Deactivate internationalisation.

Source code in .tox/docs/lib/python3.14/site-packages/humanize/i18n.py
95
96
97
def deactivate() -> None:
    """Deactivate internationalisation."""
    _CURRENT.locale = None

decimal_separator()

Return the decimal separator for a locale, default to dot.

Returns:

Name Type Description
str str

Decimal separator.

Source code in .tox/docs/lib/python3.14/site-packages/humanize/i18n.py
191
192
193
194
195
196
197
def decimal_separator() -> str:
    """Return the decimal separator for a locale, default to dot.

    Returns:
         str: Decimal separator.
    """
    return _DECIMAL_SEPARATOR.get(getattr(_CURRENT, "locale", None), ".")

thousands_separator()

Return the thousands separator for a locale, default to comma.

Returns:

Name Type Description
str str

Thousands separator.

Source code in .tox/docs/lib/python3.14/site-packages/humanize/i18n.py
182
183
184
185
186
187
188
def thousands_separator() -> str:
    """Return the thousands separator for a locale, default to comma.

    Returns:
         str: Thousands separator.
    """
    return _THOUSANDS_SEPARATOR.get(getattr(_CURRENT, "locale", None), ",")