Kaydet (Commit) 801844d6 authored tarafından Benjamin Peterson's avatar Benjamin Peterson

#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
üst fbe94c55
This diff is collapsed.
......@@ -210,19 +210,6 @@ class NullTranslations:
else:
return msgid2
def ugettext(self, message):
if self._fallback:
return self._fallback.ugettext(message)
return str(message)
def ungettext(self, msgid1, msgid2, n):
if self._fallback:
return self._fallback.ungettext(msgid1, msgid2, n)
if n == 1:
return str(msgid1)
else:
return str(msgid2)
def info(self):
return self._info
......@@ -235,15 +222,14 @@ class NullTranslations:
def set_output_charset(self, charset):
self._output_charset = charset
def install(self, str=False, names=None):
def install(self, names=None):
import builtins
builtins.__dict__['_'] = str and self.ugettext or self.gettext
builtins.__dict__['_'] = self.gettext
if hasattr(names, "__contains__"):
if "gettext" in names:
builtins.__dict__['gettext'] = builtins.__dict__['_']
if "ngettext" in names:
builtins.__dict__['ngettext'] = (str and self.ungettext
or self.ngettext)
builtins.__dict__['ngettext'] = self.ngettext
if "lgettext" in names:
builtins.__dict__['lgettext'] = self.lgettext
if "lngettext" in names:
......@@ -367,31 +353,27 @@ class GNUTranslations(NullTranslations):
else:
return msgid2
def ugettext(self, message):
def gettext(self, message):
missing = object()
tmsg = self._catalog.get(message, missing)
if tmsg is missing:
if self._fallback:
return self._fallback.ugettext(message)
return self._fallback.gettext(message)
return str(message)
return tmsg
gettext = ugettext
def ungettext(self, msgid1, msgid2, n):
def ngettext(self, msgid1, msgid2, n):
try:
tmsg = self._catalog[(msgid1, self.plural(n))]
except KeyError:
if self._fallback:
return self._fallback.ungettext(msgid1, msgid2, n)
return self._fallback.ngettext(msgid1, msgid2, n)
if n == 1:
tmsg = str(msgid1)
else:
tmsg = str(msgid2)
return tmsg
ngettext = ungettext
# Locate a .mo file using the gettext strategy
def find(domain, localedir=None, languages=None, all=0):
......@@ -465,9 +447,9 @@ def translation(domain, localedir=None, languages=None,
return result
def install(domain, localedir=None, str=False, codeset=None, names=None):
def install(domain, localedir=None, codeset=None, names=None):
t = translation(domain, localedir, fallback=True, codeset=codeset)
t.install(str, names)
t.install(names)
......
......@@ -143,13 +143,13 @@ trggrkg zrffntr pngnybt yvoenel.''')
t.install()
eq(_('nudge nudge'), 'wink wink')
# Try unicode return type
t.install(str=True)
t.install()
eq(_('mullusk'), 'bacon')
# Test installation of other methods
import builtins
t.install(str=True, names=["gettext", "lgettext"])
eq(_, t.ugettext)
eq(builtins.gettext, t.ugettext)
t.install(names=["gettext", "lgettext"])
eq(_, t.gettext)
eq(builtins.gettext, t.gettext)
eq(lgettext, t.lgettext)
del builtins.gettext
del builtins.lgettext
......@@ -305,7 +305,7 @@ class UnicodeTranslationsTest(GettextBaseTest):
self.t = gettext.GNUTranslations(fp)
finally:
fp.close()
self._ = self.t.ugettext
self._ = self.t.gettext
def test_unicode_msgid(self):
unless = self.failUnless
......
......@@ -32,6 +32,9 @@ Library
code of every single module of the standard library, including invalid files
used in the test suite.
- All the u* variant functions and methods in gettext have been renamed to their
none u* siblings.
C API
-----
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment