[Python-checkins] python/dist/src/Lib gettext.py,1.23,1.24

niemeyer at users.sourceforge.net niemeyer at users.sourceforge.net
Thu Jul 22 20:44:02 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3065/Lib

Modified Files:
	gettext.py 
Log Message:
This change implements the following gettext features, as 
discussed recently in python-dev: 

In _locale module: 

- bind_textdomain_codeset() binding 

In gettext module: 

- bind_textdomain_codeset() function 
- lgettext(), lngettext(), ldgettext(), ldngettext(), 
  which return translated strings encoded in 
  preferred system encoding, if 
  bind_textdomain_codeset() was not used. 
- Added equivalent functionality in translate()
  function and catalog classes. 

Every change was also documented.


Index: gettext.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/gettext.py,v
retrieving revision 1.23
retrieving revision 1.24
diff -C2 -d -r1.23 -r1.24
*** gettext.py	4 May 2004 09:21:43 -0000	1.23
--- gettext.py	22 Jul 2004 18:44:00 -0000	1.24
***************
*** 47,51 ****
  
  
! import copy, os, re, struct, sys
  from errno import ENOENT
  
--- 47,51 ----
  
  
! import locale, copy, os, re, struct, sys
  from errno import ENOENT
  
***************
*** 172,175 ****
--- 172,176 ----
          self._info = {}
          self._charset = None
+         self._output_charset = None
          self._fallback = None
          if fp is not None:
***************
*** 190,193 ****
--- 191,199 ----
          return message
  
+     def lgettext(self, message):
+         if self._fallback:
+             return self._fallback.lgettext(message)
+         return message
+ 
      def ngettext(self, msgid1, msgid2, n):
          if self._fallback:
***************
*** 198,201 ****
--- 204,215 ----
              return msgid2
  
+     def lngettext(self, msgid1, msgid2, n):
+         if self._fallback:
+             return self._fallback.lngettext(msgid1, msgid2, n)
+         if n == 1:
+             return msgid1
+         else:
+             return msgid2
+ 
      def ugettext(self, message):
          if self._fallback:
***************
*** 217,220 ****
--- 231,240 ----
          return self._charset
  
+     def output_charset(self):
+         return self._output_charset
+ 
+     def set_output_charset(self, charset):
+         self._output_charset = charset
+ 
      def install(self, unicode=False):
          import __builtin__
***************
*** 316,327 ****
              return message
          # Encode the Unicode tmsg back to an 8-bit string, if possible
!         if self._charset:
              return tmsg.encode(self._charset)
          return tmsg
  
      def ngettext(self, msgid1, msgid2, n):
          try:
              tmsg = self._catalog[(msgid1, self.plural(n))]
!             if self._charset:
                  return tmsg.encode(self._charset)
              return tmsg
--- 336,362 ----
              return message
          # Encode the Unicode tmsg back to an 8-bit string, if possible
!         if self._output_charset:
!             return tmsg.encode(self._output_charset)
!         elif self._charset:
              return tmsg.encode(self._charset)
          return tmsg
  
+     def lgettext(self, message):
+         missing = object()
+         tmsg = self._catalog.get(message, missing)
+         if tmsg is missing:
+             if self._fallback:
+                 return self._fallback.lgettext(message)
+             return message
+         if self._output_charset:
+             return tmsg.encode(self._output_charset)
+         return tmsg.encode(locale.getpreferredencoding())
+ 
      def ngettext(self, msgid1, msgid2, n):
          try:
              tmsg = self._catalog[(msgid1, self.plural(n))]
!             if self._output_charset:
!                 return tmsg.encode(self._output_charset)
!             elif self._charset:
                  return tmsg.encode(self._charset)
              return tmsg
***************
*** 334,337 ****
--- 369,386 ----
                  return msgid2
  
+     def lngettext(self, msgid1, msgid2, n):
+         try:
+             tmsg = self._catalog[(msgid1, self.plural(n))]
+             if self._output_charset:
+                 return tmsg.encode(self._output_charset)
+             return tmsg.encode(locale.getpreferredencoding())
+         except KeyError:
+             if self._fallback:
+                 return self._fallback.lngettext(msgid1, msgid2, n)
+             if n == 1:
+                 return msgid1
+             else:
+                 return msgid2
+ 
      def ugettext(self, message):
          missing = object()
