Skip to content

Commit

Permalink
major-update: german translation and last integration steps for pytho…
Browse files Browse the repository at this point in the history
…n-babel
  • Loading branch information
joschobart committed Jul 26, 2024
1 parent 8d4b620 commit 02a4a12
Show file tree
Hide file tree
Showing 13 changed files with 437 additions and 187 deletions.
15 changes: 11 additions & 4 deletions fun_with_flags/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,28 @@ def create_app(test_config=None):

# Babel l10n
def get_locale():
supported_langs = ["en"] + os.listdir(f"{app.root_path}/translations")

if not session.get("lang", None):
if request.accept_languages.best_match(supported_langs):
session["lang"] = request.accept_languages.best_match(supported_langs)

if request.args.get("lang"):
session["lang"] = request.args.get("lang")
if request.args.get("lang") in supported_langs:
session["lang"] = request.args.get("lang")

return session.get("lang", "en")

app.config['BABEL_DEFAULT_LOCALE'] = 'en'
babel = Babel()
babel.init_app(app, locale_selector=get_locale)

# entry-point
@app.route("/", methods=("GET", "POST"))
@decs.choose_team
@decs.set_unicorn
# @decs.set_config_from_db
def index():
"""index.html view."""

return render_template("index.html")

@app.route("/favicon.ico")
Expand Down
4 changes: 2 additions & 2 deletions fun_with_flags/challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@decs.login_required
@decs.choose_team
@decs.use_db
@decs.set_unicorn
@decs.set_config_from_db
@decs.error_check
def overview():
""" """
Expand Down Expand Up @@ -121,7 +121,7 @@ def overview():
@decs.login_required
@decs.choose_team
@decs.use_db
@decs.set_unicorn
@decs.set_config_from_db
@decs.error_check
def challenge():
""" """
Expand Down
77 changes: 50 additions & 27 deletions fun_with_flags/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
def bootstrap_generic_document(_id, _couch, _object, _namespace="payload"):
"""
:param _id:
:param _couch:
:param _object:
:param _id:
:param _couch:
:param _object:
:param _namespace: (Default value = "payload")
"""
Expand All @@ -35,9 +35,9 @@ def bootstrap_generic_document(_id, _couch, _object, _namespace="payload"):
def bootstrap_user_document(_userid, _couch, _settings):
"""
:param _userid:
:param _couch:
:param _settings:
:param _userid:
:param _couch:
:param _settings:
"""
# Bootstrap db-document if it doesn't exist
Expand Down Expand Up @@ -139,12 +139,33 @@ def get_unicorn_state():
return is_unicorn


def get_language():
""" """
if "user_id" in g:
_userid = g.user_id
_couch = g.couch
else:
_userid = session["my_team"]["user"]["user_id"]
_couch = get_db()

if _userid in _couch:
_my_document = _couch[_userid]

if "settings" in _my_document:
try:
language = _my_document["settings"]["locale"]["language"]
except Exception:
language = None

return language


def get_settings(_userid, _couch, _settings):
"""
:param _userid:
:param _couch:
:param _settings:
:param _userid:
:param _couch:
:param _settings:
"""
_my_document = g.couch[g.user_id]
Expand All @@ -170,10 +191,10 @@ def get_settings(_userid, _couch, _settings):
def get_match_history(_userid, _couch, _flagid, _place):
"""
:param _userid:
:param _couch:
:param _flagid:
:param _place:
:param _userid:
:param _couch:
:param _flagid:
:param _place:
"""
_my_document = _couch[_userid]
Expand Down Expand Up @@ -208,7 +229,9 @@ def get_match_history(_userid, _couch, _flagid, _place):

if _my_match["match_type"] == "4" or _my_match["match_type"] == "8":
_my_match["match_type"] = "Friendly (normal rules)"
elif _my_match["match_type"] == "5" or _my_match["match_type"] == "9":
elif (
_my_match["match_type"] == "5" or _my_match["match_type"] == "9"
):
_my_match["match_type"] = "Friendly (cup rules)"

