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

Skip to content

Commit 801844d

Browse files
committed
#2512 implement the 3.0 gettext API
All the u* gettext variants were renamed to their none u* variants, since there's no point in translating to byte strings. I also killed off the unicode parameters for install
1 parent fbe94c5 commit 801844d

4 files changed

Lines changed: 83 additions & 148 deletions

File tree

Doc/library/gettext.rst

Lines changed: 66 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,11 @@ class can also install themselves in the built-in namespace as the function
187187
:class:`NullTranslations` instance if *fallback* is true.
188188

189189

190-
.. function:: install(domain[, localedir[, unicode [, codeset[, names]]]])
190+
.. function:: install(domain[, localedir [, codeset[, names]]]])
191191

192192
This installs the function :func:`_` in Python's builtin namespace, based on
193193
*domain*, *localedir*, and *codeset* which are passed to the function
194-
:func:`translation`. The *unicode* flag is passed to the resulting translation
195-
object's :meth:`install` method.
194+
:func:`translation`.
196195

197196
For the *names* parameter, please see the description of the translation
198197
object's :meth:`install` method.
@@ -227,107 +226,91 @@ are the methods of :class:`NullTranslations`:
227226
``None``.
228227

229228

230-
.. method:: _parse(fp)
231-
232-
No-op'd in the base class, this method takes file object *fp*, and reads
233-
the data from the file, initializing its message catalog. If you have an
234-
unsupported message catalog file format, you should override this method
235-
to parse your format.
236-
229+
.. method:: NullTranslations._parse(fp)
237230

238-
.. method:: add_fallback(fallback)
231+
No-op'd in the base class, this method takes file object *fp*, and reads the
232+
data from the file, initializing its message catalog. If you have an
233+
unsupported message catalog file format, you should override this method to
234+
parse your format.
239235

240-
Add *fallback* as the fallback object for the current translation
241-
object. A translation object should consult the fallback if it cannot provide a
242-
translation for a given message.
243236

237+
.. method:: NullTranslations.add_fallback(fallback)
244238

245-
.. method:: gettext(message)
239+
Add *fallback* as the fallback object for the current translation object. A
240+
translation object should consult the fallback if it cannot provide a
241+
translation for a given message.
246242

247-
If a fallback has been set, forward :meth:`gettext` to the
248-
fallback. Otherwise, return the translated message. Overridden in derived
249-
classes.
250243

244+
.. method:: NullTranslations.gettext(message)
251245

252-
.. method:: lgettext(message)
246+
If a fallback has been set, forward :meth:`gettext` to the fallback. Otherwise,
247+
return the translated message. Overridden in derived classes.
253248

254-
If a fallback has been set, forward :meth:`lgettext` to the
255-
fallback. Otherwise, return the translated message. Overridden in derived
256-
classes.
257249

258-
.. method:: ugettext(message)
250+
.. method:: NullTranslations.lgettext(message)
259251

260-
If a fallback has been set, forward :meth:`ugettext` to the
261-
fallback. Otherwise, return the translated message as a string. Overridden
262-
in derived classes.
252+
If a fallback has been set, forward :meth:`lgettext` to the fallback. Otherwise,
253+
return the translated message. Overridden in derived classes.
263254

264255

265-
.. method:: ngettext(singular, plural, n)
256+
.. method:: NullTranslations.ngettext(singular, plural, n)
266257

267-
If a fallback has been set, forward :meth:`ngettext` to the
268-
fallback. Otherwise, return the translated message. Overridden in derived
269-
classes.
258+
If a fallback has been set, forward :meth:`ngettext` to the fallback. Otherwise,
259+
return the translated message. Overridden in derived classes.
270260

271-
.. method:: lngettext(singular, plural, n)
272261

273-
If a fallback has been set, forward :meth:`ngettext` to the
274-
fallback. Otherwise, return the translated message. Overridden in derived
275-
classes.
262+
.. method:: NullTranslations.lngettext(singular, plural, n)
276263

