[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
r5437 - trunk/gnue-common/src/apps
From: |
reinhard |
Subject: |
r5437 - trunk/gnue-common/src/apps |
Date: |
Sun, 21 Mar 2004 12:16:51 -0600 (CST) |
Author: reinhard
Date: 2004-03-21 12:16:50 -0600 (Sun, 21 Mar 2004)
New Revision: 5437
Modified:
trunk/gnue-common/src/apps/i18n.py
Log:
Removed second parameter from _(), introduced the u_() function and the
uException class.
Modified: trunk/gnue-common/src/apps/i18n.py
===================================================================
--- trunk/gnue-common/src/apps/i18n.py 2004-03-21 18:15:32 UTC (rev 5436)
+++ trunk/gnue-common/src/apps/i18n.py 2004-03-21 18:16:50 UTC (rev 5437)
@@ -23,11 +23,12 @@
from types import *
-import sys
-import string
-import os.path
+import exceptions
import gettext
import locale
+import os.path
+import string
+import sys
from gnue import paths
@@ -35,20 +36,20 @@
# Global variables
# -----------------------------------------------------------------------------
-modules = {} # Modules by filename
-catalogs = {} # Message catalogs by domain
+__modules = {} # Modules by filename
+__catalogs = {} # Message catalogs by domain
+
language = None
encoding = None
enc_policy = "replace" # policy to use if an unicode character
- # can't be translated to encoding
# -----------------------------------------------------------------------------
# Find a module from filename
# -----------------------------------------------------------------------------
-def _find_module (filename):
- if modules.has_key (filename):
- return modules [filename]
+def __find_module (filename):
+ if __modules.has_key (filename):
+ return __modules [filename]
for mod in sys.modules.values ():
if hasattr (mod, '__file__'):
# mod.__file__ can be .pyc if the module is already compiled
@@ -56,31 +57,31 @@
if f [-1:] == 'c':
f = f [:-1]
if os.path.normcase (f) == os.path.normcase (filename):
- modules [filename] = mod
+ __modules [filename] = mod
return mod
# -----------------------------------------------------------------------------
-# Translate a message. This is the actual implementation of _()
+# Find the correct translation catalog
# -----------------------------------------------------------------------------
-def translate (msg, unicode = False):
+def __find_catalog ():
# find out the filename of the calling function
- caller_file = (sys._getframe (1)).f_code.co_filename
+ caller_file = (sys._getframe (2)).f_code.co_filename
# find out the module name
- caller_module = _find_module (caller_file)
+ caller_module = __find_module (caller_file)
if caller_module is None:
- return msg
+ return None
# make 'gnue-common' from 'gnue.common.foo.bar'
x = string.replace (caller_module.__name__, '.', '-', 1)
i = string.find (x, '.')
domain = x [:i]
- if catalogs.has_key (domain):
- catalog = catalogs [domain]
+ if __catalogs.has_key (domain):
+ return __catalogs [domain]
else:
try:
if os.name == 'posix':
@@ -91,30 +92,72 @@
except:
catalog = None
- catalogs [domain] = catalog
+ __catalogs [domain] = catalog
+ return catalog
+
+# -----------------------------------------------------------------------------
+# Translate a message and return unicode
+# -----------------------------------------------------------------------------
+
+def utranslate (message):
+ """
+ Translates a message and returns a unicode string. This function is
+ available as the builtin function "u_()".
+ """
+ catalog = __find_catalog ()
+
if catalog is None:
- return msg
+ return message
- # catalog.gettext (msg) returns a string in the po-file's encoding. We want
- # the user's encoding, so we have to convert.
- # FIXME: Actually, this function should return unicode values.
- if unicode:
- return catalog.ugettext (msg)
- else:
- return (catalog.ugettext (msg)).encode (encoding, enc_policy)
+ return catalog.ugettext (message)
# -----------------------------------------------------------------------------
+# Translate a message and return local encoding
+# -----------------------------------------------------------------------------
+
+def translate (message):
+ """
+ Translates a message and returns an 8 bit string, encoded with @encoding (the
+ current locale's encoding). This function is available as the builtin
+ function "_()".
+ """
+ catalog = __find_catalog ()
+
+ if catalog is None:
+ return message
+
+ return (catalog.ugettext (message)).encode (encoding, enc_policy)
+
+# -----------------------------------------------------------------------------
# Convert Unicode to String, let everything else untouched. This is o().
# -----------------------------------------------------------------------------
-def outconv (msg):
- if isinstance (msg, UnicodeType):
- return msg.encode (encoding, enc_policy)
+def outconv (message):
+ """
+ Encodes a message to @encoding (the current locale's encoding). This
+ function is available as the builtin function "o()".
+ """
+ if isinstance (message, UnicodeType):
+ return message.encode (encoding, enc_policy)
else:
- return msg
+ return message
# -----------------------------------------------------------------------------
+# New basic exception class. Python's standard exception class cannot handle
+# unicode messages.
+# -----------------------------------------------------------------------------
+
+class uException (Exception):
+ """
+ The same as the builtin python Exception, but can handle messages that are
+ unicode strings. This exception is available as the builtin class
+ "Exception" (and thus overwrites the standard python exception).
+ """
+ def __init__ (self, message):
+ exceptions.Exception.__init__ (self, o(message))
+
+# -----------------------------------------------------------------------------
# Module initialization
# -----------------------------------------------------------------------------
@@ -127,7 +170,9 @@
# This will give us the user's language and encoding even on non-POSIX systems
(language, encoding) = locale.getdefaultlocale ()
-# Now define the _() and o() functions
+# Now define the new builtin stuff
import __builtin__
+__builtin__.__dict__['u_'] = utranslate
__builtin__.__dict__['_'] = translate
__builtin__.__dict__['o'] = outconv
+__builtin__.__dict__['Exception'] = uException
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- r5437 - trunk/gnue-common/src/apps,
reinhard <=