'Python 3.7.9: wx.Locale & GetLocale() gives ValueError: unknown locale: en-GB / fr-CH

When using datetime.strptime() within a wx.App() I am receiving a value error: Unknown locale: en-GB or fr-CH

I am using Python 3.7.9 & wx 4.1.1 on windows 10. Using wx.Locale() seems to cause this error. To investigate I created a small debug program below. The console output is shown as comments.

You can see that creating the wx.App() changes the locale to fr_CH (note the underscore). My windows language/region settings is English & my keyboard is fr-CH.

Then after setting the wx.Locale() the locale.GetLocale() crashes because fr-CH is an unknown locale (note the dash).

import sys
import wx
import locale
print (sys.version)         # 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)]
print(wx.version())         # 4.1.1 msw (phoenix) wxWidgets 3.1.5

print(locale.getlocale())   # (None, None)

app = wx.App()

print(locale.getlocale())   # ('fr_CH', 'ISO8859-1')

localeapp = wx.Locale()
localeapp.Init(wx.LANGUAGE_DEFAULT)

print(locale.getlocale())   # Getlocale _parse_localename raise Value Error:
                            # ValueError: Unkown locale: fr-CH

In Python 2.7.15 & wx 3.0.2.0 the output is different:

(None, None)
(None, None)
('French_Switzerland', '1252')
  1. why does the wx.App() use the keyboard language instead of the actual "regional & language settings"

  2. Why does wx.App() change the locale (None would seem to take the system language as in Python27)

  3. Is there a way to make fr_CH & fr-CH match together?

If I set the language to wx.LANGUAGE_ENGLISH I have the same problem with en_GB & en-GB

I found a bug report for Python 3.8 for this exact same issue (and a commit to wxPython to "workaround" the problem but I am using 3.7.9. https://github.com/python/cpython/issues/87281

Note, I had to add self.locale = wx.Locale() & self.locale.Init(wx.LANGUAGE_DEFAULT) to my applications because of a similar issue with wx4.1.1 & bitmaps which require locale().

See this thread where I found this solution: wxPython "Things are going to break" error

Thanks in advance for any help,

Gabbo-CH



Solution 1:[1]

I found a workaround to this problem that was ok for me.

class Application(wx.Frame):
    def __init__(self, parent, title):
        import locale
        locale.setlocale(locale.LC_ALL, "C")
        self.locale = wx.Locale()

Removing the self.locale.Init(wx.LANGUAGE_ENGLISH) is fine for me as I dont really want to change the system language & setlocale(locale.LC_ALL, "C") seems to prevent problems for systems not set to US-english.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GabboCH