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:
parent
bebadc219c
commit
2a363a6388
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
import gettext
|
import gettext
|
||||||
import locale
|
import locale
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
@ -25,6 +26,8 @@ from solaar import NAME
|
||||||
|
|
||||||
_LOCALE_DOMAIN = NAME.lower()
|
_LOCALE_DOMAIN = NAME.lower()
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def _find_locale_path(locale_domain: str) -> str:
|
def _find_locale_path(locale_domain: str) -> str:
|
||||||
prefix_share = os.path.normpath(os.path.join(os.path.realpath(sys.path[0]), ".."))
|
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}")
|
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.
|
"""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.
|
Set LC_ALL environment variable to enforce a locale setting e.g.
|
||||||
'de_DE.UTF-8'. Run Solaar with your desired localization, for German
|
'de_DE.UTF-8'. Run Solaar with your desired localization, for German
|
||||||
use:
|
use:
|
||||||
'LC_ALL=de_DE.UTF-8 solaar'
|
'LC_ALL=de_DE.UTF-8 solaar'
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
locale.setlocale(locale.LC_ALL, "")
|
locale.setlocale(locale.LC_ALL, "") # system default
|
||||||
except Exception:
|
except locale.Error:
|
||||||
pass
|
logger.error("User locale not supported by system, using no translation.")
|
||||||
|
locale.setlocale(locale.LC_ALL, "C") # untranslated (English)
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
path = _find_locale_path(_LOCALE_DOMAIN)
|
path = _find_locale_path(_LOCALE_DOMAIN)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue