Skip to content

Commit c049531

Browse files
committed
Rename cmdLineLanguage -> language and store current language in globalVars.appArgs.language
1 parent 49628a7 commit c049531

6 files changed

Lines changed: 39 additions & 35 deletions

File tree

source/core.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,9 @@ def resetConfiguration(factoryDefaults=False):
202202
config.conf.reset(factoryDefaults=factoryDefaults)
203203
logHandler.setLogLevelFromConfig()
204204
# Language
205-
lang = globalVars.appArgs.cmdLineLanguage
206-
if not lang:
205+
if languageHandler.isLanguageForced():
206+
lang = globalVars.appArgs.language
207+
else:
207208
lang = config.conf["general"]["language"]
208209
log.debug("setting language to %s"%lang)
209210
languageHandler.setLanguage(lang)
@@ -418,10 +419,11 @@ def main():
418419
except:
419420
pass
420421
logHandler.setLogLevelFromConfig()
421-
lang = globalVars.appArgs.cmdLineLanguage
422-
if not lang:
423-
lang = config.conf["general"]["language"]
424422
import languageHandler
423+
if languageHandler.isLanguageForced():
424+
lang = globalVars.appArgs.language
425+
else:
426+
lang = config.conf["general"]["language"]
425427
log.debug(f"setting language to {lang}")
426428
languageHandler.setLanguage(lang)
427429
log.info(f"Windows version: {winVersion.getWinVer()}")
@@ -609,7 +611,7 @@ def handlePowerStatusChange(self):
609611
log.error("Failed to initialize wx locale",exc_info=True)
610612
finally:
611613
# Revert wx's changes to the python locale
612-
languageHandler.setLocale(languageHandler.curLang)
614+
languageHandler.setLocale(languageHandler.getLanguage())
613615

614616
log.debug("Initializing garbageHandler")
615617
garbageHandler.initialize()

