[Python-checkins] CVS: python/dist/src/Lib gettext.py,1.12,1.13

Martin v. L?wis loewis@users.sourceforge.net
Thu, 10 Jan 2002 22:58:51 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv13259/Lib

Modified Files:
	gettext.py 
Log Message:
Add a per-message fallback mechanism for translations.


Index: gettext.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/gettext.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** gettext.py	2002/01/11 06:33:28	1.12
--- gettext.py	2002/01/11 06:58:49	1.13
***************
*** 47,50 ****
--- 47,51 ----
  import sys
  import struct
+ import copy
  from errno import ENOENT
  
***************
*** 103,106 ****
--- 104,108 ----
          self._info = {}
          self._charset = None
+         self._fallback = None
          if fp:
              self._parse(fp)
***************
*** 109,116 ****
--- 111,128 ----
          pass
  
+     def add_fallback(self, fallback):
+         if self._fallback:
+             self._fallback.add_fallback(fallback)
+         else:
+             self._fallback = fallback
+ 
      def gettext(self, message):
+         if self._fallback:
+             return self._fallback.gettext(message)
          return message
  
      def ugettext(self, message):
+         if self._fallback:
+             return self._fallback.ugettext(message)
          return unicode(message)
  
***************
*** 189,196 ****
  
      def gettext(self, message):
!         return self._catalog.get(message, message)
  
      def ugettext(self, message):
!         tmsg = self._catalog.get(message, message)
          return unicode(tmsg, self._charset)
  
--- 201,218 ----
  
      def gettext(self, message):
!         try:
!             return self._catalog[message]
!         except KeyError:
!             if self._fallback:
!                 return self._fallback.gettext(message)
!             return message
  
      def ugettext(self, message):
!         try:
!             tmsg = self._catalog[message]
!         except KeyError:
!             if self._fallback:
!                 return self._fallback.ugettext(message)
!             tmsg = message
          return unicode(tmsg, self._charset)
  
***************
*** 198,202 ****
  
  # Locate a .mo file using the gettext strategy
! def find(domain, localedir=None, languages=None):
      # Get some reasonable defaults for arguments that were not supplied
      if localedir is None:
--- 220,224 ----
  
  # Locate a .mo file using the gettext strategy
! def find(domain, localedir=None, languages=None, all=0):
      # Get some reasonable defaults for arguments that were not supplied
      if localedir is None:
***************
*** 218,221 ****
--- 240,247 ----
                  nelangs.append(nelang)
      # select a language
+     if all:
+         result = []
+     else:
+         result = None
      for lang in nelangs:
          if lang == 'C':
***************
*** 223,228 ****
          mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
          if os.path.exists(mofile):
!             return mofile
!     return None
  
  
--- 249,257 ----
          mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
          if os.path.exists(mofile):
!             if all:
!                 result.append(mofile)
!             else:
!                 return mofile
!     return result
  
  
***************
*** 235,252 ****
      if class_ is None:
          class_ = GNUTranslations
!     mofile = find(domain, localedir, languages)
!     if mofile is None:
          if fallback:
              return NullTranslations()
          raise IOError(ENOENT, 'No translation file found for domain', domain)
-     key = os.path.abspath(mofile)
      # TBD: do we need to worry about the file pointer getting collected?
      # Avoid opening, reading, and parsing the .mo file after it's been done
      # once.
!     t = _translations.get(key)
!     if t is None:
!         t = _translations.setdefault(key, class_(open(mofile, 'rb')))
!     return t
! 
  
  
--- 264,289 ----
      if class_ is None:
          class_ = GNUTranslations
!     mofiles = find(domain, localedir, languages, all=1)
!     if len(mofiles)==0:
          if fallback:
              return NullTranslations()
          raise IOError(ENOENT, 'No translation file found for domain', domain)
      # TBD: do we need to worry about the file pointer getting collected?
      # Avoid opening, reading, and parsing the .mo file after it's been done
      # once.
!     result = None
!     for mofile in mofiles:
!         key = os.path.abspath(mofile)
!         t = _translations.get(key)
!         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
!         else:
!             result.add_fallback(t)
!     return result