commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r7152 - trunk/gnue-common/src/apps


From: reinhard
Subject: [gnue] r7152 - trunk/gnue-common/src/apps
Date: Thu, 10 Mar 2005 12:09:13 -0600 (CST)

Author: reinhard
Date: 2005-03-10 12:09:13 -0600 (Thu, 10 Mar 2005)
New Revision: 7152

Modified:
   trunk/gnue-common/src/apps/i18n.py
Log:
Code cleanup. Fix for setting a user defined locale.


Modified: trunk/gnue-common/src/apps/i18n.py
===================================================================
--- trunk/gnue-common/src/apps/i18n.py  2005-03-10 16:40:44 UTC (rev 7151)
+++ trunk/gnue-common/src/apps/i18n.py  2005-03-10 18:09:13 UTC (rev 7152)
@@ -24,8 +24,8 @@
 """
 Internationalization support.
 
address@hidden language: Preferred language of the user running the program.
address@hidden encoding: Encoding to be used for console input and output.
address@hidden language: Language of the current locale
address@hidden encoding: Encoding of the current locale
 """
 
 from types import *
@@ -45,14 +45,28 @@
 # -----------------------------------------------------------------------------
 
 __modules = {}                          # Modules by filename
-__catalogs = {}                         # Message catalogs by domain
-__userlanguage = None
+__catalogs = {}                         # Message catalogs by domain:language
 
 language = None
 encoding = None
 
 
 # -----------------------------------------------------------------------------
+# Convert Unicode to String, let everything else untouched. This is o().
+# -----------------------------------------------------------------------------
+
+def outconv (message):
+  """
+  Encodes a message to L{encoding} (the current locale's encoding).  This
+  function is available as the builtin function "o()".
+  """
+  if isinstance (message, UnicodeType):
+    return message.encode (encoding, 'replace')
+  else:
+    return message
+
+
+# -----------------------------------------------------------------------------
 # Find a module from filename
 # -----------------------------------------------------------------------------
 
@@ -90,19 +104,16 @@
   i = string.find (x, '.')
   domain = x [:i]
 
-  if __catalogs.has_key (domain):
-    return __catalogs [domain]
+  if __catalogs.has_key (domain + ':' + language):
+    return __catalogs [domain + ':' + language]
   else:
     try:
-      if os.name == 'posix':
-        catalog = gettext.translation (domain, paths.data + '/share/locale')
-      else:
-        catalog = gettext.translation (domain, paths.data + '/share/locale',
-                                       [language])
+      catalog = gettext.translation (domain, paths.data + '/share/locale',
+                                     [language])
     except:
       catalog = None
 
-    __catalogs [domain] = catalog
+    __catalogs [domain + ':' + language] = catalog
 
     return catalog
 
@@ -134,30 +145,10 @@
   (the current locale's encoding).  This function is available as the builtin
   function "_()".
   """
-  catalog = __find_catalog ()
+  return outconv (utranslate (message))
 
-  if catalog is None:
-    return message
 
-  return (catalog.ugettext (message)).encode (encoding, 'replace')
-
-
 # -----------------------------------------------------------------------------
-# Convert Unicode to String, let everything else untouched. This is o().
-# -----------------------------------------------------------------------------
-
-def outconv (message):
-  """
-  Encodes a message to L{encoding} (the current locale's encoding).  This
-  function is available as the builtin function "o()".
-  """
-  if isinstance (message, UnicodeType):
-    return message.encode (encoding, 'replace')
-  else:
-    return message
-
-
-# -----------------------------------------------------------------------------
 # Get the current language
 # -----------------------------------------------------------------------------
 
@@ -168,7 +159,7 @@
 
   @return: language of the current locale.
   """
-  return __userlanguage or 'C'
+  return language
 
 
 # -----------------------------------------------------------------------------
@@ -182,14 +173,7 @@
 
   @return: encoding of the current locale.
   """
-  # For Windows, getlocale () is broken - it returns e.g. ('Hungarian_Hungary',
-  # '1250') instead of ('hu_HU', 'cp1250'). So we have to use getdefaultlocale.
-  # However, this means that under Windows, no locales but the default locale
-  # can be used.
-  if sys.platform == 'win32':
-    return locale.getdefaultlocale () [1] or 'ascii'
-  else:
-    return locale.getlocale () [1] or 'ascii'
+  return encoding
 
 
 # -----------------------------------------------------------------------------
@@ -226,6 +210,28 @@
 
 
 # -----------------------------------------------------------------------------
+# Update global variables
+# -----------------------------------------------------------------------------
+
+def __updateglobals ():
+
+  global language, encoding
+
+  # On win32, getlocale is broken - it returns strings like Hungarian_Hungary
+  # instead of hu_HU.
+  if sys.platform == 'win32':
+    (language, encoding) = locale.getdefaultlocale ()
+  else:
+    (language, encoding) = locale.getlocale ()
+
+  # Make sure language and encoding are not None
+  if not language:
+    language = 'C'
+  if not encoding:
+    encoding = 'ascii'
+
+
+# -----------------------------------------------------------------------------
 # Change the current locale
 # -----------------------------------------------------------------------------
 
@@ -240,26 +246,28 @@
   @param newLocale: string of the locale to be set, e.g. address@hidden
     (full blown) or 'de_AT' or 'en_AU'
   """
+  # Setting a locale different than the current locale doesn't work on Windows.
+  if sys.platform == 'win32':
+    return
+
   if newLocale is None:
     newLocale = ''
 
+  newLocale = newLocale.encode ()
+
   parts  = []
   add    = []
-  normal = locale.normalize (newLocale)
-  next   = normal.split ('@') [0]
-  __userlanguage = next.split ('.') [0]
+  normal = locale.normalize (newLocale)         # address@hidden
+  next   = normal.split ('@') [0]               # lc_CC.ENCODING
+  lang   = next.split ('.') [0]                 # lc_CC
 
-  # Setting a locale different than the current locale doesn't work on Windows.
-  if sys.platform == 'win32':
-    return
-
-  alias  = locale.locale_alias.get (__userlanguage.split ('_') [0].lower ())
+  alias  = locale.locale_alias.get (lang.split ('_') [0].lower ())
   if alias:
     add = [alias.split ('@') [0]]
     add.append (add [-1].split ('.') [0])
     add.append (locale.locale_alias.get (add [-1].split ('_') [0].lower ()))
 
-  for item in [normal, next, __userlanguage] + add:
+  for item in [normal, next, lang] + add:
     if item is not None and item not in parts:
       parts.append (item)
 
@@ -273,7 +281,9 @@
     else:
       break
 
+  __updateglobals ()
 
+
 # -----------------------------------------------------------------------------
 # Module initialization
 # -----------------------------------------------------------------------------
@@ -284,20 +294,10 @@
 except:
   pass
 
-# On win32, getlocale is broken - it returns Hungarian_Hungary instead of
-# hu_HU. On the other hand, getdefaultlocale is broken when $LANGUAGE contains
-# a ":" separated list of locales, like it does for Debian...
-if sys.platform == 'win32':
-  (language, encoding) = locale.getdefaultlocale ()
-else:
-  (language, encoding) = locale.getlocale ()
+__updateglobals ()
 
-# Make sure encoding is not None
-if not encoding:
-  encoding = 'ascii'
-
 # Now define the new builtin stuff
 import __builtin__
+__builtin__.__dict__['o'] = outconv
 __builtin__.__dict__['u_'] = utranslate
 __builtin__.__dict__['_'] = translate
-__builtin__.__dict__['o'] = outconv





reply via email to

[Prev in Thread] Current Thread [Next in Thread]