source/gui/settingsDialogs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ def makeSettings(self, settingsSizer):
715715
languageChoices = [x[1] for x in self.languageNames]
716716
if languageHandler.isLanguageForced():
717717
cmdLangDescription = [
718-
ld[1] for ld in self.languageNames if ld[0] == globalVars.appArgs.cmdLineLanguage
718+
ld[1] for ld in self.languageNames if ld[0] == globalVars.appArgs.language
719719
][0]
720720
languageChoices.insert(
721721
0,

source/languageHandler.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
#: or because it is not a legal locale name (e.g. "zzzz").
3535
LCID_NONE = 0 # 0 used instead of None for backwards compatibility.
3636

37-
curLang="en"
38-
3937

4038
class LOCALE(enum.IntEnum):
4139
# Represents NLS constants which can be used with `GetLocaleInfoEx` or `GetLocaleInfoW`
@@ -321,10 +319,9 @@ def setLanguage(lang: str) -> None:
321319
Sets the following using `lang` such as "en", "ru_RU", or "es-ES". Use "Windows" to use the system locale
322320
- the windows locale for the thread (fallback to system locale)
323321
- the translation service (fallback to English)
324-
- languageHandler.curLang (match the translation service)
322+
- globalVars.appArgs.language (match the translation service)
325323
- the python locale for the thread (match the translation service, fallback to system default)
326324
'''
327-
global curLang
328325
if lang == "Windows":
329326
localeName = getWindowsLanguage()
330327
else:
@@ -338,20 +335,20 @@ def setLanguage(lang: str) -> None:
338335

339336
try:
340337
trans = gettext.translation("nvda", localedir="locale", languages=[localeName])
341-
curLang = localeName
338+
globalVars.appArgs.language = localeName
342339
except IOError:
343340
try:
344341
log.debugWarning(f"couldn't set the translation service locale to {localeName}")
345342
localeName = localeName.split("_")[0]
346343
trans = gettext.translation("nvda", localedir="locale", languages=[localeName])
347-
curLang = localeName
344+
globalVars.appArgs.language = localeName
348345
except IOError:
349346
log.debugWarning(f"couldn't set the translation service locale to {localeName}")
350347
trans = gettext.translation("nvda", fallback=True)
351-
curLang = "en"
348+
globalVars.appArgs.language = "en"
352349

353350
trans.install()
354-
setLocale(curLang)
351+
setLocale(getLanguage())
355352
# Install our pgettext function.
356353
builtins.pgettext = makePgettext(trans)
357354

@@ -392,7 +389,8 @@ def _setPythonLocale(localeString: str) -> bool:
392389
def setLocale(localeName: str) -> None:
393390
'''
394391
Set python's locale using a `localeName` such as "en", "ru_RU", or "es-ES".
395-
Will fallback on `curLang` if it cannot be set and finally fallback to the system locale.
392+
Will fallback on `globalVars.appArgs.language` if it cannot be set
393+
and finally fallback to the system locale.
396394
Passing NVDA locales straight to python `locale.setlocale` does now work since it tries to normalize the
397395
parameter using `locale.normalize` which results in locales unknown to Windows (Python issue 37945).
398396
For example executing: `locale.setlocale(locale.LC_ALL, "pl")`
@@ -426,18 +424,18 @@ def setLocale(localeName: str) -> None:
426424
return
427425
# Either Windows does not know the locale, or Python is unable to handle it.
428426
# reset to default locale
429-
if originalLocaleName == curLang:
427+
if originalLocaleName == getLanguage():
430428
# reset to system locale default if we can't set the current lang's locale
431429
locale.setlocale(locale.LC_ALL, "")
432430
log.debugWarning(f"set python locale to system default")
433431
else:
434-
log.debugWarning(f"setting python locale to the current language {curLang}")
432+
log.debugWarning(f"setting python locale to the current language {getLanguage()}")
435433
# fallback and try to reset the locale to the current lang
436-
setLocale(curLang)
434+
setLocale(getLanguage())
437435

438436

439437
def getLanguage() -> str:
440-
return curLang
438+
return globalVars.appArgs.language
441439

442440

443441
def normalizeLanguage(lang: str) -> Optional[str]:

source/nvda.pyw

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,13 @@ parser.add_argument('-l','--log-level',dest='logLevel',type=int,default=0,choice
152152
parser.add_argument('-c','--config-path',dest='configPath',default=None,type=str,help="The path where all settings for NVDA are stored")
153153
parser.add_argument(
154154
'--lang',
155-
dest='cmdLineLanguage',
156-
default=None,
155+
dest='language',
156+
default="en",
157157
type=stringToLang,
158-
help="Override the configured NVDA language. Set to \"Windows\" for current user default, \"en\" for English, etc."
158+
help=(
159+
"Override the configured NVDA language."
160+
" Set to \"Windows\" for current user default, \"en\" for English, etc."
161+
)
159162
)
160163
parser.add_argument('-m','--minimal',action="store_true",dest='minimal',default=False,help="No sounds, no interface, no start message etc")
161164
parser.add_argument('-s','--secure',action="store_true",dest='secure',default=False,help="Secure mode (disable Python console)")

tests/unit/test_languageHandler.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,18 @@ class Test_languageHandler_setLocale(unittest.TestCase):
159159

160160
def setUp(self):
161161
"""
162-
`setLocale` doesn't change `languageHandler.curLang`, so reset the locale using `setLanguage` to
162+
`setLocale` doesn't change `globalVars.appArgs.language`, so reset the locale using `setLanguage` to
163163
the current language for each test.
164164
"""
165-
languageHandler.setLanguage(languageHandler.curLang)
165+
languageHandler.setLanguage(languageHandler.getLanguage())
166166

167167
@classmethod
168168
def tearDownClass(cls):
169169
"""
170-
`setLocale` doesn't change `languageHandler.curLang`, so reset the locale using `setLanguage` to
170+
`setLocale` doesn't change `globalVars.appArgs.language`, so reset the locale using `setLanguage` to
171171
the current language so the tests can continue normally.
172172
"""
173-
languageHandler.setLanguage(languageHandler.curLang)
173+
languageHandler.setLanguage(languageHandler.getLanguage())
174174

175175
def test_SupportedLocale_LocaleIsSet(self):
176176
"""
@@ -262,16 +262,16 @@ def __init__(self, *args, **kwargs):
262262
def test_NVDASupportedLanguages_LanguageIsSetCorrectly(self):
263263
"""
264264
Tests languageHandler.setLanguage, using all NVDA supported languages, which should do the following:
265-
- set the translation service and languageHandler.curLang
265+
- set the translation service and globalVars.appArgs.language
266266
- set the windows locale for the thread (fallback to system default)
267267
- set the python locale for the thread (match the translation service, fallback to system default)
268268
"""
269269
for localeName in TRANSLATABLE_LANGS:
270270
with self.subTest(localeName=localeName):
271271
langOnly = localeName.split("_")[0]
272272
languageHandler.setLanguage(localeName)
273-
# check curLang/translation service is set
274-
self.assertEqual(languageHandler.curLang, localeName)
273+
# check globalVars.appArgs.language/translation service is set
274+
self.assertEqual(languageHandler.getLanguage(), localeName)
275275

276276
# check Windows thread is set
277277
threadLocale = ctypes.windll.kernel32.GetThreadLocale()

tests/unit/test_synthDriverHandler.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88
import config
99
import languageHandler
10+
import globalVars
1011
import synthDriverHandler
1112
from synthDrivers.oneCore import SynthDriver as OneCoreSynthDriver
1213
from typing import Callable
@@ -38,14 +39,14 @@ def _getDefaultVoice(self, pickAny: bool = True):
3839
class test_synthDriverHandler(unittest.TestCase):
3940

4041
def setUp(self) -> None:
41-
self._oldLang = languageHandler.curLang
42+
self._oldLang = languageHandler.getLanguage()
4243
self._oldSynthConfig = config.conf["speech"]["synth"]
4344
self._originalSynth = synthDriverHandler._curSynth
4445
self._originalGetSynthDriver = synthDriverHandler._getSynthDriver
4546
config.conf["speech"]["synth"] = FAKE_DEFAULT_LANG
4647
synthDriverHandler._curSynth = MockSynth(FAKE_DEFAULT_SYNTH_NAME)
4748
synthDriverHandler._getSynthDriver = self._mock_getSynthDriver
48-
languageHandler.curLang = FAKE_DEFAULT_LANG
49+
globalVars.appArgs.language = FAKE_DEFAULT_LANG
4950

5051
@staticmethod
5152
def _mock_getSynthDriver(synthName: str) -> Callable[[], MockSynth]:
@@ -55,7 +56,7 @@ def tearDown(self) -> None:
5556
config.conf["speech"]["synth"] = self._oldSynthConfig
5657
synthDriverHandler._curSynth = self._originalSynth
5758
synthDriverHandler._getSynthDriver = self._originalGetSynthDriver
58-
languageHandler.curLang = self._oldLang
59+
globalVars.appArgs.language = self._oldLang
5960

6061
def test_setSynth_auto(self):
6162
"""
@@ -99,7 +100,7 @@ def test_setSynth_auto_usesOneCore_ifSupportsDefaultLanguage(self):
99100
"""
100101
Ensures that if oneCore supports the current language, setSynth("auto") uses "oneCore".
101102
"""
102-
# test setup ensures curLang is supported for oneCore
103+
# test setup ensures `globalVars.appArgs.language` is supported for oneCore
103104
synthDriverHandler.setSynth(None) # reset the synth so there is no fallback
104105
synthDriverHandler.setSynth("auto")
105106
self.assertEqual(synthDriverHandler.getSynth().name, "oneCore")
@@ -110,7 +111,7 @@ def test_setSynth_auto_fallback_ifOneCoreDoesntSupportDefaultLanguage(self):
110111
Ensures that if oneCore doesn't support the current language, setSynth("auto") falls back to the
111112
current synth, or espeak if there is no current synth.
112113
"""
113-
languageHandler.curLang = "bar" # set the lang so it is not supported
114+
globalVars.appArgs.language = "bar" # set the lang so it is not supported
114115
synthDriverHandler.setSynth("auto")
115116
self.assertEqual(synthDriverHandler.getSynth().name, FAKE_DEFAULT_SYNTH_NAME)
116117
synthDriverHandler.setSynth(None) # reset the synth so there is no fallback

0 commit comments

Comments
 (0)