Index: pykota/trunk/pykota/dumper.py
===================================================================
--- pykota/trunk/pykota/dumper.py (revision 3291)
+++ pykota/trunk/pykota/dumper.py (revision 3295)
@@ -39,6 +39,8 @@
     hasJAXML = True
 
+from pykota.utils import *
+
 from pykota import version
-from pykota.tool import PyKotaTool, N_
+from pykota.tool import PyKotaTool
 from pykota.errors import PyKotaToolError, PyKotaCommandLineError
 
Index: pykota/trunk/pykota/utils.py
===================================================================
--- pykota/trunk/pykota/utils.py (revision 3294)
+++ pykota/trunk/pykota/utils.py (revision 3295)
@@ -23,4 +23,5 @@
 
 import sys
+import os
 import locale
 import gettext
@@ -63,4 +64,24 @@
     except :
         gettext.NullTranslations().install(unicode=True)
+        
+def getpreferredlanguage() :
+    """Returns the preferred language."""
+    languages = os.environ.get("HTTP_ACCEPT_LANGUAGE", "")
+    langs = [l.strip().split(';')[0] for l in languages.split(",")]
+    return langs[0].replace("-", "_")
+    
+def getpreferredcharset() :
+    """Returns the preferred charset."""
+    charsets = os.environ.get("HTTP_ACCEPT_CHARSET", "UTF-8")
+    charsets = [l.strip().split(';')[0] for l in charsets.split(",")]
+    return charsets[0]
+
+def reinitcgilocale() :        
+    """Reinitializes the locale and gettext translations for CGI scripts, according to browser's preferences."""
+    initgettext(*initlocale(getpreferredlanguage(), getpreferredcharset()))
+    
+def N_(message) :
+    """Fake translation marker for translatable strings extraction."""
+    return message
 
 def databaseToUnicode(text) :
@@ -80,4 +101,16 @@
 def logerr(text) :
     """Logs an unicode text to stderr."""
-    sys.stderr.write(text.encode(sys.stdout.encoding, "replace"))
+    sys.stderr.write(text.encode(sys.stdout.encoding or locale.getlocale()[1], \
+                                 "replace"))
+    sys.stderr.flush()
             
+def crashed(message="Bug in PyKota") :    
+    """Minimal crash method."""
+    import traceback
+    from pykota.version import __version__
+    lines = []
+    for line in traceback.format_exception(*sys.exc_info()) :
+        lines.extend([l for l in line.split("\n") if l])
+    msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKota v%s" % __version__, message] + lines)])
+    logerr(msg)
+    return msg
Index: pykota/trunk/pykota/tool.py
===================================================================
--- pykota/trunk/pykota/tool.py (revision 3294)
+++ pykota/trunk/pykota/tool.py (revision 3295)
@@ -38,22 +38,8 @@
 from mx import DateTime
 
+from pykota import utils
 from pykota.errors import PyKotaCommandLineError
 from pykota import config, storage, logger
 from pykota.version import __version__, __author__, __years__, __gplblurb__
-
-def N_(message) :
-    """Fake translation marker for translatable strings extraction."""
-    return message
-
-def crashed(message="Bug in PyKota") :    
-    """Minimal crash method."""
-    import traceback
-    lines = []
-    for line in traceback.format_exception(*sys.exc_info()) :
-        lines.extend([l for l in line.split("\n") if l])
-    msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKota v%s" % __version__, message] + lines)])
-    sys.stderr.write(msg)
-    sys.stderr.flush()
-    return msg
 
 class Percent :
@@ -103,5 +89,5 @@
 class Tool :
     """Base class for tools with no database access."""
-    def __init__(self, lang="", charset=None, doc="PyKota v%(__version__)s (c) %(__years__)s %(__author__)s") :
+    def __init__(self, doc="PyKota v%(__version__)s (c) %(__years__)s %(__author__)s") :
         """Initializes the command line tool."""
         self.debug = True # in case of early failure
@@ -110,4 +96,6 @@
         # Saves a copy of the locale settings
         (self.language, self.charset) = locale.getlocale()
+        if not self.charset :
+            self.charset = "UTF-8"
         
         # pykota specific stuff
@@ -247,5 +235,5 @@
     def crashed(self, message="Bug in PyKota") :    
         """Outputs a crash message, and optionally sends it to software author."""
-        msg = crashed(message)
+        msg = utils.crashed(message)
         fullmessage = "========== Traceback :\n\n%s\n\n========== sys.argv :\n\n%s\n\n========== Environment :\n\n%s\n" % \
                         (msg, \
@@ -347,8 +335,4 @@
 class PyKotaTool(Tool) :    
     """Base class for all PyKota command line tools."""
-    def __init__(self, lang="", charset=None, doc="PyKota v%(__version__)s (c) %(__years__)s %(__author__)s") :
-        """Initializes the command line tool and opens the database."""
-        Tool.__init__(self, lang, charset, doc)
-        
     def deferredInit(self) :    
         """Deferred initialization."""
Index: pykota/trunk/pykota/cgifuncs.py
===================================================================
--- pykota/trunk/pykota/cgifuncs.py (revision 3275)
+++  (revision )
@@ -1,40 +1,0 @@
-# -*- coding: UTF-8 -*-
-#
-# PyKota : Print Quotas for CUPS
-#
-# (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com>
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-# 
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-# 
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-#
-# $Id$
-#
-#
-
-"""This module defines some helper functions to use in CGI scripts."""
-
-import sys
-import os
-
-def getLanguagePreference() :
-    """Returns the preferred language."""
-    languages = os.environ.get("HTTP_ACCEPT_LANGUAGE", "")
-    langs = [l.strip().split(';')[0] for l in languages.split(",")]
-    #sys.stderr.write("Languages preferences : %s\n" % langs)
-    return langs[0].replace("-", "_")
-    
-def getCharsetPreference() :
-    """Returns the preferred charset."""
-    charsets = os.environ.get("HTTP_ACCEPT_CHARSET", "UTF-8")
-    charsets = [l.strip().split(';')[0] for l in charsets.split(",")]
-    #sys.stderr.write("Charsets preferences : %s\n" % charsets)
-    return charsets[0]
