Add try/catch around i18n locale setter code, just in case...
authorBastien Montagne <montagne29@wanadoo.fr>
Mon, 31 Dec 2012 12:12:50 +0000 (12:12 +0000)
committerBastien Montagne <montagne29@wanadoo.fr>
Mon, 31 Dec 2012 12:12:50 +0000 (12:12 +0000)
intern/locale/boost_locale_wrapper.cpp

index 939c66bad132dba342d34e0b6bcbd10415c27bb3..80e75298d70ce74b1327f1ee0a33187b7be282f4 100644 (file)
@@ -55,44 +55,49 @@ void bl_locale_set(const char *locale)
        // Specify location of dictionaries.
        gen.add_messages_path(messages_path);
        gen.add_messages_domain(default_domain);
-       //gen.set_default_messages_domain(default_domain);      
+       //gen.set_default_messages_domain(default_domain);
 
-       if (locale && locale[0]) {
-               std::locale::global(gen(locale));
-       }
-       else {
-#if defined (__APPLE__)
-               // workaround to get osx system locale from user defaults
-               FILE* fp;
-               std::string locale_osx = "";
-               char result[16];
-               int result_len = 0;
+       try {
+               if (locale && locale[0]) {
+                       std::locale::global(gen(locale));
+               }
+               else {
+#ifdef __APPLE__
+                       // workaround to get osx system locale from user defaults
+                       FILE *fp;
+                       std::string locale_osx = "";
+                       char result[16];
+                       int result_len = 0;
 
-               fp = popen("defaults read .GlobalPreferences AppleLocale", "r");
+                       fp = popen("defaults read .GlobalPreferences AppleLocale", "r");
 
-               if(fp) {
-                       result_len = fread(result, 1, sizeof(result) - 1, fp);
+                       if (fp) {
+                               result_len = fread(result, 1, sizeof(result) - 1, fp);
 
-                       if(result_len > 0) {
-                               result[result_len-1] = '\0'; // \0 terminate and remove \n
-                               locale_osx = std::string(result) + std::string(".UTF-8");
-                       }
+                               if (result_len > 0) {
+                                       result[result_len - 1] = '\0'; // \0 terminate and remove \n
+                                       locale_osx = std::string(result) + std::string(".UTF-8");
+                               }
 
-                       pclose(fp);
-               }
+                               pclose(fp);
+                       }
 
-               if(locale_osx == "")
-                       fprintf(stderr, "Locale set: failed to read AppleLocale read from defaults\n");
+                       if (locale_osx == "")
+                               fprintf(stderr, "Locale set: failed to read AppleLocale read from defaults\n");
 
-               std::locale::global(gen(locale_osx.c_str()));
+                       std::locale::global(gen(locale_osx.c_str()));
 #else
-               std::locale::global(gen(""));
+                       std::locale::global(gen(""));
 #endif
+               }
+               // Note: boost always uses "C" LC_NUMERIC by default!
+       }
+       catch(std::exception const &e) {
+               std::cout << "bl_locale_set(" << locale << "): " << e.what() << " \n";
        }
-       // Note: boost always uses "C" LC_NUMERIC by default!
 }
 
-const charbl_locale_pgettext(const char *msgctxt, const char *msgid)
+const char *bl_locale_pgettext(const char *msgctxt, const char *msgid)
 {
        // Note: We cannot use short stuff like boost::locale::gettext, because those return
        //       std::basic_string objects, which c_ptr()-returned char* is no more valid
@@ -107,7 +112,7 @@ const char* bl_locale_pgettext(const char *msgctxt, const char *msgid)
                return msgid;
        }
        catch(std::exception const &e) {
-//             std::cout << "boost_locale_pgettext: " << e.what() << " \n";
+//             std::cout << "bl_locale_pgettext(" << msgctxt << ", " << msgid << "): " << e.what() << " \n";
                return msgid;
        }
 }