277-
.. method:: ungettext(singular, plural, n)
264+
If a fallback has been set, forward :meth:`ngettext` to the fallback. Otherwise,
265+
return the translated message. Overridden in derived classes.
278266

279-
If a fallback has been set, forward :meth:`ungettext` to the fallback.
280-
Otherwise, return the translated message as a string. Overridden in
281-
derived classes.
282267

283-
.. method:: info()
268+
.. method:: NullTranslations.info()
284269

285-
Return the "protected" :attr:`_info` variable.
270+
Return the "protected" :attr:`_info` variable.
286271

287272

288-
.. method:: charset()
273+
.. method:: NullTranslations.charset()
289274

290-
Return the "protected" :attr:`_charset` variable.
275+
Return the "protected" :attr:`_charset` variable.
291276

292277

293-
.. method:: output_charset()
278+
.. method:: NullTranslations.output_charset()
294279

295-
Return the "protected" :attr:`_output_charset` variable, which defines the
296-
encoding used to return translated messages.
280+
Return the "protected" :attr:`_output_charset` variable, which defines the
281+
encoding used to return translated messages.
297282

298283

299-
.. method:: set_output_charset(charset)
284+
.. method:: NullTranslations.set_output_charset(charset)
300285

301-
Change the "protected" :attr:`_output_charset` variable, which defines the
302-
encoding used to return translated messages.
286+
Change the "protected" :attr:`_output_charset` variable, which defines the
287+
encoding used to return translated messages.
303288

304289

305-
.. method:: install([unicode [, names]])
290+
.. method:: NullTranslations.install([names])
306291

307-
If the *unicode* flag is false, this method installs :meth:`self.gettext`
308-
into the built-in namespace, binding it to ``_``. If *unicode* is true,
309-
it binds :meth:`self.ugettext` instead. By default, *unicode* is false.
292+
this method installs :meth:`self.gettext` into the built-in namespace,
293+
binding it to ``_``.
310294

311-
If the *names* parameter is given, it must be a sequence containing the
312-
names of functions you want to install in the builtin namespace in
313-
addition to :func:`_`. Supported names are ``'gettext'`` (bound to
314-
:meth:`self.gettext` or :meth:`self.ugettext` according to the *unicode*
315-
flag), ``'ngettext'`` (bound to :meth:`self.ngettext` or
316-
:meth:`self.ungettext` according to the *unicode* flag), ``'lgettext'``
317-
and ``'lngettext'``.
295+
If the *names* parameter is given, it must be a sequence containing
296+
the names of functions you want to install in the builtin namespace
297+
in addition to :func:`_`. Supported names are ``'gettext'`` (bound
298+
to :meth:`self.gettext`), ``'ngettext'`` (bound to
299+
:meth:`self.ngettext`), ``'lgettext'`` and ``'lngettext'``.
318300

319-
Note that this is only one way, albeit the most convenient way, to make
320-
the :func:`_` function available to your application. Because it affects
321-
the entire application globally, and specifically the built-in namespace,
322-
localized modules should never install :func:`_`. Instead, they should use
323-
this code to make :func:`_` available to their module::
301+
Note that this is only one way, albeit the most convenient way, to
302+
make the :func:`_` function available to your application. Because
303+
it affects the entire application globally, and specifically the
304+
built-in namespace, localized modules should never install
305+
:func:`_`. Instead, they should use this code to make :func:`_`
306+
available to their module::
324307

325-
import gettext
326-
t = gettext.translation('mymodule', ...)
327-
_ = t.gettext
308+
import gettext
309+
t = gettext.translation('mymodule', ...)
310+
_ = t.gettext
328311

329-
This puts :func:`_` only in the module's global namespace and so only
330-
affects calls within this module.
312+
This puts :func:`_` only in the module's global namespace and so only
313+
affects calls within this module.
331314

332315

