mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-04-29 17:52:37 +02:00
Use Flask-Babel to localize datetime in the History Page
By formatting datetime on the server, we get nice localized datetime strings that are adapted to the currently-selected language. Example: - English: "Apr 26, 2020, 3:58:54 PM" - French: "26 avr. 2020 à 15:58:54" - German: "26.04.2020, 15:58:54" - Spanish: "26 abr. 2020 15:58:54" - Indonesian: "26 Apr 2020 15.58.54" - Chinese: "2020年4月26日 下午3:58:54" However, there is a downside: time is not adapted to the user timezone. The solution is to define a timezone on the server: we use the server OS timezone by default, and it can be customized through the BABEL_DEFAULT_TIMEZONE setting. It's still not ideal, because it assumes that all users are in the same timezone (the one configured on the server).
This commit is contained in:
parent
de13945a91
commit
914482bc76
3 changed files with 7 additions and 4 deletions
|
@ -82,7 +82,7 @@ def get_history(project, human_readable_names=True):
|
||||||
object_str = describe_version(version)
|
object_str = describe_version(version)
|
||||||
|
|
||||||
common_properties = {
|
common_properties = {
|
||||||
"time": version.transaction.issued_at.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
"time": version.transaction.issued_at,
|
||||||
"operation_type": version.operation_type,
|
"operation_type": version.operation_type,
|
||||||
"object_type": object_type,
|
"object_type": object_type,
|
||||||
"object_desc": object_str,
|
"object_desc": object_str,
|
||||||
|
|
|
@ -2,6 +2,7 @@ import os
|
||||||
import os.path
|
import os.path
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
from babel.dates import LOCALTZ
|
||||||
from flask import Flask, g, render_template, request, session
|
from flask import Flask, g, render_template, request, session
|
||||||
from flask_babel import Babel, format_currency
|
from flask_babel import Babel, format_currency
|
||||||
from flask_mail import Mail
|
from flask_mail import Mail
|
||||||
|
@ -150,8 +151,10 @@ def create_app(
|
||||||
app.jinja_env.globals["locale_from_iso"] = locale_from_iso
|
app.jinja_env.globals["locale_from_iso"] = locale_from_iso
|
||||||
app.jinja_env.filters["minimal_round"] = minimal_round
|
app.jinja_env.filters["minimal_round"] = minimal_round
|
||||||
|
|
||||||
# Translations
|
# Translations and time zone (used to display dates). The timezone is
|
||||||
babel = Babel(app)
|
# taken from the BABEL_DEFAULT_TIMEZONE settings, and falls back to
|
||||||
|
# the local timezone of the server OS by using LOCALTZ.
|
||||||
|
babel = Babel(app, default_timezone=str(LOCALTZ))
|
||||||
|
|
||||||
# Undocumented currencyformat filter from flask_babel is forwarding to Babel format_currency
|
# Undocumented currencyformat filter from flask_babel is forwarding to Babel format_currency
|
||||||
# We overwrite it to remove the currency sign ¤ when there is no currency
|
# We overwrite it to remove the currency sign ¤ when there is no currency
|
||||||
|
|
|
@ -161,7 +161,7 @@
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for event in history %}
|
{% for event in history %}
|
||||||
<tr>
|
<tr>
|
||||||
<td><script>document.write(localizeTime("{{ event.time }}"));</script></td>
|
<td>{{ event.time|datetimeformat("medium") }}</td>
|
||||||
<td >
|
<td >
|
||||||
<div class="history_icon">
|
<div class="history_icon">
|
||||||
<i {% if event.operation_type == OperationType.INSERT %}
|
<i {% if event.operation_type == OperationType.INSERT %}
|
||||||
|
|
Loading…
Reference in a new issue