Skip to content

Commit 5f52a76

Browse files
authored
Merge 29e6a9f into 0316158
2 parents 0316158 + 29e6a9f commit 5f52a76

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

source/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,11 @@ def handlePowerStatusChange(self):
447447
if wxLang:
448448
try:
449449
locale.Init(wxLang.Language)
450+
# Fix whatever wx locale did to python's locale
451+
languageHandler.setLocale(languageHandler.curLang)
450452
except:
451453
log.error("Failed to initialize wx locale",exc_info=True)
452-
454+
453455
log.debug("Initializing garbageHandler")
454456
garbageHandler.initialize()
455457

source/languageHandler.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import locale
1414
import gettext
1515
import globalVars
16+
from logging import log
1617

1718
#a few Windows locale constants
1819
LOCALE_SLANGUAGE=0x2
@@ -156,19 +157,6 @@ def setLanguage(lang):
156157
else:
157158
trans=gettext.translation("nvda", localedir="locale", languages=[lang])
158159
curLang=lang
159-
localeChanged=False
160-
#Try setting Python's locale to lang
161-
try:
162-
locale.setlocale(locale.LC_ALL,lang)
163-
localeChanged=True
164-
except:
165-
pass
166-
if not localeChanged and '_' in lang:
167-
#Python couldn'tsupport the language_country locale, just try language.
168-
try:
169-
locale.setlocale(locale.LC_ALL,lang.split('_')[0])
170-
except:
171-
pass
172160
#Set the windows locale for this thread (NVDA core) to this locale.
173161
LCID=localeNameToWindowsLCID(lang)
174162
ctypes.windll.kernel32.SetThreadLocale(LCID)
@@ -177,6 +165,38 @@ def setLanguage(lang):
177165
curLang="en"
178166
# #9207: Python 3.8 adds gettext.pgettext, so add it to the built-in namespace.
179167
trans.install(names=["pgettext"])
168+
setLocale(curLang)
169+
170+
171+
def setLocale(localeName):
172+
'''
173+
set python's locale using a localeName set by setLanguage
174+
'''
175+
# Try setting Python's locale to lang
176+
localeChanged = False
177+
try:
178+
locale.setlocale(locale.LC_ALL, localeName)
179+
localeChanged = True
180+
except locale.Error:
181+
pass
182+
if not localeChanged:
183+
# Python couldn't support the language-country locale, try language_country.
184+
try:
185+
localeName = localeName.replace('-', '_')
186+
locale.setlocale(locale.LC_ALL, localeName)
187+
localeChanged = True
188+
except locale.Error:
189+
pass
190+
if not localeChanged:
191+
# Python couldn't support the language_country locale, just try language.
192+
try:
193+
localeName = localeName.split('_')[0]
194+
locale.setlocale(locale.LC_ALL, localeName)
195+
localeChanged = True
196+
except locale.Error:
197+
pass
198+
if not localeChanged:
199+
log.warning("python locale could not be set")
180200

181201

182202
def getLanguage() -> str:

0 commit comments

Comments
 (0)