1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-05 17:46:22 +00:00
calibre-web/cps/babel.py

41 lines
1.2 KiB
Python
Raw Normal View History

from babel import negotiate_locale
from flask_babel import Babel, Locale
from babel.core import UnknownLocaleError
2023-02-04 10:18:43 +00:00
from flask import request
from flask_login import current_user
from . import logger
log = logger.create()
babel = Babel()
def get_locale():
# if a user is logged in, use the locale from the user settings
2023-02-04 10:18:43 +00:00
if current_user is not None and hasattr(current_user, "locale"):
# if the account is the guest account bypass the config lang settings
if current_user.name != 'Guest':
return current_user.locale
preferred = list()
if request.accept_languages:
for x in request.accept_languages.values():
try:
preferred.append(str(Locale.parse(x.replace('-', '_'))))
except (UnknownLocaleError, ValueError) as e:
log.debug('Could not parse locale "%s": %s', x, e)
return negotiate_locale(preferred or ['en'], get_available_translations())
def get_user_locale_language(user_language):
return Locale.parse(user_language).get_language_name(get_locale())
2022-05-21 20:52:59 +00:00
def get_available_locale():
return [Locale('en')] + babel.list_translations()
2022-05-21 20:52:59 +00:00
def get_available_translations():
return set(str(item) for item in get_available_locale())