[glom/glom-1-16] get_current_locale(): Handle more complex setlocale(NULL) results.



commit fb24175b02dc5c1c8e2a1381434402fef9e20248
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Jan 6 23:47:41 2011 +0100

    get_current_locale(): Handle more complex setlocale(NULL) results.
    
    * glom/libglom/utils.cc: locale_simplify(): On Ubuntu Natty,
    setlocale() has started returning a more complex string. Parse it, or the
    older simpler syntax.

 ChangeLog                                        |    8 +++++
 glom/libglom/data_structure/translatable_item.cc |    3 +-
 glom/libglom/utils.cc                            |   31 ++++++++++++++++++++-
 glom/mode_design/iso_codes.cc                    |    7 +++--
 glom/mode_design/translation/combobox_locale.cc  |   11 ++++++-
 5 files changed, 52 insertions(+), 8 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 67c5126..ff7cf07 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2011-01-06  Murray Cumming  <murrayc murrayc com>
+
+	get_current_locale(): Handle more complex setlocale(NULL) results.
+ 
+	* glom/libglom/utils.cc: locale_simplify(): On Ubuntu Natty, 
+	setlocale() has started returning a more complex string. Parse it, or the 
+	older simpler syntax.
+
 2010-12-15  Ben Konrath  <ben bagu org>
 
 	Add Eclipse 3.6 / CDT 7.0 configuration files.
diff --git a/glom/libglom/data_structure/translatable_item.cc b/glom/libglom/data_structure/translatable_item.cc
index 2f8d133..04fd85d 100644
--- a/glom/libglom/data_structure/translatable_item.cc
+++ b/glom/libglom/data_structure/translatable_item.cc
@@ -207,8 +207,9 @@ Glib::ustring TranslatableItem::get_current_locale()
     const char* cLocale = setlocale(LC_ALL, 0); //Passing NULL means query, instead of set.
     if(cLocale)
     {
-      //std::cout << "TranslatableItem::get_current_locale(): locale=" << cLocale << std::endl;
+      //std::cout << "debug1: " << G_STRFUNC << ": locale=" << cLocale << std::endl;
       m_current_locale = Utils::locale_simplify(cLocale);
+      //std::cout << "debug2: " << G_STRFUNC << ": m_current_locale=" << m_current_locale << std::endl;
     }
     else
       m_current_locale = 'C';
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index cd0a2ad..9b6728b 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -616,20 +616,47 @@ Glib::ustring Utils::string_escape_underscores(const Glib::ustring& text)
 Glib::ustring Utils::locale_simplify(const Glib::ustring& locale_id)
 {
   Glib::ustring result = locale_id;
-
+  
+  //At least Ubuntu Natty provides a long string such as this: LC_CTYPE=en_US.UTF-8;LC_NUMERIC=en_US.UTF-8;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_AG.utf8;LC_PAPER=en_US.UTF-8;LC_NAME=en_US.UTF-8;LC_ADDRESS=en_US.UTF-8;LC_TELEPHONE=en_US.UTF-8;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=en_US.UTF-8
+  //In Ubuntu Maverick, and earlier, it was apparently a simple string such as en_US.UTF-8.
+
+  //Look for LC_ALL or LC_COLLATE
+  //(We use the locale name only to identify translations
+  //Otherwise just start with the whole string.
+  Glib::ustring::size_type posCategory = result.find("LC_ALL=");
+  if(posCategory != Glib::ustring::npos)
+  {
+    result = result.substr(posCategory);
+  }
+  else
+  {
+    posCategory = result.find("LC_COLLATE=");
+    if(posCategory != Glib::ustring::npos)
+    {
+      result = result.substr(posCategory);
+    }
+  }
+  
   //Get everything before the .:
-  Glib::ustring::size_type posDot = locale_id.find('.');
+  const Glib::ustring::size_type posDot = result.find('.');
   if(posDot != Glib::ustring::npos)
   {
     result = result.substr(0, posDot);
   }
 
   //Get everything before the @:
-  const Glib::ustring::size_type posAt = locale_id.find('@');
+  const Glib::ustring::size_type posAt = result.find('@');
   if(posAt != Glib::ustring::npos)
   {
     result = result.substr(0, posAt);
   }
+  
+  //Get everything after the =, if any:
+  const Glib::ustring::size_type posEquals = result.find('=');
+  if(posEquals != Glib::ustring::npos)
+  {
+    result = result.substr(posEquals + 1);
+  }
 
   return result;
 }
