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

Language name, e.g. en_GB.

required
path str

Path to search for locales.

None

Returns:

Name Type Description
dict NullTranslations

Translations.

Raises:

Type Description
Exception

If humanize cannot find the locale folder.

Source code in .tox/docs/lib/python3.12/site-packages/humanize/i18n.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def activate(locale: str, path: str | None = None) -> gettext_module.NullTranslations:
    """Activate internationalisation.

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

    Args:
        locale (str): Language name, e.g. `en_GB`.
        path (str): Path to search for locales.

    Returns:
        dict: Translations.

    Raises:
        Exception: If humanize cannot find the locale folder.
    """
    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 Exception(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.12/site-packages/humanize/i18n.py
82
83
84
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.12/site-packages/humanize/i18n.py
182
183
184
185
186
187
188
189
190
191
192
def decimal_separator() -> str:
    """Return the decimal separator for a locale, default to dot.

    Returns:
         str: Decimal separator.
    """
    try:
        sep = _DECIMAL_SEPARATOR[_CURRENT.locale]
    except (AttributeError, KeyError):
        sep = "."
    return sep

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.12/site-packages/humanize/i18n.py
169
170
171
172
173
174
175
176
177
178
179
def thousands_separator() -> str:
    """Return the thousands separator for a locale, default to comma.

    Returns:
         str: Thousands separator.
    """
    try:
        sep = _THOUSANDS_SEPARATOR[_CURRENT.locale]
    except (AttributeError, KeyError):
        sep = ","
    return sep