***************
*** 398,402 ****
  
  def translation(domain, localedir=None, languages=None,
!                 class_=None, fallback=False):
      if class_ is None:
          class_ = GNUTranslations
--- 447,451 ----
  
  def translation(domain, localedir=None, languages=None,
!                 class_=None, fallback=False, codeset=None):
      if class_ is None:
          class_ = GNUTranslations
***************
*** 415,421 ****
          if t is None:
              t = _translations.setdefault(key, class_(open(mofile, 'rb')))
!         # Copy the translation object to allow setting fallbacks.
!         # All other instance data is shared with the cached object.
          t = copy.copy(t)
          if result is None:
              result = t
--- 464,473 ----
          if t is None:
              t = _translations.setdefault(key, class_(open(mofile, 'rb')))
!         # Copy the translation object to allow setting fallbacks and
!         # output charset. All other instance data is shared with the
!         # cached object.
          t = copy.copy(t)
+         if codeset:
+             t.set_output_charset(codeset)
          if result is None:
              result = t
***************
*** 425,430 ****
  
  
! def install(domain, localedir=None, unicode=False):
!     translation(domain, localedir, fallback=True).install(unicode)
  
  
--- 477,483 ----
  
  
! def install(domain, localedir=None, unicode=False, codeset=None):
!     t = translation(domain, localedir, fallback=True, codeset=codeset)
!     t.install(unicode)
  
  
***************
*** 432,435 ****
--- 485,490 ----
  # a mapping b/w domains and locale directories
  _localedirs = {}
+ # a mapping b/w domains and codesets
+ _localecodesets = {}
  # current global domain, `messages' used for compatibility w/ GNU gettext
  _current_domain = 'messages'
***************
*** 450,464 ****
  
  
  def dgettext(domain, message):
      try:
!         t = translation(domain, _localedirs.get(domain, None))
      except IOError:
          return message
      return t.gettext(message)
  
  
  def dngettext(domain, msgid1, msgid2, n):
      try:
!         t = translation(domain, _localedirs.get(domain, None))
      except IOError:
          if n == 1:
--- 505,535 ----
  
  
+ def bind_textdomain_codeset(domain, codeset=None):
+     global _localecodesets
+     if codeset is not None:
+         _localecodesets[domain] = codeset
+     return _localecodesets.get(domain)
+ 
+ 
  def dgettext(domain, message):
      try:
!         t = translation(domain, _localedirs.get(domain, None),
!                         codeset=_localecodesets.get(domain))
      except IOError:
          return message
      return t.gettext(message)
  
+ def ldgettext(domain, message):
+     try:
+         t = translation(domain, _localedirs.get(domain, None),
+                         codeset=_localecodesets.get(domain))
+     except IOError:
+         return message
+     return t.lgettext(message)
  
  def dngettext(domain, msgid1, msgid2, n):
      try:
!         t = translation(domain, _localedirs.get(domain, None),
!                         codeset=_localecodesets.get(domain))
      except IOError:
          if n == 1:
***************
*** 468,479 ****
--- 539,564 ----
      return t.ngettext(msgid1, msgid2, n)
  
+ def ldngettext(domain, msgid1, msgid2, n):
+     try:
+         t = translation(domain, _localedirs.get(domain, None),
+                         codeset=_localecodesets.get(domain))
+     except IOError:
+         if n == 1:
+             return msgid1
+         else:
+             return msgid2
+     return t.lngettext(msgid1, msgid2, n)
  
  def gettext(message):
      return dgettext(_current_domain, message)
  
+ def lgettext(message):
+     return ldgettext(_current_domain, message)
  
  def ngettext(msgid1, msgid2, n):
      return dngettext(_current_domain, msgid1, msgid2, n)
  
+ def lngettext(msgid1, msgid2, n):
+     return ldngettext(_current_domain, msgid1, msgid2, n)
  
  # dcgettext() has been deemed unnecessary and is not implemented.



More information about the Python-checkins mailing list