[GRASS-SVN] r52517 - in grass/trunk: gui/wxpython/core lib/init

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Aug 3 22:11:15 PDT 2012


Author: marisn
Date: 2012-08-03 22:11:14 -0700 (Fri, 03 Aug 2012)
New Revision: 52517

Modified:
   grass/trunk/gui/wxpython/core/settings.py
   grass/trunk/lib/init/grass.py
Log:
For GUI language switching, use only languages with present translations. Don't allow to switch to unsupported locale.

Modified: grass/trunk/gui/wxpython/core/settings.py
===================================================================
--- grass/trunk/gui/wxpython/core/settings.py	2012-08-04 05:06:23 UTC (rev 52516)
+++ grass/trunk/gui/wxpython/core/settings.py	2012-08-04 05:11:14 UTC (rev 52517)
@@ -54,25 +54,11 @@
     def _generateLocale(self):
         """!Generate locales
         """
-        # collect available locales
-        self.locs = list(set(locale.locale_alias.values()))
-        self.locs.append('en_GB.UTF-8')
+        import os
+        
+        self.locs = os.listdir(os.path.join(os.environ['GISBASE'], 'locale'))
         self.locs.sort()
         
-        try:
-            loc = list(locale.getdefaultlocale())
-        except ValueError, e:
-            sys.stderr.write(_('ERROR: %s\n') % str(e))
-            return 'C'
-        
-        if loc[1] == 'UTF8':
-            loc[1] = 'UTF-8'
-        code_loc = "%s.%s" % (loc[0], loc[1])
-        if code_loc in self.locs:
-            return code_loc
-        
-        return 'C'
-        
     def _defaultSettings(self):
         """!Define default settings
         """

Modified: grass/trunk/lib/init/grass.py
===================================================================
--- grass/trunk/lib/init/grass.py	2012-08-04 05:06:23 UTC (rev 52516)
+++ grass/trunk/lib/init/grass.py	2012-08-04 05:11:14 UTC (rev 52517)
@@ -294,15 +294,6 @@
         warning(_("GUI <%s> not supported in this version") % grass_gui)
         grass_gui = default_gui
     
-def get_locale():
-    global locale
-    locale = None
-    for var in ['LC_ALL', 'LC_MESSAGES', 'LANG', 'LANGUAGE']: 
-        loc = os.getenv(var) 
-        if loc: 
-            locale = loc[0:2] 
-        return 
-
 def path_prepend(dir, var):
     path = os.getenv(var)
     if path:
@@ -647,6 +638,7 @@
     location = os.path.join(gisdbase, location_name, mapset)
 
 def set_env_from_gisrc():
+    import locale
     kv = read_gisrc()
     
     ### addons
@@ -658,8 +650,27 @@
             os.environ['GRASS_ADDON_PATH'] = addon_path
     
     ### language
-    if kv.get('LANG'):
-        os.environ['LANGUAGE'] = kv.get('LANG')
+    language = kv.get('LANG')
+    if language:
+        language = language.split('.')[0] # Split off ignored .encoding part if present
+        try:
+            locale.setlocale(locale.LC_MESSAGES, language)
+        except:
+            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_MESSAGES, language)
+            except:
+                # If we got so far, provided locale is not supported on this system
+                # C is safe failback.
+                print "Failed to set LC_MESSAGES to %s. Giving up." % language
+                language = 'C'
+        os.environ['LANGUAGE'] = language
+        os.environ['LC_MESSAGES'] = language
+        # Calling gettext.install twice seems to allow to see also localized startup messages
+        # Black magic ;)
+        gettext.install('grasslibs', os.path.join(gisbase, 'locale'), unicode = True)
     
 def check_lock():
     global lockfile
@@ -1069,9 +1080,6 @@
 # Get environmental variables from gisenv variables
 set_env_from_gisrc()
 
-# Get Locale name
-get_locale()
-
 # Set PATH, PYTHONPATH
 set_paths()
 



More information about the grass-commit mailing list