333316
The :class:`GNUTranslations` class
@@ -336,8 +319,7 @@ The :class:`GNUTranslations` class
336319
The :mod:`gettext` module provides one additional class derived from
337320
:class:`NullTranslations`: :class:`GNUTranslations`. This class overrides
338321
:meth:`_parse` to enable reading GNU :program:`gettext` format :file:`.mo` files
339-
in both big-endian and little-endian format. It also coerces both message ids
340-
and message strings to Unicode.
322+
in both big-endian and little-endian format.
341323

342324
:class:`GNUTranslations` parses optional meta-data out of the translation
343325
catalog. It is convention with GNU :program:`gettext` to include meta-data as
@@ -347,12 +329,7 @@ key ``Content-Type`` is found, then the ``charset`` property is used to
347329
initialize the "protected" :attr:`_charset` instance variable, defaulting to
348330
``None`` if not found. If the charset encoding is specified, then all message
349331
ids and message strings read from the catalog are converted to Unicode using
350-
this encoding. The :meth:`ugettext` method always returns a Unicode, while the
351-
:meth:`gettext` returns an encoded bytestring. For the message id arguments
352-
of both methods, either Unicode strings or bytestrings containing only
353-
US-ASCII characters are acceptable. Note that the Unicode version of the
354-
methods (i.e. :meth:`ugettext` and :meth:`ungettext`) are the recommended
355-
interface to use for internationalized Python programs.
332+
this encoding.
356333

357334
The entire set of key/value pairs are placed into a dictionary and set as the
358335
"protected" :attr:`_info` instance variable.
@@ -380,14 +357,6 @@ The following methods are overridden from the base class implementation:
380357
:meth:`set_output_charset`.
381358

382359

383-
.. method:: GNUTranslations.ugettext(message)
384-
385-
Look up the *message* id in the catalog and return the corresponding message
386-
string, as a string. If there is no entry in the catalog for the
387-
*message* id, and a fallback has been set, the look up is forwarded to the
388-
fallback's :meth:`ugettext` method. Otherwise, the *message* id is returned.
389-
390-
391360
.. method:: GNUTranslations.ngettext(singular, plural, n)
392361

393362
Do a plural-forms lookup of a message id. *singular* is used as the message id
@@ -398,36 +367,24 @@ The following methods are overridden from the base class implementation:
398367
If the message id is not found in the catalog, and a fallback is specified, the
399368
request is forwarded to the fallback's :meth:`ngettext` method. Otherwise, when
400369
*n* is 1 *singular* is returned, and *plural* is returned in all other cases.
401-
402-
403-
.. method:: GNUTranslations.lngettext(singular, plural, n)
404-
405-
Equivalent to :meth:`gettext`, but the translation is returned in the preferred
406-
system encoding, if no other encoding was explicitly set with
407-
:meth:`set_output_charset`.
408-
409-
410-
.. method:: GNUTranslations.ungettext(singular, plural, n)
411-
412-
Do a plural-forms lookup of a message id. *singular* is used as the message id
413-
for purposes of lookup in the catalog, while *n* is used to determine which
414-
plural form to use. The returned message string is a string.
415-
416-
If the message id is not found in the catalog, and a fallback is specified, the
417-
request is forwarded to the fallback's :meth:`ungettext` method. Otherwise,
418-
when *n* is 1 *singular* is returned, and *plural* is returned in all other
419-
cases.
420-
370+
421371
Here is an example::
422372

