Repository: pwr-Solaar/Solaar

Issue:
locale.Error: unsupported locale setting exception when glibc locale files are not present
**Information**
- Solaar version: 1.0.3
- Distribution: Fedora
- Kernel version (ex. `uname -srmo`): `Linux 5.7.11-200.fc32.x86_64 x86_64 GNU/Linux`
- Output of `solaar show`: N/A

**Describe the bug**
Any solaar invocation is failing with a traceback when locale.setlocale() call fails, e.g. due to missing glibc locale files for the currently set locale.

**To Reproduce**
Steps to reproduce the behavior:
```
$ sudo dnf remove glibc-langpack-de
$ export LC_ALL=de_CH.UTF-8
$ export LANG=de_CH.UTF-8
$ solaar --help
Traceback (most recent call last):
  File "/usr/bin/solaar", line 59, in <module>
    import solaar.gtk
  File "/usr/lib/python3.8/site-packages/solaar/gtk.py", line 29, in <module>
    import solaar.i18n as _i18n
  File "/usr/lib/python3.8/site-packages/solaar/i18n.py", line 50, in <module>
    locale.setlocale(locale.LC_ALL, '')
  File "/usr/lib64/python3.8/locale.py", line 608, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting
$ 
```

**Additional context**
Looks like #190 is still unfixed. Downstream bug report: https://bugzilla.redhat.com/show_bug.cgi?id=1811313 .


Discussion:
The bugzilla bug report is restricted so I cannot see it.
Ah, sorry. It is indeed. I won't make it public without reporter's consent as it may contain sensitive data. It doesn't contain any useful information beyond what I posted here, though. This is 100% reproducible, just set locale to something not supported by current glibc install.
It seems to me that this is not a Solaar problem.   Googling the error results in several threads stating that the correct remedy is to set up your locale settings correctly.
One such thread is https://stackoverflow.com/questions/14547631/python-locale-error-unsupported-locale-setting/14548156

@FFY00 Any comments?


In principle, you are correct. However, this issue keeps cropping up in various places. In case of GUI, the user won't even see the error. If the user is clueless, they will be at a loss why solaar didn't launch. Maybe it should at least try to catch the exception and display a pop-up telling the user to install missing locale definition?
I think Solaar should default to `C` upon `locale.Error`.
What happens in the GUI in this situation?  If something decent happens, then I don't think that anything is wrong in the Solaar GUI.
@FFY00  How does one fall back to the C locale?
I don't have that langpack installed but when I set LC_ALL and LANG as suggested I do not get an error with `solaar show` `solaar -dd` or `solaar --help`.  It looks as if there is something else going wrong in the user's system.
@pfps If you have `glibc-all-langpacks` installed then you do have it. Uninstall both.
Aaah.  That produces the error.

It looks as if ignoring exceptions from the setlocale statement ends up with the correct behaviour.  I'll put in a PR for that, but it will need to be tested.

Resolving patch:
diff --git a/lib/solaar/i18n.py b/lib/solaar/i18n.py
--- a/lib/solaar/i18n.py
+++ b/lib/solaar/i18n.py
@@ -47,7 +47,11 @@ def _find_locale_path(lc_domain):
     # del _path
 
 
-locale.setlocale(locale.LC_ALL, '')
+try:
+    locale.setlocale(locale.LC_ALL, '')
+except Exception:
+    pass
+
 language, encoding = locale.getlocale()
 del locale
 


Test patch: