This module provides internationalization and localization support for webapp2.
To use it, you must add the babel and pytz packages to your application directory (for App Engine) or install it in your virtual environment (for other servers).
You can download babel and pytz from the following locations:
http://babel.edgewall.org/ http://pypi.python.org/pypi/gaepytz
Default configuration values for this module. Keys are:
Internalization store.
Caches loaded translations and configuration to be used between requests.
A dictionary with all loaded translations.
Path to where traslations are stored.
Translation domains to merge.
Default locale code.
Default timezone code.
Dictionary of default date formats.
A callable that returns the locale for a request.
A callable that returns the timezone for a request.
Initializes the i18n store.
| Parameters: |
|
|---|
Sets the function that defines the locale for a request.
| Parameters: |
|
|---|
Sets the function that defines the timezone for a request.
| Parameters: |
|
|---|
Returns a translation catalog for a locale.
| Parameters: |
|
|---|---|
| Returns: | A babel.support.Translations instance, or gettext.NullTranslations if none was found. |
Loads a translation catalog.
| Parameters: |
|
|---|---|
| Returns: | A babel.support.Translations instance, or gettext.NullTranslations if none was found. |
Internalization provider for a single request.
The current locale code.
The current translations.
The current timezone code.
The current tzinfo object.
Initializes the i18n provider for a request.
| Parameters: |
|
|---|
Sets the locale code for this request.
| Parameters: |
|
|---|
Sets the timezone code for this request.
| Parameters: |
|
|---|
Translates a given string according to the current locale.
| Parameters: |
|
|---|---|
| Returns: | The translated string. |
Translates a possible pluralized string according to the current locale.
| Parameters: |
|
|---|---|
| Returns: | The translated string. |
Returns a datetime object converted to the local timezone.
| Parameters: |
|
|---|---|
| Returns: | A datetime object normalized to a timezone. |
Returns a datetime object converted to UTC and without tzinfo.
| Parameters: |
|
|---|---|
| Returns: | A naive datetime object (no timezone), converted to UTC. |
Returns a date formatted according to the given pattern and following the current locale.
| Parameters: |
|
|---|---|
| Returns: | A formatted date in unicode. |
Returns a date and time formatted according to the given pattern and following the current locale and timezone.
| Parameters: |
|
|---|---|
| Returns: | A formatted date and time in unicode. |
Returns a time formatted according to the given pattern and following the current locale and timezone.
| Parameters: |
|
|---|---|
| Returns: | A formatted time in unicode. |
Formats the elapsed time from the given date to now or the given timedelta. This currently requires an unreleased development version of Babel.
| Parameters: |
|
|---|---|
| Returns: | A string with the elapsed time. |
Returns the given number formatted for the current locale. Example:
>>> format_number(1099, locale='en_US')
u'1,099'
| Parameters: |
|
|---|---|
| Returns: | The formatted number. |
Returns the given decimal number formatted for the current locale. Example:
>>> format_decimal(1.2345, locale='en_US')
u'1.234'
>>> format_decimal(1.2346, locale='en_US')
u'1.235'
>>> format_decimal(-1.2346, locale='en_US')
u'-1.235'
>>> format_decimal(1.2345, locale='sv_SE')
u'1,234'
>>> format_decimal(12345, locale='de')
u'12.345'
The appropriate thousands grouping and the decimal separator are used for each locale:
>>> format_decimal(12345.5, locale='en_US')
u'12,345.5'
| Parameters: |
|
|---|---|
| Returns: | The formatted decimal number. |
Returns a formatted currency value. Example:
>>> format_currency(1099.98, 'USD', locale='en_US')
u'$1,099.98'
>>> format_currency(1099.98, 'USD', locale='es_CO')
u'US$\xa01.099,98'
>>> format_currency(1099.98, 'EUR', locale='de_DE')
u'1.099,98\xa0\u20ac'
The pattern can also be specified explicitly:
>>> format_currency(1099.98, 'EUR', u'\xa4\xa4 #,##0.00',
... locale='en_US')
u'EUR 1,099.98'
| Parameters: |
|
|---|---|
| Returns: | The formatted currency value. |
Returns formatted percent value for the current locale. Example:
>>> format_percent(0.34, locale='en_US')
u'34%'
>>> format_percent(25.1234, locale='en_US')
u'2,512%'
>>> format_percent(25.1234, locale='sv_SE')
u'2\xa0512\xa0%'
The format pattern can also be specified explicitly:
>>> format_percent(25.1234, u'#,##0\u2030', locale='en_US')
u'25,123\u2030'
| Parameters: |
|
|---|---|
| Returns: | The formatted percent number. |
Returns value formatted in scientific notation for the current locale. Example:
>>> format_scientific(10000, locale='en_US')
u'1E4'
The format pattern can also be specified explicitly:
>>> format_scientific(1234567, u'##0E00', locale='en_US')
u'1.23E06'
| Parameters: |
|
|---|---|
| Returns: | Value formatted in scientific notation. |
Parses a date from a string.
This function uses the date format for the locale as a hint to determine the order in which the date fields appear in the string. Example:
>>> parse_date('4/1/04', locale='en_US')
datetime.date(2004, 4, 1)
>>> parse_date('01.04.2004', locale='de_DE')
datetime.date(2004, 4, 1)
| Parameters: |
|
|---|---|
| Returns: | The parsed date object. |
Parses a date and time from a string.
This function uses the date and time formats for the locale as a hint to determine the order in which the time fields appear in the string.
| Parameters: |
|
|---|---|
| Returns: | The parsed datetime object. |
Parses a time from a string.
This function uses the time format for the locale as a hint to determine the order in which the time fields appear in the string. Example:
>>> parse_time('15:30:00', locale='en_US')
datetime.time(15, 30)
| Parameters: |
|
|---|---|
| Returns: | The parsed time object. |
Parses localized number string into a long integer. Example:
>>> parse_number('1,099', locale='en_US')
1099L
>>> parse_number('1.099', locale='de_DE')
1099L
When the given string cannot be parsed, an exception is raised:
>>> parse_number('1.099,98', locale='de')
Traceback (most recent call last):
...
NumberFormatError: '1.099,98' is not a valid number
| Parameters: |
|
|---|---|
| Returns: | The parsed number. |
| Raises : | NumberFormatError if the string can not be converted to a number. |
Parses localized decimal string into a float. Example:
>>> parse_decimal('1,099.98', locale='en_US')
1099.98
>>> parse_decimal('1.099,98', locale='de')
1099.98
When the given string cannot be parsed, an exception is raised:
>>> parse_decimal('2,109,998', locale='de')
Traceback (most recent call last):
...
NumberFormatError: '2,109,998' is not a valid decimal number
| Parameters: |
|
|---|---|
| Returns: | The parsed decimal number. |
| Raises : | NumberFormatError if the string can not be converted to a decimal number. |
Returns a representation of the given timezone using “location format”.
The result depends on both the local display name of the country and the city assocaited with the time zone:
>>> from pytz import timezone
>>> tz = timezone('America/St_Johns')
>>> get_timezone_location(tz, locale='de_DE')
u"Kanada (St. John's)"
>>> tz = timezone('America/Mexico_City')
>>> get_timezone_location(tz, locale='de_DE')
u'Mexiko (Mexiko-Stadt)'
If the timezone is associated with a country that uses only a single timezone, just the localized country name is returned:
>>> tz = timezone('Europe/Berlin')
>>> get_timezone_name(tz, locale='de_DE')
u'Deutschland'
| Parameters: |
|
|---|---|
| Returns: | The localized timezone name using location format. |
Returns an instance of I18nStore from the app registry.
It’ll try to get it from the current app registry, and if it is not registered it’ll be instantiated and registered. A second call to this function will return the same instance.
| Parameters: |
|
|---|
Sets an instance of I18nStore in the app registry.
| Parameters: |
|
|---|
Returns an instance of I18n from the request registry.
It’ll try to get it from the current request registry, and if it is not registered it’ll be instantiated and registered. A second call to this function will return the same instance.
| Parameters: |
|
|---|
Sets an instance of I18n in the request registry.
| Parameters: |
|
|---|
A lazy version of gettext().
| Parameters: |
|
|---|---|
| Returns: | A babel.support.LazyProxy object that when accessed translates the string. |
See I18n.gettext().
See I18n.ngettext().
See I18n.to_utc().
See I18n.format_date().
See I18n.format_time().
See I18n.format_number().
See I18n.parse_time().
See I18n.parse_number().
See I18n.parse_decimal().