diff --git a/glom/mode_design/iso_codes.cc b/glom/mode_design/iso_codes.cc
index 961753c..9447ad0 100644
--- a/glom/mode_design/iso_codes.cc
+++ b/glom/mode_design/iso_codes.cc
@@ -107,7 +107,7 @@ Glib::ustring get_locale_name(const Glib::ustring& locale_id)
     typedef std::list<std::string> type_list_ids;
     type_list_ids list_ids;
 
-    Glib::ustring locales_path = "/usr/share/i18n/locales/";
+    const std::string locales_path = "/usr/share/i18n/locales/";
 #ifdef GLIBMM_EXCEPTIONS_ENABLED
     try
     {
@@ -137,7 +137,7 @@ Glib::ustring get_locale_name(const Glib::ustring& locale_id)
     typedef std::map<Glib::ustring, Glib::ustring> type_map_language; //ID to language name.
     type_map_language map_languages;
 
-    const Glib::ustring filename_languages = ISO_CODES_PREFIX "/share/xml/iso-codes/iso_639.xml";
+    const std::string filename_languages = ISO_CODES_PREFIX "/share/xml/iso-codes/iso_639.xml";
 
 #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
     try
@@ -242,7 +242,7 @@ Glib::ustring get_locale_name(const Glib::ustring& locale_id)
     }
 #endif // LIBXMLCPP_EXCEPTIONS_ENABLED
 
-      //Use a map so we can easily check for duplicates.
+    //Use a map so we can easily check for duplicates.
     for(type_list_ids::iterator iter = list_ids.begin(); iter != list_ids.end(); ++iter)
     {
       const Glib::ustring identifier = Utils::locale_simplify(*iter);
@@ -277,6 +277,7 @@ Glib::ustring get_locale_name(const Glib::ustring& locale_id)
             the_locale.m_identifier = identifier;
             the_locale.m_name = name;
             map_locales[identifier] = the_locale;
+            //std::cout << "DEBUG: id=" << identifier << ", name=" << name << std::endl;
           }
         }
       }
diff --git a/glom/mode_design/translation/combobox_locale.cc b/glom/mode_design/translation/combobox_locale.cc
index b97ed76..0786e3e 100644
--- a/glom/mode_design/translation/combobox_locale.cc
+++ b/glom/mode_design/translation/combobox_locale.cc
@@ -84,16 +84,23 @@ void ComboBox_Locale::set_selected_locale(const Glib::ustring& locale)
     for(Gtk::TreeModel::iterator iter = model->children().begin(); iter != model->children().end(); ++iter)
     {
       const Glib::ustring& this_text = (*iter)[m_model_columns.m_identifier];
-
+      //std::cout << G_STRFUNC << ": DEBUG: locale=" << locale << ", this_text=" << this_text << "." << std::endl;
+ 
       if(this_text == locale)
       {
         set_active(iter);
         return; //success
       }
     }
+
+    //Not found, so mark it as blank:
+    std::cerr << G_STRFUNC << ": locale not found in list: " << locale << ", list size=" << model->children().size() << std::endl;
+  }
+  else
+  {
+    std::cerr << G_STRFUNC << ": locale not found in list: " << locale << ". The model is empty." << std::endl;
   }
 
-  //Not found, so mark it as blank:
   std::cerr << "ComboBox_Locale::set_selected_locale(): locale not found in list: " << locale << std::endl;
   unset_active();
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]