from babel.core import Locale
from babel.dates import format_date, format_datetime, format_time, \
- format_timedelta
+ format_timedelta
from babel.numbers import format_number, format_decimal, format_currency, \
- format_percent, format_scientific
+ format_percent, format_scientific
from babel._compat import PY2, text_type, text_to_native
-__all__ = ['Format', 'LazyProxy', 'NullTranslations', 'Translations']
-
class Format(object):
"""Wrapper class providing the various date and number formatting functions
>>> fmt = Format('en_US')
>>> fmt.date(date(2007, 4, 1))
u'Apr 1, 2007'
-
- :see: `babel.dates.format_date`
"""
return format_date(date, format, locale=self.locale)
>>> 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`
"""
return format_datetime(datetime, format, tzinfo=self.tzinfo,
locale=self.locale)
>>> 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`
"""
return format_time(time, format, tzinfo=self.tzinfo, locale=self.locale)
>>> fmt = Format('en_US')
>>> fmt.timedelta(timedelta(weeks=11))
u'3 months'
-
- :see: `babel.dates.format_timedelta`
"""
return format_timedelta(delta, granularity=granularity,
threshold=threshold,
>>> fmt = Format('en_US')
>>> fmt.number(1099)
u'1,099'
-
- :see: `babel.numbers.format_number`
"""
return format_number(number, locale=self.locale)
>>> fmt = Format('en_US')
>>> fmt.decimal(1.2345)
u'1.234'
-
- :see: `babel.numbers.format_decimal`
"""
return format_decimal(number, format, locale=self.locale)
def currency(self, number, currency):
"""Return a number in the given currency formatted for the locale.
-
- :see: `babel.numbers.format_currency`
"""
return format_currency(number, currency, locale=self.locale)
>>> fmt = Format('en_US')
>>> fmt.percent(0.34)
u'34%'
-
- :see: `babel.numbers.format_percent`
"""
return format_percent(number, format, locale=self.locale)
def scientific(self, number):
"""Return a number formatted using scientific notation for the locale.
-
- :see: `babel.numbers.format_scientific`
"""
return format_scientific(number, locale=self.locale)
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`
"""
if locales is not None:
if not isinstance(locales, (list, tuple)):
: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`
"""
domain = getattr(translations, 'domain', self.DEFAULT_DOMAIN)
if merge and domain == self.domain:
: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`
"""
if isinstance(translations, gettext.GNUTranslations):
self._catalog.update(translations._catalog)