Skip to content

Commit

Permalink
first implementation of python-babel (gettext)
Browse files Browse the repository at this point in the history
  • Loading branch information
joschobart committed Jul 22, 2024
1 parent 35318a3 commit 8d583e3
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ wheels/

# venv
.venv

# babel
*.pot
3 changes: 3 additions & 0 deletions babel.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[python: **.py]
[jinja2: **.html]
encoding = utf-8
13 changes: 12 additions & 1 deletion fun_with_flags/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import os

from apscheduler.schedulers.background import BackgroundScheduler
from flask import Flask, render_template, send_from_directory
from flask import Flask, render_template, request, send_from_directory, session
from flask_babel import Babel
from werkzeug.middleware.proxy_fix import ProxyFix

from . import auth, challenge, decs, flags, scheduler, settings, stripe
Expand Down Expand Up @@ -52,6 +53,16 @@ def create_app(test_config=None):
logging.basicConfig()
logging.getLogger("apscheduler").setLevel(logging.INFO)

# Babel l10n
def get_locale():
if request.args.get("lang"):
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
Expand Down
8 changes: 7 additions & 1 deletion fun_with_flags/auth.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
""" FwF authentication views """


from flask import (Blueprint, flash, g, redirect, render_template, request,
session, url_for)
from flask_babel import gettext

from . import api, decs, helperf

Expand All @@ -12,6 +12,10 @@
@bp_a.route("/authorize", methods=("GET", "POST"))
def authorize():
""" """

success_message = gettext("Hello Translation!")
print(success_message)

_protocol = request.args.get("protocol")
_url = request.args.get("url")

Expand All @@ -29,6 +33,7 @@ def authorize():
access_token_key, access_token_secret = api.oauth_get_access_token(g.pin)
except Exception as e:
error = f"{e}: Pin {g.pin} was not accepted."

flash(error)
else:
creds = f"{access_token_key} {access_token_secret}"
Expand All @@ -48,6 +53,7 @@ def callback():

except Exception as e:
error = f"{e}: Pin {g.pin} was not accepted."

flash(error)

else:
Expand Down
3 changes: 2 additions & 1 deletion fun_with_flags/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ <h2>

<p><img src="https://i0.wp.com/wilwheaton.net/wp-content/uploads/2012/10/Wil-Wheaton-and-Sheldon-Cooper-have-Fun-With-Flags.jpg?ssl=1" class="images" alt="Sheldon and Wil &copy; https://wilwheaton.net"></img></p>


{% trans %}
<p><b>Purpose:</b> Fun with Flags allows you to automate capturing flags on <a href="http://hattrick.org" target="_blank">hattrick.org</a> if you are a nerdy supporter of the infamous online-game like myself.</p>
{% endtrans %}

<p>The project was created out of frustration about having to click around for a total of hours only to challenge the available maximum of 25 opponents for the friendlies every single week.</p>

Expand Down
Binary file not shown.
39 changes: 39 additions & 0 deletions fun_with_flags/translations/de/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# German translations for CHPP Fun with Flags.
# Copyright (C) 2024 joschobart
# This file is distributed under the same license as the CHPP Fun with Flags project.
# FIRST AUTHOR joschobart, 2024.
#
msgid ""
msgstr ""
"Project-Id-Version: 0.0.1\n"
"Report-Msgid-Bugs-To: joschobart\n"
"POT-Creation-Date: 2024-07-18 00:33+0200\n"
"PO-Revision-Date: 2024-07-18 00:34+0200\n"
"Last-Translator: joschobart\n"
"Language: de\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.15.0\n"

#: fun_with_flags/auth.py:16
msgid "Hello Translation!"
msgstr "Hallo Übersetzung!"

#: fun_with_flags/templates/index.html:34
msgid ""
"\n"
" <p><b>Purpose:</b> Fun with Flags allows you to automate capturing "
"flags on <a href=\"http://hattrick.org\" "
"target=\"_blank\">hattrick.org</a> if you are a nerdy supporter of the "
"infamous online-game like myself.</p>\n"
" "
msgstr ""
"\n"
" <p><b>Zweck:</b> Fun with Flags ermöglicht dir das Sammeln von "
"flaggen auf <a href=\"http://hattrick.org\" "
"target=\"_blank\">hattrick.org</a> wenn du ein nerdiger Unterstützer "
"des berühmten Onlinespiels bist, so wie ich.</p>\n"
" "

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ dependencies = [
"pygal_maps_world>=1.0.2",
"apscheduler>=3.10.4",
"stripe>=8.1.0",
"flask-babel>=4.0.0",
"jinja2>=3.1.4",
]
readme = "README.rst"
requires-python = ">= 3.12"
Expand All @@ -32,6 +34,7 @@ dev-dependencies = [
"pylint>=3.0.3",
"ruff>=0.1.11",
"pyment>=0.3.3",
"setuptools>=71.0.0",
]

[tool.hatch.metadata]
Expand Down
11 changes: 10 additions & 1 deletion requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ apscheduler==3.10.4
# via hattrick-fwf
astroid==3.0.2
# via pylint
babel==2.15.0
# via flask-babel
beautifulsoup4==4.12.2
# via bs4
black==23.12.1
Expand All @@ -36,6 +38,9 @@ cryptography==41.0.7
dill==0.3.7
# via pylint
flask==3.0.0
# via flask-babel
# via hattrick-fwf
flask-babel==4.0.0
# via hattrick-fwf
gunicorn==21.2.0
# via hattrick-fwf
Expand All @@ -49,8 +54,10 @@ isort==5.13.2
# via pylint
itsdangerous==2.1.2
# via flask
jinja2==3.1.2
jinja2==3.1.4
# via flask
# via flask-babel
# via hattrick-fwf
lxml==4.9.3
# via hattrick-libs
markupsafe==2.1.3
Expand Down Expand Up @@ -78,12 +85,14 @@ pylint==3.0.3
pyment==0.3.3
pytz==2023.3.post1
# via apscheduler
# via flask-babel
rauth==0.7.3
# via hattrick-libs
requests==2.31.0
# via rauth
# via stripe
ruff==0.1.11
setuptools==71.0.0
six==1.16.0
# via apscheduler
soupsieve==2.5
Expand Down
10 changes: 9 additions & 1 deletion requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
-e file:.
apscheduler==3.10.4
# via hattrick-fwf
babel==2.15.0
# via flask-babel
beautifulsoup4==4.12.2
# via bs4
blinker==1.7.0
Expand All @@ -30,6 +32,9 @@ couchdb==1.2
cryptography==41.0.7
# via hattrick-fwf
flask==3.0.0
# via flask-babel
# via hattrick-fwf
flask-babel==4.0.0
# via hattrick-fwf
gunicorn==21.2.0
# via hattrick-fwf
Expand All @@ -41,8 +46,10 @@ importlib-metadata==7.0.1
# via pygal
itsdangerous==2.1.2
# via flask
jinja2==3.1.2
jinja2==3.1.4
# via flask
# via flask-babel
# via hattrick-fwf
lxml==4.9.3
# via hattrick-libs
markupsafe==2.1.3
Expand All @@ -58,6 +65,7 @@ pygal-maps-world==1.0.2
# via hattrick-fwf
pytz==2023.3.post1
# via apscheduler
# via flask-babel
rauth==0.7.3
# via hattrick-libs
requests==2.31.0
Expand Down

0 comments on commit 8d583e3

Please sign in to comment.