Refactored web.py to shrink size of file

This commit is contained in:
Ozzie Isaacs
2022-04-26 11:49:06 +02:00
parent 47414ada69
commit e7464f2694
27 changed files with 639 additions and 581 deletions
+30
View File
@@ -0,0 +1,30 @@
from babel import Locale as LC
from babel import negotiate_locale
from flask_babel import Babel
from babel.core import UnknownLocaleError
from flask import request, g
from . import logger
log = logger.create()
babel = Babel()
BABEL_TRANSLATIONS = set()
@babel.localeselector
def get_locale():
# if a user is logged in, use the locale from the user settings
user = getattr(g, 'user', None)
if user is not None and hasattr(user, "locale"):
if user.name != 'Guest': # if the account is the guest account bypass the config lang settings
return user.locale
preferred = list()
if request.accept_languages:
for x in request.accept_languages.values():
try:
preferred.append(str(LC.parse(x.replace('-', '_'))))
except (UnknownLocaleError, ValueError) as e:
log.debug('Could not parse locale "%s": %s', x, e)
return negotiate_locale(preferred or ['en'], BABEL_TRANSLATIONS)