Unsupported locale: Fall back to English (#2891)

* Unsupported locale: Fall back to English

For any locale that is not supported, automatically fall back to no
translation, so it is English.

Fixes #2889

* Update lib/solaar/i18n.py

---------

Co-authored-by: Peter F. Patel-Schneider <pfpschneider@gmail.com>
This commit is contained in:
MattHag 2025-09-08 15:44:45 +02:00 committed by GitHub
parent bebadc219c
commit 2a363a6388
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 4 deletions

View File

@ -16,6 +16,7 @@
import gettext
import locale
import logging
import os
import sys
@ -25,6 +26,8 @@ from solaar import NAME
_LOCALE_DOMAIN = NAME.lower()
logger = logging.getLogger(__name__)
def _find_locale_path(locale_domain: str) -> str:
prefix_share = os.path.normpath(os.path.join(os.path.realpath(sys.path[0]), ".."))
@ -37,18 +40,23 @@ def _find_locale_path(locale_domain: str) -> str:
raise FileNotFoundError(f"Could not find locale path for {locale_domain}")
def set_locale_to_system_default():
def set_locale_to_system_default() -> None:
"""Sets locale for translations to the system default.
If locale is unsupported, fallback to standard English without
translation 'C'.
Set LC_ALL environment variable to enforce a locale setting e.g.
'de_DE.UTF-8'. Run Solaar with your desired localization, for German
use:
'LC_ALL=de_DE.UTF-8 solaar'
"""
try:
locale.setlocale(locale.LC_ALL, "")
except Exception:
pass
locale.setlocale(locale.LC_ALL, "") # system default
except locale.Error:
logger.error("User locale not supported by system, using no translation.")
locale.setlocale(locale.LC_ALL, "C") # untranslated (English)
return
try:
path = _find_locale_path(_LOCALE_DOMAIN)