423373
n = len(os.listdir('.'))
424374
cat = GNUTranslations(somefile)
425-
message = cat.ungettext(
375+
message = cat.ngettext(
426376
'There is %(num)d file in this directory',
427377
'There are %(num)d files in this directory',
428378
n) % {'num': n}
429379

430380

381+
.. method:: GNUTranslations.lngettext(singular, plural, n)
382+
383+
Equivalent to :meth:`gettext`, but the translation is returned in the preferred
384+
system encoding, if no other encoding was explicitly set with
385+
:meth:`set_output_charset`.
386+
387+
431388
Solaris message catalog support
432389
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
433390

@@ -538,13 +495,6 @@ module::
538495
t = gettext.translation('spam', '/usr/share/locale')
539496
_ = t.lgettext
540497

541-
If your translators were providing you with Unicode strings in their :file:`.po`
542-
files, you'd instead do::
543-
544-
import gettext
545-
t = gettext.translation('spam', '/usr/share/locale')
546-
_ = t.ugettext
547-
548498

549499
Localizing your application
550500
^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -560,11 +510,11 @@ driver file of your application::
560510
import gettext
561511
gettext.install('myapplication')
562512

563-
If you need to set the locale directory or the *unicode* flag, you can pass
564-
these into the :func:`install` function::
513+
If you need to set the locale directory, you can pass these into the
514+
:func:`install` function::
565515

566516
import gettext
567-
gettext.install('myapplication', '/usr/share/locale', unicode=1)
517+
gettext.install('myapplication', '/usr/share/locale')
568518

569519

570520
Changing languages on the fly

Lib/gettext.py

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,6 @@ def lngettext(self, msgid1, msgid2, n):
210210
else:
211211
return msgid2
212212

213-
def ugettext(self, message):
214-
if self._fallback:
215-
return self._fallback.ugettext(message)
216-
return str(message)
217-
218-
def ungettext(self, msgid1, msgid2, n):
219-
if self._fallback:
220-
return self._fallback.ungettext(msgid1, msgid2, n)
221-
if n == 1:
222-
return str(msgid1)
223-
else:
224-
return str(msgid2)
225-
226213
def info(self):
227214
return self._info
228215

@@ -235,15 +222,14 @@ def output_charset(self):
235222
def set_output_charset(self, charset):
236223
self._output_charset = charset
237224

238-
def install(self, str=False, names=None):
225+
def install(self, names=None):
239226
import builtins
240-
builtins.__dict__['_'] = str and self.ugettext or self.gettext
227+
builtins.__dict__['_'] = self.gettext
241228
if hasattr(names, "__contains__"):
242229
if "gettext" in names:
243230
builtins.__dict__['gettext'] = builtins.__dict__['_']
244231
if "ngettext" in names:
245-
builtins.__dict__['ngettext'] = (str and self.ungettext
246-
or self.ngettext)
232+
builtins.__dict__['ngettext'] = self.ngettext
247233
if "lgettext" in names:
248234
builtins.__dict__['lgettext'] = self.lgettext
249235
if "lngettext" in names:
@@ -367,31 +353,27 @@ def lngettext(self, msgid1, msgid2, n):
367353
else:
368354
return msgid2
369355

370-
def ugettext(self, message):
356+
def gettext(self, message):
371357
missing = object()
372358
tmsg = self._catalog.get(message, missing)
373359
if tmsg is missing:
374360
if self._fallback:
375-
return self._fallback.ugettext(message)
361+
return self._fallback.gettext(message)
376362
return str(message)
377363
return tmsg
378364

379-
gettext = ugettext
380-
381-
def ungettext(self, msgid1, msgid2, n):
365+
def ngettext(self, msgid1, msgid2, n):
382366
try:
383367
tmsg = self._catalog[(msgid1, self.plural(n))]
384368
except KeyError:
385369
if self._fallback:
386-
return self._fallback.ungettext(msgid1, msgid2, n)
370+
return self._fallback.ngettext(msgid1, msgid2, n)
387371
if n == 1:
388372
tmsg = str(msgid1)
389373
else:
390374
tmsg = str(msgid2)
391375
return tmsg
392376

393-
ngettext = ungettext
394-
395377

396378
# Locate a .mo file using the gettext strategy
397379
def find(domain, localedir=None, languages=None, all=0):
@@ -465,9 +447,9 @@ def translation(domain, localedir=None, languages=None,
465447
return result
466448

467449

468-
def install(domain, localedir=None, str=False, codeset=None, names=None):
450+
def install(domain, localedir=None, codeset=None, names=None):
469451
t = translation(domain, localedir, fallback=True, codeset=codeset)
470-
t.install(str, names)
452+
t.install(names)
471453

472454

473455

0 commit comments

Comments
 (0)