Locale¶
Localization and internationalization support for Carbonic.
carbonic.locale ¶
Carbonic localization system.
This module provides comprehensive internationalization support for the Carbonic datetime library, including pluralization rules, number formatting, and localized names for time units, months, and days.
Supported locales: - English (en): Default locale with standard English pluralization - Polish (pl): Complex pluralization rules and Polish translations - Spanish (es): Spanish localization with decimal comma formatting - French (fr): French localization with decimal comma formatting - German (de): German localization with decimal comma formatting - Portuguese (pt): Portuguese localization with decimal comma formatting
Example
from carbonic.locale import get_locale locale = get_locale("pl") locale.get_duration_unit_name("day", 5) 'dni'
Classes¶
Locale ¶
              Bases: ABC
Abstract base class for locales.
Functions¶
            pluralize
  
      abstractmethod
  
¶
    Return the correct plural form based on count and locale rules.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| count | int | float | The number to use for pluralization | required | 
| singular | str | Singular form (e.g., "day") | required | 
| plural | str | Plural form for 2-4 in languages that have it (e.g., "days") | required | 
| many | str | None | Many form for 5+ in languages that have it (e.g., "days") | None | 
Returns:
| Type | Description | 
|---|---|
| str | The correct plural form | 
            format_number
  
      abstractmethod
  
¶
    Format number according to locale conventions (decimal separator, etc.).
            get_duration_unit_name
  
      abstractmethod
  
¶
    Get the localized name for a duration unit.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| unit | str | The unit name ("second", "minute", "hour", "day", "week", "month", "year") | required | 
| count | int | float | The count to determine plural form | required | 
Returns:
| Type | Description | 
|---|---|
| str | Localized unit name | 
            get_month_name
  
      abstractmethod
  
¶
    Get localized month name.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| month | int | Month number (1-12) | required | 
| short | bool | Whether to return short form | False | 
Returns:
| Type | Description | 
|---|---|
| str | Localized month name | 
            get_day_name
  
      abstractmethod
  
¶
    Get localized day name.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| weekday | int | Weekday number (0=Monday, 6=Sunday) | required | 
| short | bool | Whether to return short form | False | 
Returns:
| Type | Description | 
|---|---|
| str | Localized day name | 
GermanLocale ¶
EnglishLocale ¶
              Bases: Locale
English locale implementation.
Functions¶
pluralize ¶
English pluralization: singular for 1, plural for everything else.
format_number ¶
Format number using English conventions (decimal point).
get_duration_unit_name ¶
Get English duration unit names with proper pluralization.
get_month_name ¶
Get English month names using Python's calendar module.
get_day_name ¶
Get English day names using Python's calendar module.
SpanishLocale ¶
FrenchLocale ¶
PolishLocale ¶
              Bases: Locale
Polish locale implementation with complex pluralization rules.
Functions¶
pluralize ¶
Polish pluralization rules: - 1: singular form (1 dzieĆ) - 2-4 (but not 12-14): plural form (2 dni, 3 dni, 4 dni) - 0.5, 1.5, 2.5, etc.: plural form (0,5 sekundy, 1,5 sekundy) - 5+, 0, 12-14: many form (5 dni, 10 dni, 12 dni, 13 dni, 14 dni) - 22, 23, 24, 32, 33, 34, etc.: plural form (22 dni, 23 dni)
format_number ¶
Format number using Polish conventions (decimal comma).
get_duration_unit_name ¶
Get Polish duration unit names with proper pluralization.
PortugueseLocale ¶
              Bases: Locale
Portuguese locale implementation.
Functions¶
get_locale ¶
Get a locale by code, defaulting to English if None or not found.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| code | str | None | Locale code (e.g., "en", "pl") or None for default | required | 
Returns:
| Type | Description | 
|---|---|
| Locale | Locale instance | 
Raises:
| Type | Description | 
|---|---|
| ValueError | If locale code is not supported | 
is_locale_available ¶
Check if a locale is available.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| code | str | Locale code (e.g., "en", "pl") | required | 
Returns:
| Type | Description | 
|---|---|
| bool | True if the locale is registered, False otherwise |