Skip to main content

babel extension

This extension is Babel library frontend. It provides filters for date/time and currency formatting.

Extension configuration​

NameTypeDescriptionApplies to
localeStringLocale identifier.All filters.
tzStringTime zone identifier.format_datetime, format_time
currencyStringCurrency code.format_currency

All values in the extension configuration are optional and can be overwritten by filter parameters. For example: format local time using format_time filter. The filter arguments (locale and tz) override the specified extension settings:

extensions:
babel:
locale: de_DE
tz: Europe/Berlin
dialog:
- condition: true
response: |
{{ utc_time|format_time(format="full", locale="en_US", tz="America/Chicago") }}
πŸ§‘ hello
πŸ€– 10:10:09 AM Central Daylight Time

Locale​

Locale identifier is defined in this format: language[_territory]. It consists of two identifiers: a language code and a territory code. The territory code is optional.

Language codes are defined in ISO 639. You can find them on Wikipedia.

Territory codes are defined in ISO 3166. You can find them on Wikipedia.

For example en_US: en is the language code (English) and US is the country code (United States of America).

If an unknown locale is specified, an error will occur:

extensions:
babel:
locale: xyz
dialog:
- condition: true
response: |
{{ utc_time|format_datetime }}
πŸ§‘ hello
logs
βœ— caused by babel.core.UnknownLocaleError: unknown locale 'xyz'

Time zone​

The set of supported time zones depends on your system. A well-known list of timezones can be found on Wikipedia (TZ identifier column).

If an unknown timezone is specified, an error will occur:

extensions:
babel: {}
dialog:
- condition: true
response: |
{{ utc_time|format_datetime(tz="xyz") }}
πŸ§‘ hello
logs
βœ— caused by builtins.LookupError: unknown timezone 'xyz'

Currency​

The currency code is defined in ISO 4217. A well-known list of currency codes can be found on Wikipedia (Code column).

format_datetime filter​

Filter format_datetime return a date and time formatted according to the given pattern.

Parameters:

NameDefault valueDescriptionNote
valueInput value.Any type suitable for the input value of filter datetime
format"medium"One of "full", "long", "medium", or "short", or a custom date/time pattern.
localeLocale identifier.If the parameter is not set, the value is taken from the extension configuration. If the value is not specified anywhere, LC_TIME value from your system is used.
tzTime zone identifier.If the parameter is not set, the value is taken from the extension configuration.

The filter is implemented by calling function babel.dates.format_datetime.

Usage example: format local date and time for Costa Rica (all formatting options are passed through filter arguments).

extensions:
babel: {}
dialog:
- condition: true
response: |
{{ utc_time|format_datetime(format="full", locale="es_CR", tz="America/Costa_Rica") }}
πŸ§‘ hello
πŸ€– martes, 30 de mayo de 2023, 08:58:46 hora estΓ‘ndar central

An custom pattern can be set in the value of format. The syntax of the pattern is described in documentation of babel library. For example:

extensions:
babel: {}
dialog:
- condition: true
response: |
{{ utc_time|format_datetime(format="yyyy.MM.dd G 'at' HH:mm:ss zzz",
locale="en", tz="America/Costa_Rica") }}
πŸ§‘ hello
πŸ€– 2023.05.30 d.C. at 09:27:49 -0600

format_date filter​

Filter format_date return a date formatted according to the given pattern.

Parameters:

NameDefault valueDescriptionNote
valueInput value.Any type suitable for the input value of filter date
format"medium"One of "full", "long", "medium", or "short", or a custom date pattern.
localeLocale identifier.If the parameter is not set, the value is taken from the extension configuration. If the value is not specified anywhere, LC_TIME value from your system is used.

The filter is implemented by calling function babel.dates.format_date.

Usage example: format local date. Locale (Germany) is set in the extension settings. To get the local time (Berlin), tz filter from datetime extension is used.

extensions:
babel:
locale: de_DE
datetime: {}
dialog:
- condition: true
response: |
{{ utc_time.astimezone("Europe/Berlin"|tz)|format_date(format="full") }}
πŸ§‘ hello
πŸ€– Dienstag, 30. Mai 2023

An custom pattern can be set in the value of format. The syntax of the pattern is described in documentation of babel library. For example:

extensions:
babel:
locale: de_DE
datetime: {}
dialog:
- condition: true
response: |
{{ utc_time.astimezone("Europe/Berlin"|tz)|format_date(format="EEE, MMM d, ''y") }}
πŸ§‘ hello
πŸ€– Di., Mai 30, '2023

format_time filter​

Filter format_time return a time formatted according to the given pattern.

Parameters:

NameDefault valueDescriptionNote
valueInput value.Any type suitable for the input value of filter time
format"medium"One of "full", "long", "medium", or "short", or a custom time pattern.
localeLocale identifier.If the parameter is not set, the value is taken from the extension configuration. If the value is not specified anywhere, LC_TIME value from your system is used.
tzTime zone identifier.If the parameter is not set, the value is taken from the extension configuration.

The filter is implemented by calling function babel.dates.format_time.

Usage example: format local time. Locale (en_US) and time zone (America/Chicago) are set in the extension settings.

extensions:
babel:
locale: en_US
tz: America/Chicago
dialog:
- condition: true
response: |
{{ utc_time|format_time(format="full") }}
πŸ§‘ hello
πŸ€– 10:10:09 AM Central Daylight Time

An custom pattern can be set in the value of format. The syntax of the pattern is described in documentation of babel library. For example:

extensions:
babel:
locale: en_US
tz: America/Chicago
dialog:
- condition: true
response: |
{{ utc_time|format_time(format="hh 'o''clock' a, zzzz") }}
πŸ§‘ hello
πŸ€– 10 o'clock AM, Central Daylight Time

format_currency filter​

Filter format_currency return formatted currency value.

NameDescriptionNote
valueInput value.Type is a Number
currencyCurrency code.If the parameter is not set, the value is taken from the extension configuration.
formatFormat string.
localeLocale identifier.If the parameter is not set, the value is taken from the extension configuration. If the value is not specified anywhere, LC_NUMERIC value from your system is used.

The filter is implemented by calling function babel.dates.format_currency.

Usage example: currency formatting (one Japanese yen). Locale (Japan) and currency (yen) are set in the extension settings.

extensions:
babel:
locale: ja_JP
currency: JPY
dialog:
- condition: true
response: |
{{ 1|format_currency }}
πŸ§‘ hello
πŸ€– οΏ₯1

The format can also be specified explicitly. The syntax of the pattern is described in documentation of babel library. For example:

extensions:
babel:
locale: en
dialog:
- condition: true
response: |
{{ 1099.98|format_currency(currency='EUR', format='#,##0.00 €€€') }}
πŸ§‘ hello
πŸ€– 1,099.98 euros

If the currency value is not specified anywhere, an BotError exception will be raised:

extensions:
babel: {}
dialog:
- condition: true
response: |
{{ 1|format_currency }}
πŸ§‘ hello
logs
βœ— `currency` is not set