CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-babel

Internationalization utilities for Python providing locale data, date/time/number formatting, and message catalog management

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

Babel

A comprehensive Python internationalization and localization library that provides an integrated collection of utilities for internationalizing and localizing Python applications. Babel consists of two major components: tools for building and working with gettext message catalogs for translating user interfaces, and a Python interface to the CLDR (Common Locale Data Repository) providing access to various locale display names, localized number and date formatting, currency symbols, and other locale-specific data.

Package Information

  • Package Name: babel
  • Language: Python
  • Installation: pip install babel

Core Imports

import babel
from babel import Locale, UnknownLocaleError

For specific functionality:

from babel.core import Locale, default_locale, negotiate_locale, parse_locale
from babel.dates import format_date, format_datetime, format_time
from babel.numbers import format_decimal, format_currency, format_percent
from babel.messages import Catalog, Message

Basic Usage

from babel import Locale
from babel.dates import format_date, format_datetime
from babel.numbers import format_decimal, format_currency
import datetime

# Create locale objects
locale = Locale('en_US')
french_locale = Locale('fr_FR')

# Format dates according to locale
date = datetime.date(2023, 12, 25)
formatted_date = format_date(date, locale=locale)  # "Dec 25, 2023"
formatted_date_fr = format_date(date, locale=french_locale)  # "25 déc. 2023"

# Format numbers and currencies
number = 1234.56
formatted_number = format_decimal(number, locale=locale)  # "1,234.56"
formatted_currency = format_currency(number, 'USD', locale=locale)  # "$1,234.56"

# Access locale information
print(locale.get_display_name())  # "English (United States)"
print(locale.get_language_name())  # "English"
print(locale.get_territory_name())  # "United States"

Architecture

Babel's architecture centers around locale objects and formatting functions that leverage CLDR data:

  • Locale: Central class providing access to locale-specific data and formatting rules
  • CLDR Integration: Comprehensive locale data repository providing culturally appropriate formatting
  • Formatting Functions: Specialized functions for dates, numbers, currencies, and units
  • Message Catalogs: Complete gettext-compatible translation management system
  • Extensible Design: Modular structure allowing selective imports and customization

This design enables Babel to serve as the primary internationalization library for Python applications, supporting both simple locale-aware formatting tasks and complex multilingual application development workflows.

Capabilities

Core Locale Management

Fundamental locale operations including locale creation, parsing, negotiation, and access to locale-specific display information and formatting rules.

class Locale:
    def __init__(self, language, territory=None, script=None, variant=None, modifier=None): ...
    @classmethod
    def parse(cls, identifier, sep='_', resolve_likely_subtags=True): ...
    def get_display_name(self, locale=None): ...
    def get_language_name(self, locale=None): ...

def default_locale(category=None, aliases=None): ...
def negotiate_locale(preferred, available, sep='_', aliases=None): ...
def parse_locale(identifier, sep='_'): ...

Core Locale Management

Date and Time Formatting

Comprehensive date, time, and datetime formatting with locale-aware patterns, timezone support, and flexible formatting options including skeleton-based formatting and relative time display.

def format_date(date, format='medium', locale=default_locale()): ...
def format_datetime(datetime, format='medium', tzinfo=None, locale=default_locale()): ...
def format_time(time, format='medium', tzinfo=None, locale=default_locale()): ...
def format_timedelta(delta, granularity='second', threshold=0.85, add_direction=False, format='long', locale=default_locale()): ...

Date and Time Formatting

Number and Currency Formatting

Locale-aware number, decimal, currency, and percentage formatting with support for different numbering systems, compact notation, and precision control.

def format_decimal(number, format=None, locale=default_locale(), numbering_system="latn"): ...
def format_currency(number, currency, format=None, locale=default_locale(), currency_digits=True, format_type='standard'): ...
def format_percent(number, format=None, locale=default_locale(), numbering_system="latn"): ...
def format_scientific(number, format=None, locale=default_locale(), numbering_system="latn"): ...

Number and Currency Formatting

Message Catalog Management

Complete gettext-compatible message catalog system for managing translatable strings, including catalog creation, message extraction, PO/MO file handling, and translation management.

class Catalog:
    def __init__(self, locale=None, domain=None, **kwargs): ...
    def add(self, id, string=None, locations=(), flags=(), auto_comments=(), user_comments=()): ...
    def get(self, id, context=None): ...

class Message:
    def __init__(self, id, string='', locations=(), flags=(), auto_comments=(), user_comments=()): ...

Message Catalog Management

Plural Rules and Language Data

Plural rule handling for different languages, language territory information, and list formatting according to locale conventions.

class PluralRule:
    def __init__(self, rules): ...
    def __call__(self, n): ...

def get_official_languages(territory, regional=False, de_facto=False): ...
def format_list(lst, style='standard', locale=default_locale()): ...

Plural Rules and Language Data

Units and Utilities

Unit formatting for measurements, utility functions for text processing and pattern matching, and support classes for translation management.

def format_unit(value, measurement_unit, length='long', format=None, locale=default_locale()): ...
def format_compound_unit(numerator_value, numerator_unit, denominator_value, denominator_unit, length='long', format=None, locale=default_locale()): ...

class Format:
    def __init__(self, locale, tzinfo=None, numbering_system="latn"): ...
    def date(self, date=None, format='medium'): ...
    def currency(self, number, currency): ...

Units and Utilities

Exception Classes

class UnknownLocaleError(Exception):
    """Raised when an unknown locale identifier is used."""

class UnknownCurrencyError(Exception):
    """Raised when an unknown currency code is used."""

class UnknownUnitError(ValueError):
    """Raised when an unknown unit of measurement is used."""

class UnsupportedNumberingSystemError(Exception):
    """Raised when an unsupported numbering system is used."""

class UnknownCurrencyFormatError(KeyError):
    """Raised when an unknown currency format is requested."""

class NumberFormatError(ValueError):
    """Raised when number formatting fails."""

class ParseError(ValueError):
    """Raised when date/time parsing fails."""

class RuleError(Exception):
    """Raised when plural rule parsing or evaluation fails."""

class TranslationError(Exception):
    """Raised for translation-related errors."""

class PoFileError(Exception):
    """Raised when PO file parsing fails."""

docs

core-locales.md

date-time-formatting.md

index.md

message-catalogs.md

number-currency-formatting.md

plural-language-data.md

units-utilities.md

tile.json