From afad8aeec67e4ba8181f3cdbbc237f4ec59558ac Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 30 Jun 2022 02:01:55 +0200 Subject: use properly localized decimal separators --- development.ini | 2 ++ fietsboek/jinja2.py | 20 ++++++++++++++++++++ fietsboek/models/track.py | 12 +++++++----- fietsboek/static/favicon.png | Bin 0 -> 20338 bytes fietsboek/static/pyramid-16x16.png | Bin 1319 -> 0 bytes fietsboek/static/pyramid.png | Bin 12901 -> 0 bytes fietsboek/templates/admin.jinja2 | 4 ++-- fietsboek/templates/details.jinja2 | 16 ++++++++-------- fietsboek/templates/edit_form.jinja2 | 2 +- fietsboek/templates/home.jinja2 | 6 +++--- fietsboek/templates/layout.jinja2 | 3 ++- fietsboek/templates/util.jinja2 | 2 +- setup.py | 2 +- 13 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 fietsboek/jinja2.py create mode 100644 fietsboek/static/favicon.png delete mode 100644 fietsboek/static/pyramid-16x16.png delete mode 100644 fietsboek/static/pyramid.png diff --git a/development.ini b/development.ini index 3cec096..608f400 100644 --- a/development.ini +++ b/development.ini @@ -13,6 +13,8 @@ pyramid.debug_routematch = false pyramid.default_locale_name = en pyramid.includes = pyramid_debugtoolbar +jinja2.filters = + format_decimal = fietsboek.jinja2.filter_format_decimal sqlalchemy.url = sqlite:///%(here)s/fietsboek.sqlite diff --git a/fietsboek/jinja2.py b/fietsboek/jinja2.py new file mode 100644 index 0000000..b940d74 --- /dev/null +++ b/fietsboek/jinja2.py @@ -0,0 +1,20 @@ +"""Custom filters for Jinja2.""" +import jinja2 + +from babel.numbers import format_decimal + + +@jinja2.pass_context +def filter_format_decimal(ctx, value): + """Format a decimal number according to the locale. + + This uses the right thousands grouping and the right decimal separator. + + :param ctx: The jinja context, passed automatically. + :type ctx: jinja2.runtime.Context + :param value: The value to format. + :type value: float + """ + request = ctx.get('request') + locale = request.localizer.locale_name + return format_decimal(value, locale=locale) diff --git a/fietsboek/models/track.py b/fietsboek/models/track.py index 6f500b9..66bf12e 100644 --- a/fietsboek/models/track.py +++ b/fietsboek/models/track.py @@ -20,6 +20,7 @@ import gzip import datetime from markupsafe import Markup +from babel.numbers import format_decimal from .meta import Base from .. import util @@ -244,14 +245,15 @@ class Track(Base): :return: The generated HTML. :rtype: Markup """ + number = lambda n: format_decimal(n, locale=localizer.locale_name) rows = [ - (_("tooltip.table.length"), f'{round(self.length / 1000, 2)} km'), - (_("tooltip.table.uphill"), f'{round(self.uphill, 2)} m'), - (_("tooltip.table.downhill"), f'{round(self.downhill, 2)} m'), + (_("tooltip.table.length"), f'{number(round(self.length / 1000, 2))} km'), + (_("tooltip.table.uphill"), f'{number(round(self.uphill, 2))} m'), + (_("tooltip.table.downhill"), f'{number(round(self.downhill, 2))} m'), (_("tooltip.table.moving_time"), f'{self.moving_time}'), (_("tooltip.table.stopped_time"), f'{self.stopped_time}'), - (_("tooltip.table.max_speed"), f'{round(util.mps_to_kph(self.max_speed), 2)} km/h'), - (_("tooltip.table.avg_speed"), f'{round(util.mps_to_kph(self.avg_speed), 2)} km/h'), + (_("tooltip.table.max_speed"), f'{number(round(util.mps_to_kph(self.max_speed), 2))} km/h'), + (_("tooltip.table.avg_speed"), f'{number(round(util.mps_to_kph(self.avg_speed), 2))} km/h'), ] rows = [ "{}{}".format(localizer.translate(name), value) diff --git a/fietsboek/static/favicon.png b/fietsboek/static/favicon.png new file mode 100644 index 0000000..136fe91 Binary files /dev/null and b/fietsboek/static/favicon.png differ diff --git a/fietsboek/static/pyramid-16x16.png b/fietsboek/static/pyramid-16x16.png deleted file mode 100644 index 9792031..0000000 Binary files a/fietsboek/static/pyramid-16x16.png and /dev/null differ diff --git a/fietsboek/static/pyramid.png b/fietsboek/static/pyramid.png deleted file mode 100644 index 4ab837b..0000000 Binary files a/fietsboek/static/pyramid.png and /dev/null differ diff --git a/fietsboek/templates/admin.jinja2 b/fietsboek/templates/admin.jinja2 index a347ebf..04a9ac3 100644 --- a/fietsboek/templates/admin.jinja2 +++ b/fietsboek/templates/admin.jinja2 @@ -1,5 +1,5 @@ {% extends "layout.jinja2" %} -{% import "util.jinja2" as util %} +{% import "util.jinja2" as util with context %} {% block content %}

