Skip to content

Commit

Permalink
Merge pull request #1702 from wxWidgets/locale-stuff
Browse files Browse the repository at this point in the history
New InitLocale implementation
  • Loading branch information
RobinD42 authored Jun 29, 2020
2 parents b452a35 + 41220cc commit 7ac363c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
51 changes: 47 additions & 4 deletions docs/MigrationGuide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ longer there. They will have to be used by importing a wx submodule. Most of
them will be in the ``wx.adv`` module. One nice advantage of doing this is that
if your application is not using any of these lesser used classes then you
will not have to bundle the new modules (nor the associated wx DLLs) with
your application when you use py2exe or other executable builder.
your application when you use py2exe or other executable builder.


wx.ListCtrl
Expand Down Expand Up @@ -578,7 +578,7 @@ original, called PyPubSub. It's all the same code, but with just a different
access path. However, now that Python 2.7 support in PyPubSub is no longer being
maintained in the latest versions, it is now time for wxPython to disconnect
itself in order to not have to remain on the older version. This means that
``wx.lib.pubsub`` is now deprecated.
``wx.lib.pubsub`` is now deprecated.

Switching to the official PyPubSub is simple however, just install the package::

Expand All @@ -596,7 +596,7 @@ wx.html.HtmlWindow.OnOpeningURL
In wxPython Classic the return value of ``wx.html.HtmlWindow.OnOpeningURL`` and
``wx.html.HtmlWindowInterface.OnHTMLOpeningURL`` could be either a value from the
``wx.html.HtmlOpeningStatus`` enumeration, or a string containing the URL to
redirect to.
redirect to.

In Phoenix this has been changed to a simpler wrapper implementation which
requires that both an enum value and a string be returned as a tuple. For
Expand Down Expand Up @@ -641,7 +641,7 @@ wx.WS_EX_VALIDATE_RECURSIVELY is obsolete
The wx.WS_EX_VALIDATE_RECURSIVELY extended style flag is obsolete, as it is
now the default (and only) behavior. The style flag has been added back into
wxPython for compatibility, but with a zero value. You can just stop using it
in your code with no change in behavior.
in your code with no change in behavior.


Parameter name changes in radial gradient methods
Expand All @@ -668,6 +668,49 @@ And they now look like this::
const wxGraphicsMatrix& matrix = wxNullGraphicsMatrix);



Possible Locale Mismatch on Windows
-----------------------------------

On the Windows platform, prior to Python 3.8, it appears that Python did not do
any initialization of the process locale settings, at least for the "en_US"
based locales. For example, in Python 3.7::

>>> import locale
>>> locale.getdefaultlocale()
('en_US', 'cp1252')
>>> locale.getlocale()
(None, None)

And in Python 3.8::

>>> import locale
>>> locale.getdefaultlocale()
('en_US', 'cp1252')
>>> locale.getlocale()
('English_United States', '1252')

Now, when you add in the wxWidgets class wxLocale, then it can get even more
confusing on Windows. It seems that this boils down to wxWidgets setting the
locale using a Windows-specific name like "en-US" (with a hyphen instead of an
underscore). Since Python's locale module does not recognize this as a
legitimate locale alias, then calling `locale.getlocale()` after a `wx.Locale`
has been created will result in a `ValueError` exception.

So wxPython has added code in the `wx.App` class to try and set up the locale so
both Python and wxWidgets are set to equivalent settings. This is still somewhat
experimental however, and the implementation in wxPython 4.1.0 is still
problematic in some cases. If you have problems with it then you can disable or
change this code by overriding the `wx.App.InitLocale` method in a derived
class. It can either just do nothing, or you can implement some alternative
locale setup code there.

There will be a new implementation of `InitLocale` in 4.1.1 which should be
simpler and less likely to still have problems. But you'll still be able to
override `InitLocale` if needed.



.. toctree::
:maxdepth: 2
:hidden:
Expand Down
30 changes: 16 additions & 14 deletions etg/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,28 +506,30 @@ def run():

PyFunctionDef('InitLocale', '(self)',
doc="""\
Try to ensure that the C and Python locale is in sync with wxWidgets locale.
Try to ensure that the C and Python locale is in sync with the wxWidgets
locale on Windows. If you have troubles from the default behavior of this
method you can override it in a derived class to behave differently.
Please report the problem you encountered.
""",
body="""\
self.ResetLocale()
import locale
try:
loc, enc = locale.getlocale()
except ValueError:
loc = enc = None
# Try to set it to the same language as what is already set in the C locale
info = wx.Locale.FindLanguageInfo(loc) if loc else None
if info:
self._initial_locale = wx.Locale(info.Language)
else:
# otherwise fall back to the system default
self._initial_locale = wx.Locale(wx.LANGUAGE_DEFAULT)
if 'wxMSW' in PlatformInfo:
import locale
try:
lang, enc = locale.getdefaultlocale()
self._initial_locale = wx.Locale(lang, lang[:2], lang)
locale.setlocale(locale.LC_ALL, lang)
except ValueError as ex:
target = wx.LogStderr()
orig = wx.Log.SetActiveTarget(target)
wx.LogError("Unable to set default locale: '{}'".format(ex))
wx.Log.SetActiveTarget(orig)
"""),

PyFunctionDef('ResetLocale', '(self)',
doc="""\
Release the wx.Locale object created in :meth:`InitLocale`.
This will reset the application's locale to the previous settings.
This should reset the application's locale to the previous settings.
""",
body="""\
self._initial_locale = None
Expand Down
1 change: 1 addition & 0 deletions etg/intl.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def run():

module.addItem(etgtools.WigCode("""\
char* wxSetlocale(int category, const char *locale);
char* wxSetlocale(int category, const wxString& locale);
"""))


Expand Down

0 comments on commit 7ac363c

Please sign in to comment.