CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-flask-babel

Adds i18n/l10n support for Flask applications.

Pending
Overview
Eval results
Files

translation.mddocs/

Translation Functions

String translation functions supporting pluralization, context, and lazy evaluation for Flask applications.

Capabilities

Basic Translation

Translate strings with variable substitution.

def gettext(string, **variables):
    """
    Translate a string with current locale.
    
    Parameters:
    - string: String to translate
    - **variables: Variables for string formatting
    
    Returns:
    Translated string
    
    Usage:
    gettext('Hello World!')
    gettext('Hello %(name)s!', name='John')
    """

# Common alias for gettext
_ = gettext

Plural Translation

Translate strings with plural forms based on count.

def ngettext(singular, plural, num, **variables):
    """
    Translate string with plural forms.
    
    Parameters:
    - singular: Singular form string
    - plural: Plural form string  
    - num: Number determining singular/plural
    - **variables: Variables for string formatting (num is automatically added)
    
    Returns:
    Translated string in appropriate form
    
    Usage:
    ngettext('%(num)d item', '%(num)d items', count)
    ngettext('One apple', '%(num)d apples', apple_count)
    """

Context Translation

Translate strings with context to disambiguate identical strings.

def pgettext(context, string, **variables):
    """
    Translate string with context.
    
    Parameters:
    - context: Context string for disambiguation
    - string: String to translate
    - **variables: Variables for string formatting
    
    Returns:
    Translated string
    
    Usage:
    pgettext('button', 'Save')  # Save button
    pgettext('menu', 'Save')    # Save menu item
    """

def npgettext(context, singular, plural, num, **variables):
    """
    Translate string with context and plural forms.
    
    Parameters:
    - context: Context string for disambiguation
    - singular: Singular form string
    - plural: Plural form string
    - num: Number determining singular/plural
    - **variables: Variables for string formatting
    
    Returns:
    Translated string in appropriate form
    """

Lazy Translation

Return lazy-evaluated translation objects that resolve when converted to strings.

def lazy_gettext(string, **variables):
    """
    Return lazy-evaluated translation.
    
    Parameters:
    - string: String to translate
    - **variables: Variables for string formatting
    
    Returns:
    LazyString object that translates when converted to string
    
    Usage:
    lazy_message = lazy_gettext('Hello World!')
    # Translation happens when lazy_message is used as string
    """

def lazy_ngettext(singular, plural, num, **variables):
    """
    Return lazy-evaluated plural translation.
    
    Parameters:
    - singular: Singular form string
    - plural: Plural form string
    - num: Number determining singular/plural
    - **variables: Variables for string formatting
    
    Returns:
    LazyString object
    """

def lazy_pgettext(context, string, **variables):
    """
    Return lazy-evaluated context translation.
    
    Parameters:
    - context: Context string
    - string: String to translate
    - **variables: Variables for string formatting
    
    Returns:
    LazyString object
    """

def lazy_npgettext(context, singular, plural, num, **variables):
    """
    Return lazy-evaluated context plural translation.
    
    Parameters:
    - context: Context string
    - singular: Singular form string
    - plural: Plural form string
    - num: Number determining singular/plural
    - **variables: Variables for string formatting
    
    Returns:
    LazyString object
    """

Translation Context Access

Get translation objects and context information.

def get_translations():
    """
    Get translations object for current request.
    
    Returns:
    babel.support.Translations or NullTranslations object
    """

Types

class LazyString:
    """
    Lazy-evaluated string that resolves translation when used.
    
    Supports all string operations and converts to string automatically
    when used in string contexts. Implements full string interface.
    """
    def __init__(self, func, *args, **kwargs): ...
    def __str__(self): ...
    def __repr__(self): ...
    def __html__(self): ...
    def __len__(self): ...
    def __getitem__(self, key): ...
    def __iter__(self): ...
    def __contains__(self, item): ...
    def __add__(self, other): ...
    def __radd__(self, other): ...
    def __mul__(self, other): ...
    def __rmul__(self, other): ...
    def __mod__(self, other): ...
    def __rmod__(self, other): ...
    def __lt__(self, other): ...
    def __le__(self, other): ...
    def __eq__(self, other): ...
    def __ne__(self, other): ...
    def __gt__(self, other): ...
    def __ge__(self, other): ...
    def __hash__(self): ...
    def __getattr__(self, attr): ...

Usage Examples

Basic Translation

from flask_babel import gettext as _

# Simple translation
message = _('Welcome to our site!')

# With variables
greeting = _('Hello %(username)s!', username=current_user.name)

Plural Forms

from flask_babel import ngettext

def show_results(count):
    message = ngettext(
        'Found %(num)d result',
        'Found %(num)d results', 
        count
    )
    return message

Context Disambiguation

from flask_babel import pgettext

# Different contexts for same English word
save_button = pgettext('button', 'Save')
save_menu = pgettext('menu', 'Save')
save_file = pgettext('file action', 'Save')

Lazy Evaluation

from flask_babel import lazy_gettext

# Define at module level
ERROR_MESSAGES = {
    'required': lazy_gettext('This field is required'),
    'invalid': lazy_gettext('Invalid input'),
}

# Translation happens when message is used
def validate_field(value):
    if not value:
        return str(ERROR_MESSAGES['required'])  # Translates here

Form Integration

from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
from flask_babel import lazy_gettext as _l

class MyForm(FlaskForm):
    username = StringField(_l('Username'), validators=[DataRequired()])
    submit = SubmitField(_l('Submit'))

Install with Tessl CLI

npx tessl i tessl/pypi-flask-babel

docs

configuration.md

formatting.md

index.md

translation.md

tile.json