[GRASS-SVN] r53502 - in grass/branches/develbranch_6/gui/wxpython: core gui_core

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Oct 19 07:45:06 PDT 2012


Author: martinl
Date: 2012-10-19 07:45:05 -0700 (Fri, 19 Oct 2012)
New Revision: 53502

Modified:
   grass/branches/develbranch_6/gui/wxpython/core/globalvar.py
   grass/branches/develbranch_6/gui/wxpython/core/settings.py
   grass/branches/develbranch_6/gui/wxpython/gui_core/preferences.py
Log:
wxGUI: sync lang support with relbr64
       developers: new features should come first into devbr6!!!


Modified: grass/branches/develbranch_6/gui/wxpython/core/globalvar.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/core/globalvar.py	2012-10-19 14:37:05 UTC (rev 53501)
+++ grass/branches/develbranch_6/gui/wxpython/core/globalvar.py	2012-10-19 14:45:05 UTC (rev 53502)
@@ -184,6 +184,50 @@
                     
     Debug.msg(1, "Number of new AddOn commands: %d", nCmd)
 
+def SetLanguage():
+    import locale
+    
+    language = os.getenv('LANG')
+    if language:
+        language = language.split('.')[0] # Split off ignored .encoding part if present
+        orig_language = language
+        try:
+            locale.setlocale(locale.LC_ALL, 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_ALL, language)
+            except:
+                # If we got so far, provided locale is not supported on this system
+                sys.stderr.write("Failed to set LC_ALL to %s\n" % language)
+                try:
+                    default_locale = locale.getdefaultlocale()
+                except:
+                    default_locale = None
+                
+                if default_locale and default_locale[0]:
+                    language = default_locale[0]
+                else:
+                    language = 'C'
+        
+        # Set up environment for subprocesses
+        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
+        
+        # Some code in GRASS might not like other decimal separators than .
+        # Other potential sources for problems are: LC_TIME LC_CTYPE
+        locale.setlocale(locale.LC_NUMERIC, 'C')
+        os.environ['LC_NUMERIC'] = 'C'
+        if os.getenv('LC_ALL'):
+            del os.environ['LC_ALL'] # Remove LC_ALL to not override LC_NUMERIC
+        
+        # Even if setting locale has failed, 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'] = orig_language
+        os.environ['LANG'] = orig_language
+
 """@brief Collected GRASS-relared binaries/scripts"""
 grassCmd, grassScripts = GetGRASSCommands()
 Debug.msg(1, "Number of GRASS commands: %d", len(grassCmd))
@@ -200,3 +244,5 @@
 
 """@Check version of wxPython, use agwStyle for 2.8.11+"""
 hasAgw = CheckWxVersion()
+
+SetLanguage()

Modified: grass/branches/develbranch_6/gui/wxpython/core/settings.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/core/settings.py	2012-10-19 14:37:05 UTC (rev 53501)
+++ grass/branches/develbranch_6/gui/wxpython/core/settings.py	2012-10-19 14:45:05 UTC (rev 53502)
@@ -54,25 +54,20 @@
     def _generateLocale(self):
         """!Generate locales
         """
-        # collect available locales
-        self.locs = list(set(locale.locale_alias.values()))
-        self.locs.append('en_GB.UTF-8')
-        self.locs.sort()
+        import os
         
         try:
-            loc = list(locale.getdefaultlocale())
-        except ValueError, e:
-            sys.stderr.write(_('ERROR: %s\n') % str(e))
-            return 'C'
+            self.locs = os.listdir(os.path.join(os.environ['GISBASE'], 'locale'))
+            self.locs.append('en') # GRASS doesn't ship EN po files
+            self.locs.sort()
+            # Add a default choice to not override system locale
+            self.locs.insert(0, 'system')
+        except:
+            # No NLS
+            self.locs = ['system']
         
-        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 'system'
         
-        return 'C'
-        
     def _defaultSettings(self):
         """!Define default settings
         """
@@ -81,8 +76,6 @@
         except KeyError:
             projFile = ''
         
-        id_loc = self._generateLocale()
-        
         self.defaultSettings = {
             #
             # general
@@ -157,7 +150,7 @@
             #
             'language': {
                 'locale': {
-                    'lc_all' : id_loc
+                    'lc_all' : self._generateLocale(),
                 }
             },
             #

Modified: grass/branches/develbranch_6/gui/wxpython/gui_core/preferences.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_core/preferences.py	2012-10-19 14:37:05 UTC (rev 53501)
+++ grass/branches/develbranch_6/gui/wxpython/gui_core/preferences.py	2012-10-19 14:45:05 UTC (rev 53502)
@@ -162,15 +162,25 @@
         Posts event EVT_SETTINGS_CHANGED.
         """
         if self._updateSettings():
+            lang = self.settings.Get(group = 'language', key = 'locale', subkey = 'lc_all')
+            if lang == 'system':
+                # Most fool proof way to use system locale is to not provide any locale info at all
+                self.settings.Set(group = 'language', key = 'locale', subkey = 'lc_all', value = None)
+                lang = None
+            if lang == 'en':
+                # GRASS doesn't ship EN translation, default texts have to be used instead
+                self.settings.Set(group = 'language', key = 'locale', subkey = 'lc_all', value = 'C')
+                lang = 'C'
             self.settings.SaveToFile()
             self.parent.goutput.WriteLog(_('Settings saved to file \'%s\'.') % self.settings.filePath)
-            lang = UserSettings.Get(group = 'language', key = 'locale', subkey = 'lc_all')
             if lang:
                 RunCommand('g.gisenv', set = 'LANG=%s' % lang)
+            else:
+                RunCommand('g.gisenv', set = 'LANG=')
             event = wxSettingsChanged()
             wx.PostEvent(self, event)
             self.Close()
-
+        
     def _updateSettings(self):
         """!Update user settings"""
         for item in self.winId.keys():



More information about the grass-commit mailing list