Skip to content

Commit

Permalink
Fix I18N
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Bompard <[email protected]>
  • Loading branch information
abompard committed Jun 19, 2024
1 parent 4cfe95f commit 133e207
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
2 changes: 1 addition & 1 deletion babel.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[python: **.py]
[jinja2: **/templates/**.html]
extensions=jinja2.ext.i18n,jinja2.ext.autoescape,jinja2.ext.with_
extensions=jinja2.ext.i18n
6 changes: 3 additions & 3 deletions tahrir/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from flask_wtf.csrf import CSRFProtect
from whitenoise import WhiteNoise

from tahrir import l10n
from tahrir.cache import cache
from tahrir.cli import tahrir_cli
from tahrir.database import db
from tahrir.l10n import babel, store_locale
from tahrir.utils import import_all
from tahrir.utils.avatar import as_avatar
from tahrir.utils.date_time import relative_time
Expand Down Expand Up @@ -66,8 +66,8 @@ def create_app(config=None):

# Extensions
oidc.init_app(app, prefix="/oidc")
babel.init_app(app)
app.before_request(store_locale)
l10n.babel.init_app(app, locale_selector=l10n.pick_locale)
app.before_request(l10n.store_locale)
app.jinja_env.add_extension("jinja2.ext.i18n")
csrf.init_app(app)

Expand Down
14 changes: 3 additions & 11 deletions tahrir/l10n.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import flask_babel
from flask import g, request
from flask_babel import Babel, get_locale


_LANGUAGES = []

babel = Babel()
babel = flask_babel.Babel()


def _get_accepted_languages():
Expand All @@ -15,18 +15,10 @@ def _get_accepted_languages():
return _LANGUAGES


@babel.localeselector
def pick_locale():
return request.accept_languages.best_match(_get_accepted_languages())


@babel.timezoneselector
def get_timezone():
user = getattr(g, "user", None)
if user is not None:
return user.timezone


def store_locale():
# Store the current locale in g for access in the templates.
g.locale = get_locale()
g.locale = flask_babel.get_locale()

0 comments on commit 133e207

Please sign in to comment.