Edgewall Software
Last modified 8 years ago Last modified on 06/08/07 18:00:55
Please note:

The documentation here is generated live, in a way that is neither comprehensive nor 100% correct. We strongly recommend using the HTML documentation included with the source tarballs instead.

babel.support

Several classes and functions that help with integrating and using Babel in applications.

Format

Wrapper class providing the various date and number formatting functions bound to a specific locale and time-zone.

>>> fmt = Format('en_US', UTC)
>>> fmt.date(date(2007, 4, 1))
u'Apr 1, 2007'
>>> fmt.decimal(1.2345)
u'1.234'

date(self, date=None, format='medium')

Return a date formatted according to the given pattern.

>>> fmt = Format('en_US')
>>> fmt.date(date(2007, 4, 1))
u'Apr 1, 2007'
see:babel.dates.format_date

datetime(self, datetime=None, format='medium')

Return a date and time formatted according to the given pattern.

>>> from pytz import timezone
>>> fmt = Format('en_US', tzinfo=timezone('US/Eastern'))
>>> fmt.datetime(datetime(2007, 4, 1, 15, 30))
u'Apr 1, 2007 11:30:00 AM'
see:babel.dates.format_datetime

time(self, time=None, format='medium')

Return a time formatted according to the given pattern.

>>> from pytz import timezone
>>> fmt = Format('en_US', tzinfo=timezone('US/Eastern'))
>>> fmt.time(datetime(2007, 4, 1, 15, 30))
u'11:30:00 AM'
see:babel.dates.format_time

timedelta(self, delta, granularity='second', threshold=0.85)

Return a time delta according to the rules of the given locale.

>>> fmt = Format('en_US')
>>> fmt.timedelta(timedelta(weeks=11))
u'3 mths'
see:babel.dates.format_timedelta

number(self, number)

Return an integer number formatted for the locale.

>>> fmt = Format('en_US')
>>> fmt.number(1099)
u'1,099'
see:babel.numbers.format_number

decimal(self, number, format=None)

Return a decimal number formatted for the locale.

>>> fmt = Format('en_US')
>>> fmt.decimal(1.2345)
u'1.234'
see:babel.numbers.format_decimal

currency(self, number, currency)

Return a number in the given currency formatted for the locale.

see:babel.numbers.format_currency

percent(self, number, format=None)

Return a number formatted as percentage for the locale.

>>> fmt = Format('en_US')
>>> fmt.percent(0.34)
u'34%'
see:babel.numbers.format_percent

scientific(self, number)

Return a number formatted using scientific notation for the locale.

see:babel.numbers.format_scientific

LazyProxy

Class for proxy objects that delegate to a specified function to evaluate the actual object.

>>> def greeting(name='world'):
...     return 'Hello, %s!' % name
>>> lazy_greeting = LazyProxy(greeting, name='Joe')
>>> print lazy_greeting
Hello, Joe!
>>> u'  ' + lazy_greeting
u'  Hello, Joe!'
>>> u'(%s)' % lazy_greeting
u'(Hello, Joe!)'

This can be used, for example, to implement lazy translation functions that delay the actual translation until the string is actually used. The rationale for such behavior is that the locale of the user may not always be available. In web applications, you only know the locale when processing a request.

The proxy implementation attempts to be as complete as possible, so that the lazy objects should mostly work as expected, for example for sorting:

>>> greetings = [
...     LazyProxy(greeting, 'world'),
...     LazyProxy(greeting, 'Joe'),
...     LazyProxy(greeting, 'universe'),
... ]
>>> greetings.sort()
>>> for greeting in greetings:
...     print greeting
Hello, Joe!
Hello, universe!
Hello, world!

value(self)

(Not documented)

NullTranslations

(Not documented)

dgettext(self, domain, message)

Like gettext(), but look the message up in the specified domain.

ldgettext(self, domain, message)

Like lgettext(), but look the message up in the specified domain.

udgettext(self, domain, message)

Like ugettext(), but look the message up in the specified domain.

dngettext(self, domain, singular, plural, num)

Like ngettext(), but look the message up in the specified domain.

ldngettext(self, domain, singular, plural, num)

Like lngettext(), but look the message up in the specified domain.

udngettext(self, domain, singular, plural, num)

Like ungettext() but look the message up in the specified domain.

pgettext(self, context, message)

Look up the context and message id in the catalog and return the corresponding message string, as an 8-bit string encoded with the catalog's charset encoding, if known. If there is no entry in the catalog for the message id and context , and a fallback has been set, the look up is forwarded to the fallback's pgettext() method. Otherwise, the message id is returned.

lpgettext(self, context, message)

Equivalent to pgettext(), but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with bind_textdomain_codeset().

npgettext(self, context, singular, plural, num)

Do a plural-forms lookup of a message id. singular is used as the message id for purposes of lookup in the catalog, while num is used to determine which plural form to use. The returned message string is an 8-bit string encoded with the catalog's charset encoding, if known.

If the message id for context is not found in the catalog, and a fallback is specified, the request is forwarded to the fallback's npgettext() method. Otherwise, when num is 1 singular is returned, and plural is returned in all other cases.

lnpgettext(self, context, singular, plural, num)

Equivalent to npgettext(), but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with bind_textdomain_codeset().

upgettext(self, context, message)

Look up the context and message id in the catalog and return the corresponding message string, as a Unicode string. If there is no entry in the catalog for the message id and context, and a fallback has been set, the look up is forwarded to the fallback's upgettext() method. Otherwise, the message id is returned.

unpgettext(self, context, singular, plural, num)

Do a plural-forms lookup of a message id. singular is used as the message id for purposes of lookup in the catalog, while num is used to determine which plural form to use. The returned message string is a Unicode string.

If the message id for context is not found in the catalog, and a fallback is specified, the request is forwarded to the fallback's unpgettext() method. Otherwise, when num is 1 singular is returned, and plural is returned in all other cases.

dpgettext(self, domain, context, message)

Like pgettext(), but look the message up in the specified domain.

udpgettext(self, domain, context, message)

Like upgettext(), but look the message up in the specified domain.

ldpgettext(self, domain, context, message)

Equivalent to dpgettext(), but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with bind_textdomain_codeset().

dnpgettext(self, domain, context, singular, plural, num)

Like npgettext, but look the message up in the specified domain.

udnpgettext(self, domain, context, singular, plural, num)

Like unpgettext, but look the message up in the specified domain.

ldnpgettext(self, domain, context, singular, plural, num)

Equivalent to dnpgettext(), but the translation is returned in the preferred system encoding, if no other encoding was explicitly set with bind_textdomain_codeset().

Translations

An extended translation catalog class.

load(cls, dirname=None, locales=None, domain=None)

Load translations from the given directory.

param dirname:the directory containing the MO files
param locales:the list of locales in order of preference (items in this list can be either Locale objects or locale strings)
param domain:the message domain (default: 'messages')
return:the loaded catalog, or a NullTranslations instance if no matching translations were found
rtype:Translations

add(self, translations, merge=True)

Add the given translations to the catalog.

If the domain of the translations is different than that of the current catalog, they are added as a catalog that is only accessible by the various d*gettext functions.

param translations:
 the Translations instance with the messages to add
param merge:whether translations for message domains that have already been added should be merged with the existing translations
return:the Translations instance (self) so that merge calls can be easily chained
rtype:Translations

merge(self, translations)

Merge the given translations into the catalog.

Message translations in the specified catalog override any messages with the same identifier in the existing catalog.

param translations:
 the Translations instance with the messages to merge
return:the Translations instance (self) so that merge calls can be easily chained
rtype:Translations


See ApiDocs, Documentation