#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------------
# This file is part of Mentat system (https://mentat.cesnet.cz/).
#
# Copyright (C) since 2011 CESNET, z.s.p.o (http://www.ces.net/)
# Use of this source is governed by the MIT license, see LICENSE file.
# -------------------------------------------------------------------------------
"""
This module contains useful internationalization utilities for *Hawat* application.
"""
__author__ = "Jan Mach <jan.mach@cesnet.cz>"
__credits__ = "Pavel Kácha <pavel.kacha@cesnet.cz>, Andrea Kropáčová <andrea.kropacova@cesnet.cz>"
import flask
import flask_babel
import flask_login
from babel import Locale
BABEL = flask_babel.Babel()
[docs]def get_locale(): # pylint: disable=locally-disabled,unused-variable
"""
Implementation of locale selector for :py:mod:`flask_babel`.
"""
# If a user is logged in, try to use the locale from the user settings.
if flask_login.current_user.is_authenticated:
if hasattr(flask_login.current_user, 'locale') and flask_login.current_user.locale:
flask.session['locale'] = flask_login.current_user.locale
# Store the best locale selection into the session.
if 'locale' not in flask.session or not flask.session['locale']:
if flask.current_app.config['BABEL_DETECT_LOCALE']:
flask.session['locale'] = flask.request.accept_languages.best_match(
flask.current_app.config['SUPPORTED_LOCALES'].keys()
)
else:
flask.session['locale'] = flask.current_app.config['BABEL_DEFAULT_LOCALE']
if 'locale' in flask.session and flask.session['locale']:
return flask.session['locale']
return flask.current_app.config['BABEL_DEFAULT_LOCALE']
[docs]def get_timezone(): # pylint: disable=locally-disabled,unused-variable
"""
Implementation of timezone selector for :py:mod:`flask_babel`.
"""
# If a user is logged in, try to use the timezone from the user settings.
if flask_login.current_user.is_authenticated:
if hasattr(flask_login.current_user, 'timezone') and flask_login.current_user.timezone:
flask.session['timezone'] = flask_login.current_user.timezone
# Store the default timezone selection into the session.
if 'timezone' not in flask.session or not flask.session['timezone']:
flask.session['timezone'] = flask.current_app.config['BABEL_DEFAULT_TIMEZONE']
if 'timezone' in flask.session and flask.session['timezone']:
return flask.session['timezone']
return flask.current_app.config['BABEL_DEFAULT_TIMEZONE']
[docs]def babel_translate_locale(locale_id, with_current=False):
"""
Translate given locale language. By default, return language in locale`s
language. Optionally return language in given locale`s language.
"""
locale_obj = Locale.parse(locale_id)
if not with_current:
return locale_obj.language_name
return locale_obj.get_language_name(flask_babel.get_locale())
[docs]def babel_language_in_locale(locale_id='en'):
"""
Translate given locale language. By default, return language in locale`s
language. Optionally return language in given locale`s language.
"""
locale_obj = Locale.parse(flask_babel.get_locale())
return locale_obj.get_language_name(locale_id)