1313import locale
1414import gettext
1515import globalVars
16+ from logging import log
1617
1718#a few Windows locale constants
1819LOCALE_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
182202def getLanguage () -> str :
0 commit comments