lenticular_cloud2/lenticular_cloud/translations/__init__.py

72 lines
1.8 KiB
Python
Raw Normal View History

2020-05-09 18:00:07 +00:00
from flask import g, request, Flask
from flask_babel import Babel
2020-05-09 18:00:07 +00:00
from flask_login import current_user
from typing import Optional
2022-02-06 22:57:01 +00:00
from lenticular_cloud.model import db, User
2023-03-18 11:00:40 +00:00
from importlib.metadata import version
2020-05-09 18:00:07 +00:00
LANGUAGES = {
'en': 'English',
'de': 'Deutsch'
}
def get_locale() -> str:
# if a user is logged in, use the locale from the user settings
2023-09-30 10:58:24 +00:00
#user = current_user # type: Optional[User]
return 'de'
# prefer lang argument
2023-09-30 10:58:24 +00:00
# if 'lang' in request.args:
# lang = request.args['lang'] # type: str
# if lang in LANGUAGES:
# if not isinstance(user, User):
# return lang
# user.locale = lang
# db.session.commit()
2023-09-30 10:58:24 +00:00
# if isinstance(user, User):
# return user.locale
# # otherwise try to guess the language from the user accept
# # header the browser transmits. We support de/fr/en in this
# # example. The best match wins.
# return request.accept_languages.best_match(['de'])
def get_timezone() -> Optional[str]:
2020-05-09 18:00:07 +00:00
# user = getattr(g, 'user', None)
# if user is not None:
# return user.timezone
return None
2023-03-18 11:00:40 +00:00
flask_babel_version = version('flask_babel')
kwargs = {}
if flask_babel_version >= "3.0.0":
kwargs = {
'locale_selector': get_locale,
#'timezone_selector': get_timezone,
}
babel = Babel(**kwargs)
if flask_babel_version < "3.0.0":
@babel.localeselector
def _get_locale() -> str:
return get_locale()
@babel.timezoneselector
def _get_timezone() -> Optional[str]:
return get_timezone()
def init_babel(app: Flask) -> None:
babel.init_app(app)
2020-05-09 18:00:07 +00:00
@app.context_processor
def get_locale_jinja() -> dict:
def get_locale_() -> str:
2022-06-18 11:05:18 +00:00
return get_locale() # type: ignore
2020-05-09 18:00:07 +00:00
return dict(get_locale=get_locale_)
return None