diff options
author | Jannis M. Hoffmann <jannis@fehcom.de> | 2023-12-07 22:10:53 +0100 |
---|---|---|
committer | Jannis M. Hoffmann <jannis@fehcom.de> | 2023-12-07 22:10:53 +0100 |
commit | 1c13b581254b8e46cef420811b6ae14a06180ace (patch) | |
tree | 62f5bb3d0bd9202752aec72454957f3ff7c4c812 | |
parent | 57d9c77df9ff834b6c6e9ef6ead863bdb9317a95 (diff) |
use BABEL default language setting
only accept valid locales
-rw-r--r-- | src/jwebmail/__init__.py | 25 | ||||
-rw-r--r-- | src/jwebmail/translations/en/LC_MESSAGES/messages.po | 177 | ||||
-rw-r--r-- | src/jwebmail/webmail.py | 12 |
3 files changed, 203 insertions, 11 deletions
diff --git a/src/jwebmail/__init__.py b/src/jwebmail/__init__.py index ba0b6af..f3ba58e 100644 --- a/src/jwebmail/__init__.py +++ b/src/jwebmail/__init__.py @@ -3,7 +3,8 @@ import pwd import sys from os import environ -from flask import Flask, g +from babel import parse_locale +from flask import Flask, abort, g from flask_babel import Babel, get_locale from flask_login import LoginManager, login_required from jinja2 import ChainableUndefined @@ -27,9 +28,11 @@ from .webmail import ( if sys.version_info >= (3, 11): from tomllib import load as toml_load + toml_read_file = dict(load=toml_load, text=False) else: from toml import load as toml_load + toml_read_file = dict(load=toml_load, text=True) __version__ = "2.0.0.dev2" @@ -50,8 +53,6 @@ def validate_config(app): conf["JWEBMAIL"]["READ_MAILS"].setdefault("AUTHENTICATOR", "qmail-authuser") ) - conf["JWEBMAIL"].setdefault("DEFAULT_LANGUAGE", "de") - def create_app(): app = Flask(__name__) @@ -67,9 +68,7 @@ def create_app(): validate_config(app) - DEFAULT_LANGUAGE = app.config["JWEBMAIL"]["DEFAULT_LANGUAGE"] - - Babel(app, locale_selector=lambda: g.get("lang_code", DEFAULT_LANGUAGE)) + Babel(app, locale_selector=lambda: g.get("lang_code")) app.cli.add_command(compile_css_command) @@ -94,13 +93,19 @@ def create_app(): if "lang_code" in values: return if app.url_map.is_endpoint_expecting(endpoint, "lang_code"): - values["lang_code"] = g.get("lang_code", DEFAULT_LANGUAGE) + values["lang_code"] = g.get("lang_code", app.config["BABEL_DEFAULT_LOCALE"]) @app.url_value_preprocessor def pull_lang_code(endpoint, values): - g.lang_code = ( - values.pop("lang_code", DEFAULT_LANGUAGE) if values else DEFAULT_LANGUAGE - ) + if values and values.get("lang_code"): + try: + pl = parse_locale(values["lang_code"]) + if pl[0] in ["en", "de"]: + g.lang_code = values.pop("lang_code") + else: + abort(404) + except ValueError: + abort(404) return app diff --git a/src/jwebmail/translations/en/LC_MESSAGES/messages.po b/src/jwebmail/translations/en/LC_MESSAGES/messages.po new file mode 100644 index 0000000..5b1e43a --- /dev/null +++ b/src/jwebmail/translations/en/LC_MESSAGES/messages.po @@ -0,0 +1,177 @@ +# English translations for PROJECT. +# Copyright (C) 2023 ORGANIZATION +# This file is distributed under the same license as the PROJECT project. +# FIRST AUTHOR <EMAIL@ADDRESS>, 2023. +# +msgid "" +msgstr "" +"Project-Id-Version: PROJECT VERSION\n" +"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" +"POT-Creation-Date: 2023-12-06 23:44+0100\n" +"PO-Revision-Date: 2023-12-07 19:04+0100\n" +"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" +"Language: en\n" +"Language-Team: en <LL@li.org>\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.13.1\n" + +#: src/jwebmail/render_mail.py:67 +msgid "Attachment {filename} of type {filetype}" +msgstr "" + +#: src/jwebmail/render_mail.py:117 +msgid "From" +msgstr "" + +#: src/jwebmail/render_mail.py:118 +msgid "To" +msgstr "" + +#: src/jwebmail/render_mail.py:119 +msgid "CC" +msgstr "" + +#: src/jwebmail/render_mail.py:120 +msgid "BCC" +msgstr "" + +#: src/jwebmail/render_mail.py:147 +msgid "Unsupported MIME type of {mime_type}." +msgstr "" + +#: src/jwebmail/webmail.py:25 +msgid "Username" +msgstr "" + +#: src/jwebmail/webmail.py:27 +msgid "Password" +msgstr "" + +#: src/jwebmail/webmail.py:62 +msgid "login failed!" +msgstr "" + +#: src/jwebmail/webmail.py:109 +msgid "displaying <b>{start} - {end}</b> of <b>{total}</b> {record_name}" +msgstr "" + +#: src/jwebmail/webmail.py:162 +msgid "succ_move" +msgstr "" + +#: src/jwebmail/webmail.py:209 +msgid "error_send" +msgstr "" + +#: src/jwebmail/webmail.py:211 +msgid "succ_send" +msgstr "" + +#: src/jwebmail/templates/_bot_nav.html:11 +msgid "Move to" +msgstr "" + +#: src/jwebmail/templates/_bot_nav.html:19 +#: src/jwebmail/templates/_folders.html:21 +msgid "Home" +msgstr "" + +#: src/jwebmail/templates/_bot_nav.html:26 +msgid "Move" +msgstr "" + +#: src/jwebmail/templates/_bot_nav.html:38 +msgid "Remove" +msgstr "" + +#: src/jwebmail/templates/_bot_nav.html:44 +msgid "check all" +msgstr "" + +#: src/jwebmail/templates/_folders.html:36 +#, python-format +msgid "%(total_new_mails)s new" +msgstr "" + +#: src/jwebmail/templates/_folders.html:41 +msgid "mailbox size: " +msgstr "" + +#: src/jwebmail/templates/_top_nav.html:4 +msgid "Logout" +msgstr "" + +#: src/jwebmail/templates/_top_nav.html:5 +msgid "Write" +msgstr "" + +#: src/jwebmail/templates/_top_nav.html:11 +msgid "Search" +msgstr "" + +#: src/jwebmail/templates/_top_nav.html:26 +msgid "Sort" +msgstr "" + +#: src/jwebmail/templates/_top_nav.html:32 +#: src/jwebmail/templates/_top_nav.html:33 +msgid "Date" +msgstr "" + +#: src/jwebmail/templates/_top_nav.html:32 +#: src/jwebmail/templates/_top_nav.html:34 +#: src/jwebmail/templates/_top_nav.html:35 +msgid "Descending" +msgstr "" + +#: src/jwebmail/templates/_top_nav.html:33 +#: src/jwebmail/templates/_top_nav.html:36 +msgid "Ascending" +msgstr "" + +#: src/jwebmail/templates/_top_nav.html:34 +msgid "Size" +msgstr "" + +#: src/jwebmail/templates/_top_nav.html:35 +#: src/jwebmail/templates/_top_nav.html:36 +msgid "Sender" +msgstr "" + +#: src/jwebmail/templates/about.html:61 src/jwebmail/templates/login.html:8 +#: src/jwebmail/templates/login.html:64 +msgid "Login" +msgstr "" + +#: src/jwebmail/templates/displayheaders.html:25 +msgid "This folder is empty!" +msgstr "" + +#: src/jwebmail/templates/mainlayout.html:22 +msgid "About" +msgstr "" + +#: src/jwebmail/templates/mainlayout.html:25 +msgid "Version" +msgstr "" + +#: src/jwebmail/templates/not_found.html:19 +msgid "start page" +msgstr "" + +#: src/jwebmail/templates/readmail.html:12 +#: src/jwebmail/templates/writemail.html:78 +msgid "back" +msgstr "" + +#: src/jwebmail/templates/writemail.html:62 +msgid "attach file" +msgstr "" + +#: src/jwebmail/templates/writemail.html:71 +msgid "Send" +msgstr "" + diff --git a/src/jwebmail/webmail.py b/src/jwebmail/webmail.py index ed4bd1d..26dfe20 100644 --- a/src/jwebmail/webmail.py +++ b/src/jwebmail/webmail.py @@ -1,6 +1,15 @@ from urllib.parse import urlparse -from flask import abort, current_app, flash, redirect, render_template, request, url_for +from flask import ( + abort, + current_app, + flash, + g, + redirect, + render_template, + request, + url_for, +) from flask_babel import gettext, lazy_gettext from flask_login import current_user, login_user, logout_user from flask_paginate import Pagination, get_page_parameter, get_per_page_parameter @@ -57,6 +66,7 @@ def login(): if urlparse(nxt).netloc: abort(401) + g.setdefault("lang_code", current_app.config["BABEL_DEFAULT_LOCALE"]) return redirect(nxt or url_for("displayheaders"), 303) else: warn = gettext("login failed!") |