1
|
|
|
""" |
2
|
|
|
byceps.util.templatefunctions |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
Provide and register custom template global functions. |
6
|
|
|
|
7
|
|
|
:Copyright: 2006-2021 Jochen Kupperschmidt |
8
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
9
|
|
|
""" |
10
|
|
|
|
11
|
1 |
|
import babel |
12
|
1 |
|
from flask_babel import get_locale, get_timezone |
13
|
|
|
|
14
|
|
|
|
15
|
1 |
|
def register(app): |
16
|
|
|
"""Make functions available as global functions in templates.""" |
17
|
1 |
|
functions = [ |
18
|
|
|
(_format_date, 'format_date'), |
19
|
|
|
(_format_datetime, 'format_datetime'), |
20
|
|
|
(_format_time, 'format_time'), |
21
|
|
|
(_format_number, 'format_number'), |
22
|
|
|
] |
23
|
|
|
|
24
|
1 |
|
for f, name in functions: |
25
|
1 |
|
app.add_template_global(f, name) |
26
|
|
|
|
27
|
|
|
|
28
|
1 |
|
def _format_date(*args) -> str: |
29
|
|
|
return babel.dates.format_date(*args, locale=get_locale()) |
30
|
|
|
|
31
|
|
|
|
32
|
1 |
|
def _format_datetime(*args) -> str: |
33
|
|
|
return babel.dates.format_datetime( |
34
|
|
|
*args, locale=get_locale(), tzinfo=get_timezone() |
35
|
|
|
) |
36
|
|
|
|
37
|
|
|
|
38
|
1 |
|
def _format_time(*args) -> str: |
39
|
|
|
return babel.dates.format_time( |
40
|
|
|
*args, locale=get_locale(), tzinfo=get_timezone() |
41
|
|
|
) |
42
|
|
|
|
43
|
|
|
|
44
|
1 |
|
def _format_number(*args) -> str: |
45
|
|
|
return babel.numbers.format_number(*args, locale=get_locale()) |
46
|
|
|
|