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

Overview
Eval results
Files

core-locales.mddocs/

Core Locale Management

Fundamental locale operations for working with locale identifiers, creating locale objects, and accessing locale-specific information. The Locale class serves as the central interface to CLDR data and locale-aware functionality throughout Babel.

Capabilities

Locale Class

The primary class for representing locales and accessing locale-specific data from the CLDR.

class Locale:
    """
    Represents a locale with access to CLDR data.
    
    Attributes:
        language (str): Language code (e.g. 'en')
        territory (str): Territory code (e.g. 'US') 
        script (str): Script code (e.g. 'Latn')
        variant (str): Variant code
        modifier (str): Modifier
    """
    def __init__(self, language, territory=None, script=None, variant=None, modifier=None):
        """
        Initialize a locale.
        
        Args:
            language (str): Language code
            territory (str, optional): Territory/country code
            script (str, optional): Script code  
            variant (str, optional): Variant code
            modifier (str, optional): Modifier
        """
    
    @classmethod
    def parse(cls, identifier, sep='_', resolve_likely_subtags=True):
        """
        Parse a locale identifier string into a Locale object.
        
        Args:
            identifier (str): Locale identifier (e.g. 'en_US', 'fr-FR')
            sep (str): Separator character ('_' or '-')
            resolve_likely_subtags (bool): Whether to resolve likely subtags
            
        Returns:
            Locale: Parsed locale object
        """
    
    def get_display_name(self, locale=None):
        """
        Get the display name of this locale.
        
        Args:
            locale (Locale, optional): Locale for display names
            
        Returns:
            str: Display name (e.g. "English (United States)")
        """
    
    def get_language_name(self, locale=None):
        """
        Get the language display name.
        
        Args:
            locale (Locale, optional): Locale for display names
            
        Returns:
            str: Language name (e.g. "English")
        """
    
    def get_script_name(self, locale=None):
        """
        Get the script display name.
        
        Args:
            locale (Locale, optional): Locale for display names
            
        Returns:
            str: Script name (e.g. "Latin")
        """
    
    def get_territory_name(self, locale=None):
        """
        Get the territory display name.
        
        Args:
            locale (Locale, optional): Locale for display names
            
        Returns:
            str: Territory name (e.g. "United States")
        """
    
    def plural_form(self, n):
        """
        Get the plural form for a number in this locale.
        
        Args:
            n (int|float): Number
            
        Returns:
            str: Plural form ('zero', 'one', 'two', 'few', 'many', 'other')
        """
    
    # Properties providing access to locale data
    @property
    def english_name(self):
        """str: English name of the locale"""
    
    @property
    def display_name(self):
        """str: Display name in the locale's own language"""
    
    @property
    def text_direction(self):
        """str: Text direction ('ltr' or 'rtl')"""
    
    @property
    def languages(self):
        """dict: Language code to display name mapping"""
    
    @property
    def territories(self):
        """dict: Territory code to display name mapping"""
    
    @property
    def scripts(self):
        """dict: Script code to display name mapping"""
    
    @property
    def currencies(self):
        """dict: Currency code to display name mapping"""
    
    @property
    def currency_symbols(self):
        """dict: Currency code to symbol mapping"""
    
    @property
    def number_symbols(self):
        """dict: Number formatting symbols (decimal, group, etc.)"""
    
    @property
    def decimal_formats(self):
        """dict: Decimal number format patterns"""
    
    @property
    def currency_formats(self):
        """dict: Currency format patterns"""
    
    @property
    def percent_formats(self):
        """dict: Percentage format patterns"""
    
    @property
    def scientific_formats(self):
        """dict: Scientific notation format patterns"""
    
    @property
    def datetime_formats(self):
        """dict: Datetime format patterns"""
    
    @property
    def date_formats(self):
        """dict: Date format patterns"""
    
    @property
    def time_formats(self):
        """dict: Time format patterns"""
    
    @property
    def days(self):
        """dict: Weekday names in various formats"""
    
    @property
    def months(self):
        """dict: Month names in various formats"""
    
    @property
    def quarters(self):
        """dict: Quarter names in various formats"""
    
    @property
    def eras(self):
        """dict: Era names (BC/AD, etc.)"""
    
    @property
    def time_zones(self):
        """dict: Time zone display names"""
    
    @property
    def first_week_day(self):
        """int: First day of week (0=Monday, 6=Sunday)"""
    
    @property
    def weekend_start(self):
        """int: Weekend start day (0=Monday, 6=Sunday)"""
    
    @property
    def weekend_end(self):
        """int: Weekend end day (0=Monday, 6=Sunday)"""
    
    @property
    def character_order(self):
        """str: Text direction ('left-to-right' or 'right-to-left')"""
    
    @property
    def list_patterns(self):
        """dict: Patterns for formatting lists"""
    
    @property
    def unit_display_names(self):
        """dict: Display names for units of measurement"""
    
    @property
    def default_numbering_system(self):
        """str: Default numbering system for this locale"""
    
    @property
    def other_numbering_systems(self):
        """dict: Other available numbering systems"""

