通过在wxpython 3.0.2.0中创建wxLocale对象来更改语言环境

时间:2019-01-14 08:36:50

标签: python python-2.7 localization wxpython

我正在维护旧版python 2应用,并且尝试从wxpython 2.8.12.1升级到3.0.2-我收到此断言错误:

 Traceback (most recent call last):
      File "Wrye Bash Launcher.pyw", line 88, in <module>
        bash.main()
      File "bash\bash.py", line 428, in main
        frame = app.Init() # Link.Frame is set here !
      File "bash\basher\__init__.py", line 4316, in Init
        size=settings['bash.frameSize'])
      File "bash\basher\__init__.py", line 3906, in __init__
        self.SetStatusBar(BashStatusBar(self))
      File "bash\basher\__init__.py", line 3669, in __init__
        self.UpdateIconSizes()
      File "bash\basher\__init__.py", line 3707, in UpdateIconSizes
        self._addButton(link)
      File "bash\basher\__init__.py", line 3681, in _addButton
        gButton = link.GetBitmapButton(self,style=wx.NO_BORDER)
      File "bash\basher\app_buttons.py", line 657, in GetBitmapButton
        image=staticBitmap(window, special='undo', size=(size,size)))
      File "bash\balt.py", line 509, in staticBitmap
        return bmp(wx.ART_UNDO,wx.ART_TOOLBAR,size)
      File "C:\_\Python27\lib\site-packages\wx-3.0-msw\wx\_misc.py", line 3013, in ArtProvider_GetBitmap
        return _misc_.ArtProvider_GetBitmap(*args, **kwargs)
    wx._core.PyAssertionError: C++ assertion "strcmp(setlocale(LC_ALL, NULL), "C") == 0" failed at ..\..\src\common\intl.cpp(1449) in wxLocale::GetInfo(): You probably called setlocale() directly instead of using wxLocale and now there is a mismatch between C/C++ and Windows locale.
    Things are going to break, please only change locale by creating wxLocale objects to avoid this!

它说:

  

您可能直接调用了setlocale()而不是使用wxLocale,现在C / C ++与Windows语言环境不匹配

(什么是Windows,什么是C / C ++语言环境?)

好吧,我这样做的方法是:

#--Do translator test and set
if locale.getlocale() == (None,None):
    locale.setlocale(locale.LC_ALL,u'')
initTranslator(language)

locale.setlocale的文档中,我们读到:

  

应用程序通常以

开头
import locale
locale.setlocale(locale.LC_ALL, '')
     

这会将所有类别的语言环境设置为用户的默认设置

然后在程序中,在as in上都定义了可识别语言环境的功能:

def formatInteger(value):
    """Convert integer to string formatted to locale."""
    return decode(locale.format('%d', int(value), True),
                  locale.getpreferredencoding())

省略对locale.setlocale的调用会使程序启动,但是我认为这些功能将不再有用(编辑:已确认)。

wx.Image is throwing PyAssertionError中存在类似的问题,但是该解决方案建议修改wx.App的语言环境,而我的代码中的setlocale调用甚至在导入wx之前就已发出。人们也将构造用作wx.Locale(wx.LANGUAGE_ENGLISH),但这显然是一个hack,我不想强​​制用户使用英语的语言环境-有关另一个hack,请参见here

上面的initTranslator方法使用标准的gettextmsgfmt机制。

问题

我需要一种标准的 non hacky 方式,用wx模拟代替locale.setlocale调用,而又不会破坏程序的翻译和语言环境感知部分。我已经有将近四年的这个问题了,没有找到规范的答案。

编辑:省略setlocale调用不会影响翻译(是!),但确实会破坏语言环境感知的显示功能。

EDIT2 :将上面的呼叫替换为:

-    # if locale.getlocale() == (None,None):
-    #     locale.setlocale(locale.LC_ALL,u'')
+    if locale.getlocale() == (None,None):
+        # locale.setlocale(locale.LC_ALL,u'')
+        import wx
+        wx.Locale(wx.LANGUAGE_DEFAULT)
     if not lang:

导致一系列警告:

7:12:22 AM: Debug: ..\..\src\common\stdpbase.cpp(56): assert "traits" failed in wxStandardPathsBase::Get(): create wxApp before calling this
7:12:41 AM: Debug: ..\..\src\common\stdpbase.cpp(56): assert "traits" failed in wxStandardPathsBase::Get(): create wxApp before calling this
7:12:42 AM: Debug: ..\..\src\common\stdpbase.cpp(56): assert "traits" failed in wxStandardPathsBase::Get(): create wxApp before calling this
7:12:42 AM: Debug: ..\..\src\common\platinfo.cpp(171): assert "wxAssertFailure" failed in wxPlatformInfo::InitForCurrentPlatform(): failed to initialize wxPlatformInfo
7:12:52 AM: Debug: ..\..\src\common\platinfo.cpp(109): assert "value" failed in wxGetIndexFromEnumValue(): invalid enum value
7:13:12 AM: Debug: ..\..\src\common\platinfo.cpp(240): assert "idx < (sizeof(wxPortIdNames)/sizeof(wxPortIdNames[0]))" failed in wxPlatformInfo::GetPortIdName(): invalid port id

所以我必须创建wx.App(我很想避免这种情况,因为我希望能够在创建wx.App之前很久就翻译(可能是致命的)错误消息)

0 个答案:

没有答案