Saved user language was not being picked up in case of non-server mode.

Because - the get_locale(...) function was being executed before any
request, and user login after first request. Hence - the values for
the labels in the Preferences are not being translated properly.

Fetch the raw value of user_language from configuration in case of
runtime/non-server mode to fix the issue.
This commit is contained in:
Alexander Lakhin 2017-10-30 18:20:25 +05:30 committed by Ashesh Vashi
parent bc0320d21e
commit 8860bc3c3e
2 changed files with 47 additions and 8 deletions

View File

@ -17,7 +17,7 @@ from importlib import import_module
from flask import Flask, abort, request, current_app, session, url_for
from flask_babel import Babel, gettext
from flask_login import user_logged_in, user_logged_out
from flask_security import Security, SQLAlchemyUserDatastore
from flask_security import Security, SQLAlchemyUserDatastore, current_user
from flask_mail import Mail
from flask_security.utils import login_user
from werkzeug.datastructures import ImmutableDict
@ -242,13 +242,17 @@ def create_app(app_name=None):
language = 'en'
if config.SERVER_MODE is False:
# Get the user language preference from the miscellaneous module
misc_preference = Preferences.module('miscellaneous', False)
if misc_preference:
user_languages = misc_preference.preference(
'user_language'
if current_user.is_authenticated:
user_id = current_user.id
else:
user = user_datastore.get_user(config.DESKTOP_USER)
if user is not None:
user_id = user.id
user_language = Preferences.raw_value(
'miscellaneous', 'user_language', None, user_id
)
if user_languages:
language = user_languages.get() or language
if user_language is not None:
language = user_language
else:
# If language is available in get request then return the same
# otherwise check the session or cookie

View File

@ -473,6 +473,41 @@ class Preferences(object):
options, help_str, category_label
)
@staticmethod
def raw_value(_module, _preference, _category=None, _user_id=None):
# Find the entry for this module in the configuration database.
module = ModulePrefTable.query.filter_by(name=_module).first()
if module is None:
return None
if _category is None:
_category = _module
if _user_id is None:
_user_id = getattr(current_user, 'id', None)
if _user_id is None:
return None
cat = PrefCategoryTbl.query.filter_by(mid=module.id).filter_by(name=_category).first()
if cat is None:
return None
pref = PrefTable.query.filter_by(name=_preference).filter_by(cid=cat.id).first()
if pref is None:
return None
user_pref = UserPrefTable.query.filter_by(
pid=pref.id
).filter_by(uid=_user_id).first()
if user_pref is not None:
return user_pref.value
return None
@classmethod
def module(cls, name, create=True):
"""