Edgewall Software
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.


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


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)

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'

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'

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(time(15, 30))
u'11:30:00 AM'

number(self, number)

Return an integer number formatted for the locale.

>>> fmt = Format('en_US')
>>> fmt.number(1099)

decimal(self, number, format=None)

Return a decimal number formatted for the locale.

>>> fmt = Format('en_US')
>>> fmt.decimal(1.2345)

currency(self, number, currency)

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


percent(self, number, format=None)

Return a number formatted as percentage for the locale.

>>> fmt = Format('en_US')
>>> fmt.percent(0.34)

scientific(self, number)

Return a number formatted using scientific notation for the locale.



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!


(Not documented)


An extended translation catalog class.

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

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
return:the loaded catalog, or a NullTranslations instance if no matching translations were found

merge(self, translations)

Merge the given translations into the catalog.

Message translations in the specfied 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

See ApiDocs/0.8, Documentation

Last modified 18 months ago Last modified on Dec 10, 2015 6:25:47 AM