[GRASS-SVN] r59652 - grass/trunk/lib/init

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Apr 9 02:26:24 PDT 2014


Author: marisn
Date: 2014-04-09 02:26:24 -0700 (Wed, 09 Apr 2014)
New Revision: 59652

Modified:
   grass/trunk/lib/init/grass.py
Log:
Even more robust locale handling. Do not fail if no locale has been detected.

Modified: grass/trunk/lib/init/grass.py
===================================================================
--- grass/trunk/lib/init/grass.py	2014-04-09 06:34:15 UTC (rev 59651)
+++ grass/trunk/lib/init/grass.py	2014-04-09 09:26:24 UTC (rev 59652)
@@ -768,6 +768,7 @@
     import locale
     
     language = 'None' # Such string sometimes is present in wx file
+    encoding = None
     
     # Override value is stored in wxGUI preferences file.
     # As it's the only thing required, we'll just grep it out.
@@ -779,7 +780,7 @@
     else:
         for line in fd:
             if re.search('^language', line):
-                line = line.rstrip('%s' % os.linesep)
+                line = line.rstrip(' %s' % os.linesep)
                 language = ''.join(line.split(';')[-1:])
                 break
         fd.close()
@@ -789,50 +790,54 @@
         # As by default program runs with C locale, but users expect to
         # have their default locale, we'll just set default locale
         locale.setlocale(locale.LC_ALL, '')
-        if windows and os.getenv('LANG') is None:
-            language, encoding = locale.getdefaultlocale()
-            os.environ['LANG'] = language
-            gettext.install('grasslibs', os.path.join(gisbase, 'locale'), codeset=encoding)
-        else:
-            gettext.install('grasslibs', os.path.join(gisbase, 'locale'), unicode=True)
-        return
+        language, encoding = locale.getdefaultlocale()
+        if not language:
+            warning("Default locale settings are missing. GRASS running with C locale.")
+            return
     
-    warning("A language override has been requested. Trying to switch GRASS into '%s'..." % language)
-    
-    # Even if setting locale will fail, let's set LANG in a hope,
-    # that UI will use it GRASS texts will be in selected language,
-    # system messages (i.e. OK, Cancel etc.) - in system default
-    # language
-    os.environ['LANGUAGE'] = language
-    os.environ['LANG'] = language
-    
-    try:
-        locale.setlocale(locale.LC_ALL, language)
-    except locale.Error, e:
+    else:
+        warning("A language override has been requested. Trying to switch GRASS into '%s'..." % language)
+        
         try:
-            # Locale lang.encoding might be missing. Let's try
-            # UTF-8 encoding before giving up as on Linux systems
-            # lang.UTF-8 locales are more common than legacy
-            # ISO-8859 ones.
-            language = locale.normalize('%s.UTF-8' % (language))
             locale.setlocale(locale.LC_ALL, language)
         except locale.Error, e:
-            # The last attempt...
             try:
-                language = locale.normalize('%s.%s' % (language, locale.getpreferredencoding()))
-                locale.setlocale(locale.LC_ALL, language)
+                # Locale lang.encoding might be missing. Let's try
+                # UTF-8 encoding before giving up as on Linux systems
+                # lang.UTF-8 locales are more common than legacy
+                # ISO-8859 ones.
+                encoding = 'UTF-8'
+                normalized = locale.normalize('%s.%s' % (language, encoding))
+                locale.setlocale(locale.LC_ALL, normalized)
             except locale.Error, e:
-                # If we got so far, attempts to set up language and locale have failed
-                # on this system
-                sys.stderr.write("Failed to enforce user specified language '%s' with error: '%s'\n" % (language, e))
-                sys.stderr.write("A LANGUAGE environmental variable has been set.\nPart of messages will be displayed in the requested language.\n")
-                return
-
+                # The last attempt...
+                try:
+                    encoding = locale.getpreferredencoding()
+                    normalized = locale.normalize('%s.%s' % (language, encoding))
+                    locale.setlocale(locale.LC_ALL, normalized)
+                except locale.Error, e:
+                    # If we got so far, attempts to set up language and locale have failed
+                    # on this system
+                    sys.stderr.write("Failed to enforce user specified language '%s' with error: '%s'\n" % (language, e))
+                    sys.stderr.write("A LANGUAGE environmental variable has been set.\nPart of messages will be displayed in the requested language.\n")
+                    # Even if setting locale will fail, let's set LANG in a hope,
+                    # that UI will use it GRASS texts will be in selected language,
+                    # system messages (i.e. OK, Cancel etc.) - in system default
+                    # language
+                    os.environ['LANGUAGE'] = language
+                    return
+    
     # Set up environment for subprocesses
+    os.environ['LANGUAGE'] = language
+    os.environ['LANG'] = language
+    if encoding:
+        normalized = locale.normalize('%s.%s' % (language, encoding))
+    else:
+        normalized = language
     for lc in ('LC_CTYPE', 'LC_MESSAGES', 'LC_TIME', 'LC_COLLATE',
                'LC_MONETARY', 'LC_PAPER', 'LC_NAME', 'LC_ADDRESS',
                'LC_TELEPHONE', 'LC_MEASUREMENT', 'LC_IDENTIFICATION'):
-        os.environ[lc] = language
+        os.environ[lc] = normalized
 
     # Some code in GRASS might not like other decimal separators than .
     # Other potential sources for problems are: LC_TIME LC_CTYPE
@@ -840,11 +845,13 @@
     os.environ['LC_NUMERIC'] = 'C'
     if os.getenv('LC_ALL'):
         del os.environ['LC_ALL']  # Remove LC_ALL to not override LC_NUMERIC
-
+    
     # From now on enforce the new language
-    gettext.install('grasslibs', os.path.join(gisbase, 'locale'), unicode=True)
+    if encoding: 
+        gettext.install('grasslibs', os.path.join(gisbase, 'locale'), codeset=encoding)
+    else:
+        gettext.install('grasslibs', os.path.join(gisbase, 'locale'), unicode=True)
 
-
 def check_lock():
     global lockfile
     if not os.path.exists(location):



More information about the grass-commit mailing list