Patch #1349274: gettext.install() now optionally installs additional
authorGeorg Brandl <georg@python.org>
Sun, 19 Feb 2006 13:26:36 +0000 (13:26 +0000)
committerGeorg Brandl <georg@python.org>
Sun, 19 Feb 2006 13:26:36 +0000 (13:26 +0000)
translation functions other than _() in the builtin namespace.

Doc/lib/libgettext.tex
Lib/gettext.py
Lib/test/test_gettext.py
Misc/NEWS

index 593d964c8d1ba8e2773651f737d90d767db151a5..e41f8bff0ddd46d3b78f7b285651acf272e36235 100644 (file)
@@ -219,13 +219,16 @@ true.
 \end{funcdesc}
 
 \begin{funcdesc}{install}{domain\optional{, localedir\optional{, unicode
-                         \optional{, codeset}}}}
+                         \optional{, codeset\optional{, names}}}}}
 This installs the function \function{_} in Python's builtin namespace,
 based on \var{domain}, \var{localedir}, and \var{codeset} which are
 passed to the function \function{translation()}.  The \var{unicode}
 flag is passed to the resulting translation object's \method{install}
 method.
 
+For the \var{names} parameter, please see the description of the
+translation object's \method{install} method.
+
 As seen below, you usually mark the strings in your application that are
 candidates for translation, by wrapping them in a call to the
 \function{_()} function, like this:
@@ -239,6 +242,7 @@ Python's builtin namespace, so it is easily accessible in all modules
 of your application.  
 
 \versionchanged[Added the \var{codeset} parameter]{2.4}
+\versionchanged[Added the \var{names} parameter]{2.5}
 \end{funcdesc}
 
 \subsubsection{The \class{NullTranslations} class}
@@ -332,12 +336,21 @@ defines the encoding used to return translated messages.
 \versionadded{2.4}
 \end{methoddesc}
 
-\begin{methoddesc}[NullTranslations]{install}{\optional{unicode}}
+\begin{methoddesc}[NullTranslations]{install}{\optional{unicode
+                                              \optional{, names}}}
 If the \var{unicode} flag is false, this method installs
 \method{self.gettext()} into the built-in namespace, binding it to
 \samp{_}.  If \var{unicode} is true, it binds \method{self.ugettext()}
 instead.  By default, \var{unicode} is false.
 
+If the \var{names} parameter is given, it must be a sequence containing
+the names of functions you want to install in the builtin namespace in
+addition to \function{_()}. Supported names are \code{'gettext'} (bound
+to \method{self.gettext()} or \method{self.ugettext()} according to the
+\var{unicode} flag), \code{'ngettext'} (bound to \method{self.ngettext()}
+or \method{self.ungettext()} according to the \var{unicode} flag),
+\code{'lgettext'} and \code{'lngettext'}.
+
 Note that this is only one way, albeit the most convenient way, to
 make the \function{_} function available to your application.  Because it
 affects the entire application globally, and specifically the built-in
@@ -353,6 +366,8 @@ _ = t.gettext
 
 This puts \function{_} only in the module's global namespace and so
 only affects calls within this module.
+
+\versionchanged[Added the \var{names} parameter]{2.5}
 \end{methoddesc}
 
 \subsubsection{The \class{GNUTranslations} class}
index a20c6f15224d565b964b3f0c451eb0c928f9d154..90ebc51800164ff092b73c5ca9c1d99e1ea7a150 100644 (file)
@@ -239,9 +239,19 @@ class NullTranslations:
     def set_output_charset(self, charset):
         self._output_charset = charset
 
-    def install(self, unicode=False):
+    def install(self, unicode=False, names=None):
         import __builtin__
         __builtin__.__dict__['_'] = unicode and self.ugettext or self.gettext
+        if hasattr(names, "__contains__"):
+            if "gettext" in names:
+                __builtin__.__dict__['gettext'] = __builtin__.__dict__['_']
+            if "ngettext" in names:
+                __builtin__.__dict__['ngettext'] = (unicode and self.ungettext
+                                                             or self.ngettext)
+            if "lgettext" in names:
+                __builtin__.__dict__['lgettext'] = self.lgettext
+            if "lngettext" in names:
+                __builtin__.__dict__['lngettext'] = self.lngettext
 
 
 class GNUTranslations(NullTranslations):
@@ -479,9 +489,9 @@ def translation(domain, localedir=None, languages=None,
     return result
 
 
-def install(domain, localedir=None, unicode=False, codeset=None):
+def install(domain, localedir=None, unicode=False, codeset=None, names=None):
     t = translation(domain, localedir, fallback=True, codeset=codeset)
-    t.install(unicode)
+    t.install(unicode, names)
 
 
 
index 517dfc0f318fae4753915fc5602e0332b0efa324..76253dedf4f7c12f7a84bf79df7ba8a82565e4b4 100644 (file)
@@ -145,6 +145,14 @@ trggrkg zrffntr pngnybt yvoenel.''')
         # Try unicode return type
         t.install(unicode=True)
         eq(_('mullusk'), 'bacon')
+        # Test installation of other methods
+        import __builtin__
+        t.install(unicode=True, names=["gettext", "lgettext"])
+        eq(_, t.ugettext)
+        eq(__builtin__.gettext, t.ugettext)
+        eq(lgettext, t.lgettext)
+        del __builtin__.gettext
+        del __builtin__.lgettext
 
 
 class GettextTestCase2(GettextBaseTest):
index 47bb57aad7b0457a8c3b26e449e35097870d2513..95d13ba03491463fea2ba6bb06f91b82e64665ee 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -366,6 +366,9 @@ Extension Modules
 Library
 -------
 
+- Patch #1349274: gettext.install() now optionally installs additional
+  translation functions other than _() in the builtin namespace.
+
 - Patch #1337756: fileinput now accepts Unicode filenames.
 
 - Patch #1373643: The chunk module can now read chunks larger than