externalize localized application messages

pull/19/head
Yax 6 months ago
parent 4d52229e4d
commit 69f1c35ef5

@ -0,0 +1,17 @@
import configparser
import os
class Messages:
def __init__(self):
self.property_dict = {}
def load_messages(self, lang):
config = configparser.ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), 'messages_' + lang + '.properties'))
for key, value in config.items('messages'):
self.property_dict[key] = value
def get(self, key):
return self.property_dict.get(key)

@ -0,0 +1,6 @@
[messages]
login.failure.username=Username or password incorrect
logout.flash=You have been logged out.
admin.comment.notfound=Comment not found.
admin.comment.approved=Comment published.
admin.comment.deleted=Comment deleted.

@ -0,0 +1,6 @@
[messages]
login.failure.username=Identifiant ou mot de passe incorrect
logout.flash=Vous avez été déconnecté.
admin.comment.notfound=Commentaire introuvable
admin.comment.approved=Commentaire publié
admin.comment.deleted=Commentaire supprimé

@ -37,10 +37,7 @@ def login():
if is_login_ok(username, password):
session["user"] = username
return redirect("/web/admin")
if app.config["CONFIG"].get(ConfigParameter.LANG) == "fr":
flash("Identifiant ou mot de passe incorrect")
else:
flash("Username or password incorrect")
flash(app.config["MESSAGES"].get("login.failure.username"))
return redirect("/web/login")
# GET
return render_template(
@ -51,10 +48,7 @@ def login():
@app.route("/web/logout", methods=["GET"])
def logout():
session.pop("user")
if app.config["CONFIG"].get(ConfigParameter.LANG) == "fr":
flash("Vous avez été déconnecté.")
else:
flash("You have been logged out.")
flash(app.config["MESSAGES"].get("logout.flash"))
return redirect("/web/admin")
@ -78,21 +72,12 @@ def admin_homepage():
def admin_action():
comment = dao.find_comment_by_id(request.form.get("comment"))
if comment is None:
if app.config["CONFIG"].get(ConfigParameter.LANG) == "fr":
flash("Commentaire introuvable")
else:
flash("Comment not found.")
flash(app.config["MESSAGES"].get("admin.comment.notfound"))
elif request.form.get("action") == "APPROVE":
dao.publish_comment(comment)
app.config["RSS"].generate()
if app.config["CONFIG"].get(ConfigParameter.LANG) == "fr":
flash("Commentaire publié")
else:
flash("Comment published.")
flash(app.config["MESSAGES"].get("admin.comment.approved"))
else:
dao.delete_comment(comment)
if app.config["CONFIG"].get(ConfigParameter.LANG) == "fr":
flash("Commentaire supprimé")
else:
flash("Comment deleted.")
flash(app.config["MESSAGES"].get("admin.comment.deleted"))
return redirect("/web/admin")

@ -7,6 +7,7 @@ import os
import sys
from stacosys.db import database
from stacosys.i18n.messages import Messages
from stacosys.interface import api, app, form
from stacosys.interface.web import admin
from stacosys.service.configuration import Config, ConfigParameter
@ -64,6 +65,12 @@ def configure_rss(config):
return rss
def configure_localization(config):
messages = Messages()
messages.load_messages(config.get(ConfigParameter.LANG))
return messages
def main(config_pathname):
logger = configure_logging()
config = load_and_validate_config(config_pathname, logger)
@ -72,11 +79,13 @@ def main(config_pathname):
logger.info("Start Stacosys application")
rss = configure_rss(config)
mailer = configure_and_validate_mailer(config, logger)
messages = configure_localization(config)
logger.info("start interfaces %s %s %s", api, form, admin)
app.config["CONFIG"] = config
app.config["MAILER"] = mailer
app.config["RSS"] = rss
app.config["MESSAGES"] = messages
app.run(
host=config.get(ConfigParameter.HTTP_HOST),
port=config.get_int(ConfigParameter.HTTP_PORT),

Loading…
Cancel
Save