Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3.8/gettext: replace custom pgettext implementation with built--in pgettext #12109

Merged
merged 7 commits into from
Mar 11, 2021
14 changes: 7 additions & 7 deletions source/addonHandler/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- coding: UTF-8 -*-
#addonHandler.py
#A part of NonVisual Desktop Access (NVDA)
#Copyright (C) 2012-2019 Rui Batista, NV Access Limited, Noelia Ruiz Martínez, Joseph Lee, Babbage B.V., Arnold Loubriat
#This file is covered by the GNU General Public License.
#See the file COPYING for more details.
# A part of NonVisual Desktop Access (NVDA)
# Copyright (C) 2012-2021 NV Access Limited, Rui Batista, Noelia Ruiz Martínez,
# Joseph Lee, Babbage B.V., Arnold Loubriat
# This file is covered by the GNU General Public License.
# See the file COPYING for more details.

import sys
import os.path
Expand Down Expand Up @@ -539,8 +539,8 @@ def initTranslation():
try:
callerFrame = inspect.currentframe().f_back
callerFrame.f_globals['_'] = translations.gettext
# Install our pgettext function.
callerFrame.f_globals['pgettext'] = languageHandler.makePgettext(translations)
# Install pgettext function.
callerFrame.f_globals['pgettext'] = translations.pgettext
finally:
del callerFrame # Avoid reference problems with frames (per python docs)

Expand Down
37 changes: 6 additions & 31 deletions source/languageHandler.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#languageHandler.py
#A part of NonVisual Desktop Access (NVDA)
#Copyright (C) 2007-2018 NV access Limited, Joseph Lee
#This file is covered by the GNU General Public License.
#See the file COPYING for more details.
# A part of NonVisual Desktop Access (NVDA)
# Copyright (C) 2007-2021 NV Access Limited, Joseph Lee
# This file is covered by the GNU General Public License.
# See the file COPYING for more details.

"""Language and localization support.
This module assists in NVDA going global through language services such as converting Windows locale ID's to friendly names and presenting available languages.
"""

import builtins
import os
import sys
import ctypes
Expand Down Expand Up @@ -123,27 +121,6 @@ def getAvailableLanguages(presentational=False):
)
return langs

def makePgettext(translations):
"""Obtaina pgettext function for use with a gettext translations instance.
pgettext is used to support message contexts,
but Python's gettext module doesn't support this,
so NVDA must provide its own implementation.
"""
if isinstance(translations, gettext.GNUTranslations):
def pgettext(context, message):
try:
# Look up the message with its context.
return translations._catalog[u"%s\x04%s" % (context, message)]
except KeyError:
return message
elif isinstance(translations, gettext.NullTranslations):
# A language with out a translation catalog, such as English.
def pgettext(context, message):
return message
else:
raise ValueError("%s is Not a GNUTranslations or NullTranslations object"%translations)
return pgettext

def getWindowsLanguage():
"""
Fetches the locale name of the user's configured language in Windows.
Expand Down Expand Up @@ -198,10 +175,8 @@ def setLanguage(lang):
except IOError:
trans=gettext.translation("nvda",fallback=True)
curLang="en"
trans.install()
# Install our pgettext function.
import builtins
builtins.pgettext = makePgettext(trans)
# #9207: Python 3.8 adds gettext.pgettext, so add it to the built-in namespace.
trans.install(names=["pgettext"])

def getLanguage():
return curLang
Expand Down