From 8d19d8a22f865324a79495b0d745bd390abf4ee8 Mon Sep 17 00:00:00 2001 From: Matt Lewis Date: Sun, 21 Jul 2024 11:23:22 +0200 Subject: [PATCH] GUI startup/locale. Changed so that if there's an error when setting locale, it shows an error notification on the console and tries to continue. This is to address part of issue #3 on GitHub (comment 17 June 2024). --- docs/config-examples/defaults.ini | 6 +++--- pygenda/pygenda_gui.py | 11 ++++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/docs/config-examples/defaults.ini b/docs/config-examples/defaults.ini index c767c38..1ea19d8 100644 --- a/docs/config-examples/defaults.ini +++ b/docs/config-examples/defaults.ini @@ -57,9 +57,9 @@ # language = language string (e.g. en_GB, en_US, fr) # UI language. Note: the selected locale must be installed on the -# operating system, otherwise it will give an error and fail to -# start. (To enable a locale on Debian, edit the /etc/locale.gen -# file and then run (sudo) locale-gen.) +# operating system, otherwise it will show an error in the console +# and fall back to the default. (To enable a locale on Debian, edit +# the /etc/locale.gen file and then run (sudo) locale-gen.) # Default: Use operating system locale # date_sep = string diff --git a/pygenda/pygenda_gui.py b/pygenda/pygenda_gui.py index ea3cabd..f801006 100644 --- a/pygenda/pygenda_gui.py +++ b/pygenda/pygenda_gui.py @@ -29,6 +29,7 @@ from importlib import import_module from os import path as ospath from sys import stderr +from pathlib import Path import signal import ctypes from typing import Optional, Tuple, List, Union, Any, Type @@ -427,7 +428,15 @@ def _init_locale(cls) -> None: if isinstance(lang,str) and '.' not in lang: # Need to include encoding lang = (lang,'UTF-8') - locale.setlocale(locale.LC_ALL, lang) + try: + locale.setlocale(locale.LC_ALL, lang) + except locale.Error: + lang_dname = lang if isinstance(lang,str) else '.'.join(lang) + print("Error loading locale {:s} (not installed on OS?)".format(lang_dname), file=stderr) + if Path('/etc/debian_version').exists(): + # Add extra info for users on Debian-like systems + print(" On Debian try: uncomment corresponding line in /etc/locale.gen & run locale-gen", file=stderr) + print(" Trying to continue with default locale...", file=stderr) locale.bindtextdomain('pygenda', cls._LOCALE_DIR) locale.textdomain('pygenda')