_played_matches = sorted(
Expand All @@ -221,11 +244,11 @@ def get_match_history(_userid, _couch, _flagid, _place):
def set_match_history(_userid, _couch, _league_id, _match_id, _place):
"""
:param _userid:
:param _couch:
:param _league_id:
:param _match_id:
:param _place:
:param _userid:
:param _couch:
:param _league_id:
:param _match_id:
:param _place:
"""
# Instantiate clone of db-document
Expand Down Expand Up @@ -267,11 +290,11 @@ def set_match_history(_userid, _couch, _league_id, _match_id, _place):
def init_stripe_session(_userid, _couch, _stripe_user, _session_id, _transaction_id):
"""
:param _userid:
:param _couch:
:param _stripe_user:
:param _session_id:
:param _transaction_id:
:param _userid:
:param _couch:
:param _stripe_user:
:param _session_id:
:param _transaction_id:
"""
# Instantiate clone of db-document
Expand All @@ -294,9 +317,9 @@ def init_stripe_session(_userid, _couch, _stripe_user, _session_id, _transaction
def close_stripe_session(_userid, _couch, _session_id):
"""
:param _userid:
:param _couch:
:param _session_id:
:param _userid:
:param _couch:
:param _session_id:
"""
# sleep 3 secs as stripe sometimes needs time to submit to the webhook
Expand Down
13 changes: 12 additions & 1 deletion fun_with_flags/decs.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def wrapped_view(**kwargs):
return wrapped_view


def set_unicorn(view):
def set_config_from_db(view):
"""
:param view:
Expand All @@ -146,6 +146,17 @@ def wrapped_view(**kwargs):
else:
session["unicorn"] = False

lang = db.get_language()
if (
session.get("lang")
and session["lang"] != lang
and lang is not None
and lang != "None"
):
session["lang"] = lang

return redirect(request.url)

return view(**kwargs)

return wrapped_view
4 changes: 2 additions & 2 deletions fun_with_flags/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@bp_f.route("/overview", methods=("GET", "POST"))
@decs.login_required
@decs.choose_team
@decs.set_unicorn
@decs.set_config_from_db
@decs.error_check
def overview():
""" """
Expand All @@ -36,7 +36,7 @@ def overview():
@decs.login_required
@decs.choose_team
@decs.use_db
@decs.set_unicorn
@decs.set_config_from_db
@decs.error_check
def details():
""" """
Expand Down
6 changes: 3 additions & 3 deletions fun_with_flags/helperf.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,12 @@ def random_quotes(_quotes):
"""
for _key in _quotes.keys():
random_quote_index = randrange(0, len(_quotes[_key]))
random_quote_index = randrange(0, len(_quotes[_key][session.get("lang", "en")]))

if _key == "quotes_ante":
quote_ante = _quotes[_key][random_quote_index]
quote_ante = _quotes[_key][session.get("lang", "en")][random_quote_index]
else:
quote_post = _quotes[_key][random_quote_index]
quote_post = _quotes[_key][session.get("lang", "en")][random_quote_index]

return quote_ante, quote_post

Expand Down
2 changes: 1 addition & 1 deletion fun_with_flags/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@decs.login_required
@decs.choose_team
@decs.use_db
@decs.set_unicorn
@decs.set_config_from_db
@decs.error_check
def settings():
""" """
Expand Down
4 changes: 2 additions & 2 deletions fun_with_flags/stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def hook():
@decs.login_required
@decs.choose_team
@decs.use_db
@decs.set_unicorn
@decs.set_config_from_db
@decs.error_check
def success():
""" """
Expand All @@ -161,7 +161,7 @@ def success():
@decs.login_required
@decs.choose_team
@decs.use_db
@decs.set_unicorn
@decs.set_config_from_db
@decs.error_check
def fail():
""" """
Expand Down
61 changes: 33 additions & 28 deletions fun_with_flags/templates/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,41 @@ <h2>{% block title %}{% trans %}Settings{% endtrans %}{% endblock %}</h2>

{% if g.my_document is defined and g.db_settings is defined %}
<form method="POST">
{% for object in g.db_settings['settings'].items() %}
{% if object[0] != "meta" %}
<h3>{{ object[1]['name'] }}</h3>
<table border="1px">
{% for key, value in object[1]['schema'].items() %}
<tr>
<td style="text-align:left" width="500px">
{{ value[0] }}:
</td>
<td style="text-align:left" width="30px">
<div id="hideMe" tabindex="0" title="{{ value[1] }}">
<img src="{{ url_for('static', filename='info.png') }}" alt="Info" height="20px">
</div>
</td>
<td style="text-align:left">
<select name="{{ object[0] }}.{{ key }}" id="{{ object[0] }}.{{ key }}">
{% for item in value[2] %}
{% if g.my_document['settings'][object[0]][key] is defined %}
{% if g.my_document['settings'][object[0]][key] == item %}
<option value="{{ item }}" selected>{{ item }}</option>
{% else %}
<option value="{{ item }}">{{ item }}</option>
{% endif%}
{% endif%}
{% for key, value in g.db_settings['settings'].items() %}
{% if key != "meta" %}
{% for lang_key, values_dict in value.items() %}
{% if lang_key == session.get("lang", "en") %}
<h3>{{ values_dict['name'] }}</h3>
<table border="1px">
{% for sub_key, sub_value in values_dict['schema'].items() %}
<tr>
<td style="text-align:left" width="500px">
{{ sub_value[0] }}:
</td>
<td style="text-align:left" width="30px">
<div id="hideMe" tabindex="0" title="{{ sub_value[1] }}">
<img src="{{ url_for('static', filename='info.png') }}" alt="Info" height="20px">
</div>
</td>
<td style="text-align:left">
<select name="{{ key }}.{{ sub_key }}" id="{{ key }}.{{ sub_key }}">
{% for item in sub_value[2] %}
{% if g.my_document['settings'][key][sub_key] is defined %}
{% if g.my_document['settings'][key][sub_key] == item %}
<option value="{{ item }}" selected>{{ item }}</option>
{% else %}
<option value="{{ item }}">{{ item }}</option>
{% endif%}
{% endif%}
{% endfor %}
</select>
</td>
</tr>
{% endfor %}
</select>
</td>
</tr>
</table>

{% endif %}
{% endfor %}
</table>
{% endif %}
{% endfor %}
{% trans %}<p><input type="submit" value="Update Settings" id="update_settings"></p>{% endtrans %}
Expand Down
Binary file modified fun_with_flags/translations/de/LC_MESSAGES/messages.mo
Binary file not shown.
Loading

0 comments on commit 02a4a12

Please sign in to comment.