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.

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

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)

Translations

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
rtype:Translations

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
rtype:Translations


See ApiDocs/0.8, Documentation

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