OSDN Git Service

Gettext message contexts are now supported. This allows multiple translations to...
authorJames Teh <jamie@jantrid.net>
Thu, 13 Sep 2012 01:25:48 +0000 (11:25 +1000)
committerJames Teh <jamie@jantrid.net>
Thu, 13 Sep 2012 01:25:48 +0000 (11:25 +1000)
Fixes #1524.

source/addonHandler.py
source/languageHandler.py
user_docs/en/changes.t2t

index 58f58af..76f7a91 100644 (file)
@@ -325,6 +325,8 @@ def initTranslation():
        try:
                callerFrame = inspect.currentframe().f_back
                callerFrame.f_globals['_'] = translations.ugettext
+               # Install our pgettext function.
+               callerFrame.f_globals['pgettext'] = languageHandler.makePgettext(translations)
        finally:
                del callerFrame # Avoid reference problems with frames (per python docs)
 
index ef82a41..0d99414 100644 (file)
@@ -1,3 +1,4 @@
+import __builtin__\r
 import os\r
 import sys\r
 import ctypes\r
@@ -71,16 +72,30 @@ def getAvailableLanguages():
        #return a zipped up version of both the lists (a list with tuples of locale,label)\r
        return zip(l,d)\r
 \r
+def makePgettext(translations):\r
+       if isinstance(translations, gettext.GNUTranslations):\r
+               def pgettext(context, message):\r
+                       message = unicode(message)\r
+                       try:\r
+                               # Look up the message with its context.\r
+                               return translations._catalog[u"%s\x04%s" % (context, message)]\r
+                       except KeyError:\r
+                               return message\r
+       else:\r
+               def pgettext(context, message):\r
+                       return unicode(message)\r
+       return pgettext\r
+\r
 def setLanguage(lang):\r
        global curLang\r
        try:\r
                if lang=="Windows":\r
                        windowsLCID=ctypes.windll.kernel32.GetUserDefaultUILanguage()\r
                        localeName=locale.windows_locale[windowsLCID]\r
-                       gettext.translation('nvda',localedir='locale',languages=[localeName]).install(True)\r
+                       trans=gettext.translation('nvda',localedir='locale',languages=[localeName])\r
                        curLang=localeName\r
                else:\r
-                       gettext.translation("nvda", localedir="locale", languages=[lang]).install(True)\r
+                       trans=gettext.translation("nvda", localedir="locale", languages=[lang])\r
                        curLang=lang\r
                        localeChanged=False\r
                        #Try setting Python's locale to lang\r
@@ -98,11 +113,12 @@ def setLanguage(lang):
                        #Set the windows locale for this thread (NVDA core) to this locale.\r
                        LCID=localeNameToWindowsLCID(lang)\r
                        ctypes.windll.kernel32.SetThreadLocale(LCID)\r
-               return True\r
        except IOError:\r
-               gettext.install("nvda", unicode=True)\r
+               trans=gettext.translation("nvda",fallback=True)\r
                curLang="en"\r
-               return False\r
+       trans.install(unicode=True)\r
+       # Install our pgettext function.\r
+       __builtin__.__dict__["pgettext"] = makePgettext(trans)\r
 \r
 def getLanguage():\r
        return curLang\r
index 84b3ecf..cdcc357 100644 (file)
 - Previous log file is now copied to nvda-old.log on NVDA initialization. Therefore, if NVDA crashes or is restarted, logging information from that session is still accessible for inspection. (#916)\r
 - Fetching the role property in chooseNVDAObjectOverlayClasses no longer causes the role to be incorrect and thus not reported on focus for certain objects such as Windows command consoles and Scintilla controls. (#2569)\r
 - The NVDA Preferences, Tools and Help menus are now accessible as attributes on gui.mainFrame.sysTrayIcon named preferencesMenu, toolsMenu and helpMenu, respectively. This allows plugins to more easily add items to these menus.\r
+- Gettext message contexts are now supported. This allows multiple translations to be defined for a single English message depending on the context. (#1524)\r
+ - This is done using the pgettext(context, message) function.\r
+ - This is supported for both NVDA itself and add-ons.\r
+ - xgettext and msgfmt from GNU gettext must be used to create any PO and MO files. The Python tools do not support message contexts.\r
+ - For xgettext, pass the --keyword=pgettext:1c,2 command line argument to enable inclusion of message contexts.\r
+ - See http://www.gnu.org/software/gettext/manual/html_node/Contexts.html#Contexts for more information.\r
 \r
 \r
 = 2012.2.1 =\r