babel extension
This extension is Babel library frontend. It provides filters for date/time and currency formatting.
Extension configurationβ
Name | Type | Description | Applies to |
---|---|---|---|
locale | String | Locale identifier. | All filters. |
tz | String | Time zone identifier. | format_datetime, format_time |
currency | String | Currency 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:
Name | Default value | Description | Note |
---|---|---|---|
value | Input 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. | |
locale | Locale 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. | |
tz | Time 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:
Name | Default value | Description | Note |
---|---|---|---|
value | Input 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. | |
locale | Locale 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:
Name | Default value | Description | Note |
---|---|---|---|
value | Input 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. | |
locale | Locale 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. | |
tz | Time 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.
Name | Description | Note |
---|---|---|
value | Input value. | Type is a Number |
currency | Currency code. | If the parameter is not set, the value is taken from the extension configuration. |
format | Format string. | |
locale | Locale 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