src/modules/misc.py
changeset 791 123e9401e939
parent 779 35b869ed3334
child 814 76909c2cad8f
--- a/src/modules/misc.py	Thu Dec 18 13:08:47 2008 -0600
+++ b/src/modules/misc.py	Thu Dec 18 11:15:47 2008 -0800
@@ -38,6 +38,7 @@
 import time
 import calendar
 import shutil
+import locale
 from stat import *
 
 import pkg.urlhelpers as urlhelpers
@@ -300,6 +301,26 @@
                         raise PipeError, e
                 raise
 
+def setlocale(category, loc=None, printer=None):
+        """Wraps locale.setlocale(), falling back to the C locale if the desired
+        locale is broken or unavailable.  The 'printer' parameter should be a
+        function which takes a string and displays it.  If 'None' (the default),
+        setlocale() will print the message to stderr."""
+
+        if printer is None:
+                printer = emsg
+
+        try:
+                locale.setlocale(category, loc)
+        except locale.Error:
+                try:
+                        dl = " '%s.%s'" % locale.getdefaultlocale()
+                except ValueError:
+                        dl = ""
+                printer("Unable to set locale%s; locale package may be broken "
+                    "or\nnot installed.  Reverting to C locale." % dl)
+                locale.setlocale(category, "C")
+
 def port_available(host, port):
         """Returns True if the indicated port is available to bind to;
         otherwise returns False."""