Enable translation support

This commit is contained in:
rubenwardy 2022-01-07 19:42:52 +00:00
parent eba1626f2e
commit 482c9e5905
4 changed files with 76 additions and 11 deletions

1
.gitignore vendored
View File

@ -11,6 +11,7 @@ app/public/thumbnails
celerybeat-schedule
/data
.idea
*.mo
# Created by https://www.gitignore.io/api/linux,macos,python,windows

View File

@ -16,7 +16,9 @@ COPY utils utils
COPY config.cfg config.cfg
COPY migrations migrations
COPY app app
COPY translations translations
RUN pybabel compile -d translations
RUN chown -R cdb:cdb /home/cdb
USER cdb

View File

@ -33,6 +33,12 @@ app.config["FLATPAGES_ROOT"] = "flatpages"
app.config["FLATPAGES_EXTENSION"] = ".md"
app.config["FLATPAGES_MARKDOWN_EXTENSIONS"] = MARKDOWN_EXTENSIONS
app.config["FLATPAGES_EXTENSION_CONFIG"] = MARKDOWN_EXTENSION_CONFIG
app.config["BABEL_TRANSLATION_DIRECTORIES"] = "../translations"
app.config["LANGUAGES"] = {
"en": "English",
"fr": "Français",
}
app.config.from_pyfile(os.environ["FLASK_CONFIG"])
r = redis.Redis.from_url(app.config["REDIS_URL"])
@ -57,6 +63,7 @@ login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "users.login"
from .sass import sass
sass(app)
@ -66,14 +73,9 @@ if not app.debug and app.config["MAIL_UTILS_ERROR_SEND_TO"]:
app.logger.addHandler(build_handler(app))
# @babel.localeselector
# def get_locale():
# return request.accept_languages.best_match(app.config["LANGUAGES"].keys())
from . import models, template_filters
@login_manager.user_loader
def load_user(user_id):
return models.User.query.filter_by(username=user_id).first()
@ -104,7 +106,8 @@ def check_for_ban():
current_user.rank = models.UserRank.MEMBER
models.db.session.commit()
from .utils import clearNotifications
from .utils import clearNotifications, is_safe_url
@app.before_request
def check_for_notifications():
@ -114,3 +117,31 @@ def check_for_notifications():
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html"), 404
@babel.localeselector
def get_locale():
locales = app.config["LANGUAGES"].keys()
locale = request.cookies.get("locale")
if locale in locales:
return locale
return request.accept_languages.best_match(locales)
@app.route("/set-locale/", methods=["POST"])
def set_locale():
locale = request.form.get("locale")
if locale not in app.config["LANGUAGES"].keys():
flash("Unknown locale {}".format(locale), "danger")
locale = None
next_url = request.form.get("r")
if next_url and is_safe_url(next_url):
resp = make_response(redirect(next_url))
else:
resp = make_response(redirect(url_for("homepage.home")))
resp.set_cookie("locale", locale)
return resp

View File

@ -109,12 +109,15 @@
<i class="fas fa-plus"></i>
</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle"
data-toggle="dropdown"
role="button"
aria-expanded="false">{{ current_user.display_name }}
<span class="caret"></span></a>
data-toggle="dropdown"
role="button"
aria-expanded="false">
{{ current_user.display_name }}
<span class="caret"></span>
</a>
<ul class="dropdown-menu dropdown-menu-right" role="menu">
<li class="nav-item">
@ -154,6 +157,34 @@
{% else %}
<li><a class="nav-link" href="{{ url_for('users.login') }}">{{ _("Sign in") }}</a></li>
{% endif %}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle"
data-toggle="dropdown"
role="button"
aria-expanded="false">
<i class="fas fa-language"></i>
</a>
<ul class="dropdown-menu dropdown-menu-right" role="menu">
<li class="nav-item">
<form method="POST" action="{{ url_for('set_locale') }}">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
<input type="hidden" name="locale" value="en" />
<input type="hidden" name="r" value="{{ url_set_query() }}" />
<input type="submit" class="btn btn-link nav-link" value="English (en)">
</form>
</li>
<li>
<form method="POST" action="{{ url_for('set_locale') }}">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
<input type="hidden" name="locale" value="fr" />
<input type="hidden" name="r" value="{{ url_set_query() }}" />
<input type="submit" class="btn btn-link nav-link" value="Français (fr)">
</form>
</li>
</ul>
</form>
</li>
</ul>
</div>
</div>