Added system language detection

This commit is contained in:
Ignacio Serantes
2026-03-26 20:09:46 +01:00
parent a7ce2ceb75
commit 096cee6ca3
5 changed files with 50 additions and 32 deletions

View File

@@ -29,7 +29,7 @@ if FORCE_X11:
# --- CONFIGURATION ---
PROG_NAME = "Bagheera Image Viewer"
PROG_ID = "bagheeraview"
PROG_VERSION = "0.9.14"
PROG_VERSION = "0.9.15-dev"
PROG_AUTHOR = "Ignacio Serantes"
# --- CACHE SETTINGS ---
@@ -341,23 +341,16 @@ DEFAULT_VIEWER_SHORTCUTS = {
# Supported languages
SUPPORTED_LANGUAGES = {
"system": "System",
"en": "English",
"es": "Español",
"gl": "Galego"
}
# Default language
DEFAULT_LANGUAGE = "en"
# Determine current language:
# 1. Environment variable (for debugging/override)
# 2. Saved configuration
# 3. Default
CURRENT_LANGUAGE = os.getenv("BAGHEERA_LANG") or \
APP_CONFIG.get("language", DEFAULT_LANGUAGE)
# Ensure the loaded language is supported, otherwise fallback to default
if CURRENT_LANGUAGE not in SUPPORTED_LANGUAGES:
CURRENT_LANGUAGE = DEFAULT_LANGUAGE
# Default language for configuration
DEFAULT_LANGUAGE = "system"
# Fallback language for translations
FALLBACK_LANGUAGE = "en"
_UI_TEXTS = {
"en": {
@@ -1770,6 +1763,26 @@ _UI_TEXTS = {
}
# Determine which language to use for UI strings
def _get_current_language():
lang = os.getenv("BAGHEERA_LANG") or APP_CONFIG.get("language", DEFAULT_LANGUAGE)
if lang == "system":
sys_lang = os.getenv("LANG")
if sys_lang:
# LANG is usually something like 'en_US.UTF-8'
lang = sys_lang[0:2].lower()
else:
lang = FALLBACK_LANGUAGE
# If the resolved language is not supported by our translation dictionaries,
# fallback to English.
return lang if lang in _UI_TEXTS else FALLBACK_LANGUAGE
CURRENT_LANGUAGE = _get_current_language()
class _UITextsProxy:
"""
A proxy class to access UI strings from the _UI_TEXTS dictionary.
@@ -1781,12 +1794,12 @@ class _UITextsProxy:
"""
def __getattr__(self, name):
# Get the dictionary for the current language, or fallback to the default.
lang_texts = _UI_TEXTS.get(CURRENT_LANGUAGE, _UI_TEXTS[DEFAULT_LANGUAGE])
lang_texts = _UI_TEXTS.get(CURRENT_LANGUAGE, _UI_TEXTS[FALLBACK_LANGUAGE])
# Get the specific string. If not found in the current language,
# try the default language.
text = lang_texts.get(name)
if text is None:
default_texts = _UI_TEXTS[DEFAULT_LANGUAGE]
default_texts = _UI_TEXTS[FALLBACK_LANGUAGE]
# Return a placeholder if not found anywhere
text = default_texts.get(name, f"_{name}_")
return text