Skip to content

Commit

Permalink
i18n: add DEFAULT_UI_LANGUAGE server setting
Browse files Browse the repository at this point in the history
The DEFAULT_UI_LANGUAGE server setting allows to override the "Use browser's default" when detecting the locale to use.
This allows to e.g., force the default language of the users server-wide, which can be useful in specific use cases (e.g., exams).
  • Loading branch information
dezhidki committed Feb 19, 2025
1 parent afef864 commit 9dd0e14
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 6 additions & 0 deletions timApp/defaultconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@
SESSION_COOKIE_SAMESITE = "Lax" if HAS_HTTPS else None
SESSION_COOKIE_SECURE = HAS_HTTPS # Require HTTPS or localhost for session cookies

DEFAULT_UI_LANGUAGE = None
"""
The default UI language to use when user's language is not explicitly set.
If not set, determines the language based on the user's browser settings.
"""

BOOKMARKS_ENABLED = True

# If False, only admins can create folders and documents.
Expand Down
7 changes: 6 additions & 1 deletion timApp/util/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ def get_locale(force_refresh: bool = False) -> str:
KNOWN_LANGUAGES, default="en-US"
) # type: ignore
lng = request.cookies.get("lang")
default_language: str | None = current_app.config["DEFAULT_UI_LANGUAGE"]
if default_language not in KNOWN_LANGUAGES:
default_language = None
if not lng or force_refresh:
if not logged_in():
return header_lang
return default_language or header_lang
u = get_current_user_object()
lng = u.get_prefs().language
if lng in KNOWN_LANGUAGES:
header_lang = lng
elif default_language is not None:
header_lang = default_language
override = get_document_lang_override()
if override and override in KNOWN_LANGUAGES:
header_lang = override
Expand Down

0 comments on commit 9dd0e14

Please sign in to comment.