Usage example:

from babel import Locale

# Create locale objects
us_locale = Locale('en', 'US')
french_locale = Locale.parse('fr_FR')

# Access display information
print(us_locale.get_display_name())  # "English (United States)"
print(french_locale.get_language_name())  # "French"
print(us_locale.text_direction)  # "ltr"

# Access formatting data
print(us_locale.number_symbols['decimal'])  # "."
print(french_locale.number_symbols['decimal'])  # ","

Locale Parsing and Identification

Functions for working with locale identifier strings and building locale identifiers from components.

def parse_locale(identifier, sep='_'):
    """
    Parse a locale identifier into its components.
    
    Args:
        identifier (str): Locale identifier string
        sep (str): Separator character
        
    Returns:
        tuple: (language, territory, script, variant) components
    """

def get_locale_identifier(tup, sep='_'):
    """
    Build a locale identifier from components.
    
    Args:
        tup (tuple): (language, territory, script, variant) tuple
        sep (str): Separator character
        
    Returns:
        str: Locale identifier string
    """

Usage example:

from babel.core import parse_locale, get_locale_identifier

# Parse locale identifiers
components = parse_locale('en_US')  # ('en', 'US', None, None)
components = parse_locale('zh-Hans-CN', sep='-')  # ('zh', 'CN', 'Hans', None)

# Build locale identifiers
identifier = get_locale_identifier(('fr', 'CA', None, None))  # 'fr_CA'

Default Locale Detection

Get the system's default locale with fallback handling.

def default_locale(category=None, aliases=None):
    """
    Get the system default locale.
    
    Args:
        category (str, optional): Locale category (LC_ALL, LC_CTYPE, etc.)
        aliases (dict, optional): Locale aliases mapping
        
    Returns:
        str: Default locale identifier with fallback to 'en_US_POSIX'
    """

Usage example:

from babel.core import default_locale

system_locale = default_locale()  # e.g. 'en_US'

Locale Negotiation

Negotiate the best matching locale from a list of preferred and available locales.

def negotiate_locale(preferred, available, sep='_', aliases=None):
    """
    Negotiate the best matching locale from preferences.
    
    Args:
        preferred (iterable): Preferred locale identifiers in priority order
        available (iterable): Available locale identifiers
        sep (str): Separator character
        aliases (dict, optional): Locale aliases mapping
        
    Returns:
        str|None: Best matching locale identifier or None if no match
    """

Usage example:

from babel.core import negotiate_locale

preferred = ['de_DE', 'de', 'en_US', 'en']
available = ['en', 'fr', 'de_AT']
best_match = negotiate_locale(preferred, available)  # 'de_AT'

CLDR Data Access

Access global CLDR data by key.

def get_global(key):
    """
    Access global CLDR data by key.
    
    Args:
        key (str): CLDR data key
        
    Returns:
        Mapping: CLDR data for the specified key
    """

Exception Classes

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

Install with Tessl CLI

npx tessl i tessl/pypi-babel

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