{{ _("page.admin.title") }}

@@ -9,7 +9,7 @@
{% for badge in badges %} - {{ util.render_badge(request, badge) }} + {{ util.render_badge(badge) }}
diff --git a/fietsboek/templates/details.jinja2 b/fietsboek/templates/details.jinja2 index 595f888..e335880 100644 --- a/fietsboek/templates/details.jinja2 +++ b/fietsboek/templates/details.jinja2 @@ -1,5 +1,5 @@ {% extends "layout.jinja2" %} -{% import "util.jinja2" as util %} +{% import "util.jinja2" as util with context %} {% block content %}
@@ -52,7 +52,7 @@
- {{ _("page.details.download") }} + {{ _("page.details.download") }} @@ -70,15 +70,15 @@ - + - + - + @@ -90,18 +90,18 @@ - + - +
{{ _("page.details.length") }}{{ (track.length / 1000) | round(2) }} km{{ (track.length / 1000) | round(2) | format_decimal }} km
{{ _("page.details.uphill") }}{{ track.uphill | round(2) }} m{{ track.uphill | round(2) | format_decimal }} m
{{ _("page.details.downhill") }}{{ track.downhill | round(2) }} m{{ track.downhill | round(2) | format_decimal }} m
{{ _("page.details.moving_time") }}
{{ _("page.details.max_speed") }}{{ mps_to_kph(track.max_speed) | round(2) }} km/h{{ mps_to_kph(track.max_speed) | round(2) | format_decimal }} km/h
{{ _("page.details.avg_speed") }}{{ mps_to_kph(track.avg_speed) | round(2) }} km/h{{ mps_to_kph(track.avg_speed) | round(2) | format_decimal }} km/h
{% if track.badges %}
{% for badge in track.badges %} - {{ util.render_badge(request, badge) }} + {{ util.render_badge(badge) }} {% endfor %}
{% endif %} diff --git a/fietsboek/templates/edit_form.jinja2 b/fietsboek/templates/edit_form.jinja2 index 7559fe7..0a52319 100644 --- a/fietsboek/templates/edit_form.jinja2 +++ b/fietsboek/templates/edit_form.jinja2 @@ -22,7 +22,7 @@
{{ _("page.track.form.tags") }}
{% for tag in tags %} - {{ tag }} + {{ tag }} {% endfor %}
diff --git a/fietsboek/templates/home.jinja2 b/fietsboek/templates/home.jinja2 index ccd9af0..e8c7d0c 100644 --- a/fietsboek/templates/home.jinja2 +++ b/fietsboek/templates/home.jinja2 @@ -6,10 +6,10 @@ {% if summary %}
{% for year in summary %} - {{ year.year }} — {{ (year.total_length / 1000) | round(2) }} km + {{ year.year }} — {{ (year.total_length / 1000) | round(2) | format_decimal }} km
{% for month in year %} - {{ month_name(request, month.month) }} — {{ (month.total_length / 1000) | round(2) }} km + {{ month_name(request, month.month) }} — {{ (month.total_length / 1000) | round(2) | format_decimal }} km
{% for track in month %} {{ track.date.day }}: {{ track.title | default(track.date, true) }} @@ -20,7 +20,7 @@ {% endfor %}

- {{ _("page.home.total") }}: {{ (summary.total_length / 1000) | round(2) }} km + {{ _("page.home.total") }}: {{ (summary.total_length / 1000) | round(2) | format_decimal }} km

{% endif %}
diff --git a/fietsboek/templates/layout.jinja2 b/fietsboek/templates/layout.jinja2 index 6b8c9b2..8551fb5 100644 --- a/fietsboek/templates/layout.jinja2 +++ b/fietsboek/templates/layout.jinja2 @@ -5,6 +5,7 @@ + {% block title %}Fietsboek{% endblock %} @@ -28,7 +29,7 @@