Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 602b9ba

Browse files
committed
Patch #1349274: gettext.install() now optionally installs additional
translation functions other than _() in the builtin namespace.
1 parent e466217 commit 602b9ba

4 files changed

Lines changed: 41 additions & 5 deletions

File tree

Doc/lib/libgettext.tex

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,16 @@ \subsection{Class-based API}
219219
\end{funcdesc}
220220

221221
\begin{funcdesc}{install}{domain\optional{, localedir\optional{, unicode
222-
\optional{, codeset}}}}
222+
\optional{, codeset\optional{, names}}}}}
223223
This installs the function \function{_} in Python's builtin namespace,
224224
based on \var{domain}, \var{localedir}, and \var{codeset} which are
225225
passed to the function \function{translation()}. The \var{unicode}
226226
flag is passed to the resulting translation object's \method{install}
227227
method.
228228

229+
For the \var{names} parameter, please see the description of the
230+
translation object's \method{install} method.
231+
229232
As seen below, you usually mark the strings in your application that are
230233
candidates for translation, by wrapping them in a call to the
231234
\function{_()} function, like this:
@@ -239,6 +242,7 @@ \subsection{Class-based API}
239242
of your application.
240243

241244
\versionchanged[Added the \var{codeset} parameter]{2.4}
245+
\versionchanged[Added the \var{names} parameter]{2.5}
242246
\end{funcdesc}
243247

244248
\subsubsection{The \class{NullTranslations} class}
@@ -332,12 +336,21 @@ \subsubsection{The \class{NullTranslations} class}
332336
\versionadded{2.4}
333337
\end{methoddesc}
334338

335-
\begin{methoddesc}[NullTranslations]{install}{\optional{unicode}}
339+
\begin{methoddesc}[NullTranslations]{install}{\optional{unicode
340+
\optional{, names}}}
336341
If the \var{unicode} flag is false, this method installs
337342
\method{self.gettext()} into the built-in namespace, binding it to
338343
\samp{_}. If \var{unicode} is true, it binds \method{self.ugettext()}
339344
instead. By default, \var{unicode} is false.
340345

346+
If the \var{names} parameter is given, it must be a sequence containing
347+
the names of functions you want to install in the builtin namespace in
348+
addition to \function{_()}. Supported names are \code{'gettext'} (bound
349+
to \method{self.gettext()} or \method{self.ugettext()} according to the
350+
\var{unicode} flag), \code{'ngettext'} (bound to \method{self.ngettext()}
351+
or \method{self.ungettext()} according to the \var{unicode} flag),
352+
\code{'lgettext'} and \code{'lngettext'}.
353+
341354
Note that this is only one way, albeit the most convenient way, to
342355
make the \function{_} function available to your application. Because it
343356
affects the entire application globally, and specifically the built-in
@@ -353,6 +366,8 @@ \subsubsection{The \class{NullTranslations} class}
353366

354367
This puts \function{_} only in the module's global namespace and so
355368
only affects calls within this module.
369+
370+
\versionchanged[Added the \var{names} parameter]{2.5}
356371
\end{methoddesc}
357372

358373
\subsubsection{The \class{GNUTranslations} class}

Lib/gettext.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,19 @@ def output_charset(self):
239239
def set_output_charset(self, charset):
240240
self._output_charset = charset
241241

242-
def install(self, unicode=False):
242+
def install(self, unicode=False, names=None):
243243
import __builtin__
244244
__builtin__.__dict__['_'] = unicode and self.ugettext or self.gettext
245+
if hasattr(names, "__contains__"):
246+
if "gettext" in names:
247+
__builtin__.__dict__['gettext'] = __builtin__.__dict__['_']
248+
if "ngettext" in names:
249+
__builtin__.__dict__['ngettext'] = (unicode and self.ungettext
250+
or self.ngettext)
251+
if "lgettext" in names:
252+
__builtin__.__dict__['lgettext'] = self.lgettext
253+
if "lngettext" in names:
254+
__builtin__.__dict__['lngettext'] = self.lngettext
245255

246256

247257
class GNUTranslations(NullTranslations):
@@ -479,9 +489,9 @@ def translation(domain, localedir=None, languages=None,
479489
return result
480490

481491

482-
def install(domain, localedir=None, unicode=False, codeset=None):
492+
def install(domain, localedir=None, unicode=False, codeset=None, names=None):
483493
t = translation(domain, localedir, fallback=True, codeset=codeset)
484-
t.install(unicode)
494+
t.install(unicode, names)
485495

486496

487497

Lib/test/test_gettext.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ def test_the_alternative_interface(self):
145145
# Try unicode return type
146146
t.install(unicode=True)
147147
eq(_('mullusk'), 'bacon')
148+
# Test installation of other methods
149+
import __builtin__
150+
t.install(unicode=True, names=["gettext", "lgettext"])
151+
eq(_, t.ugettext)
152+
eq(__builtin__.gettext, t.ugettext)
153+
eq(lgettext, t.lgettext)
154+
del __builtin__.gettext
155+
del __builtin__.lgettext
148156

149157

150158
class GettextTestCase2(GettextBaseTest):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ Extension Modules
366366
Library
367367
-------
368368

369+
- Patch #1349274: gettext.install() now optionally installs additional
370+
translation functions other than _() in the builtin namespace.
371+
369372
- Patch #1337756: fileinput now accepts Unicode filenames.
370373

371374
- Patch #1373643: The chunk module can now read chunks larger than

0 commit comments

Comments
 (0)