[gtkmm] Gtk::Application: Set the global locale



commit b0afd728bc9135061d061d5d7c01891d8d4f7525
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Wed Dec 14 09:56:24 2016 +0100

    Gtk::Application: Set the global locale
    
    The constructors set the global locale as specified by
    Glib::get_init_to_users_preferred_locale(). Bug 661588

 gtk/src/application.ccg |   27 +++++++++++++++++++++++++++
 gtk/src/application.hg  |   16 ++++++++--------
 2 files changed, 35 insertions(+), 8 deletions(-)
---
diff --git a/gtk/src/application.ccg b/gtk/src/application.ccg
index ab4ce92..e4f4be7 100644
--- a/gtk/src/application.ccg
+++ b/gtk/src/application.ccg
@@ -27,6 +27,9 @@
 #endif //GTKMM_ATKMM_ENABLED
 #include <gdkmm/wrap_init.h>
 #include <gtkmm/wrap_init.h>
+#include <locale>
+#include <clocale>
+#include <stdexcept>
 
 namespace
 {
@@ -48,10 +51,32 @@ static void init_gtkmm_internals()
     Gdk::wrap_init();
     Gtk::wrap_init();
 
+    // Shall gtk_init() set the global C locale to the user's preferred locale?
+    if (!Glib::get_init_to_users_preferred_locale())
+      gtk_disable_setlocale();
+
     init_done = true;
   }
 }
 
+// Make sure the C++ locale is the same as the C locale.
+// The first call to Glib::init() fixes this. We repeat it here just to be sure.
+// The application program may have changed the locale settings after
+// the first call to Glib::init(). Unlikely, but possible.
+static void set_cxx_locale_to_c_locale()
+{
+  try
+  {
+    // Make the C++ locale equal to the C locale.
+    std::locale::global(std::locale(std::setlocale(LC_ALL, nullptr)));
+  }
+  catch (const std::runtime_error& ex)
+  {
+    g_warning("Can't make the global C++ locale equal to the C locale.\n"
+      "   %s\n   C locale = %s\n", ex.what(), std::setlocale(LC_ALL, nullptr));
+  }
+}
+
 } // anonymous namespace
 
 namespace Gtk
@@ -73,6 +98,7 @@ Application::Application(const Glib::ustring& application_id, Gio::ApplicationFl
   m_argv(nullptr)
 {
   gtk_init(nullptr, nullptr);
+  set_cxx_locale_to_c_locale();
 }
 
 Application::Application(int& argc, char**& argv, const Glib::ustring& application_id, Gio::ApplicationFlags 
flags)
@@ -85,6 +111,7 @@ Application::Application(int& argc, char**& argv, const Glib::ustring& applicati
   m_argv(argv)
 {
   gtk_init(&argc, &argv);
+  set_cxx_locale_to_c_locale();
 }
 
 Glib::RefPtr<Application> Application::create(const Glib::ustring& application_id, Gio::ApplicationFlags 
flags)
diff --git a/gtk/src/application.hg b/gtk/src/application.hg
index 252060f..c6afbc1 100644
--- a/gtk/src/application.hg
+++ b/gtk/src/application.hg
@@ -103,9 +103,9 @@ protected:
   /** Creates a new Application instance.
    *
    * This initializes gtkmm (and the GObject type system) for you.
+   * The global locale is set as specified by Glib::set_init_to_users_preferred_locale().
    *
-   * If non-empty, the application ID must be valid.  See
-   * g_application_id_is_valid().
+   * If non-empty, the application ID must be valid. See Gio::Application::id_is_valid().
    *
    * If no application ID is given then some features (most notably application
    * uniqueness) will be disabled. A null application ID is only allowed with
@@ -128,6 +128,7 @@ protected:
    *
    * This initializes gtkmm (and the GObject type system) for you, parsing the
    * standard GTK+ command line arguments.
+   * The global locale is set as specified by Glib::set_init_to_users_preferred_locale().
    *
    * You could instead use the constructor that takes no command line arguments
    * and later supply the command line arguments to run().
@@ -136,8 +137,7 @@ protected:
    * can instead be achieved by setting suitable environment variables
    * such as G_DEBUG.
    *
-   * If non-empty, the application ID must be valid.  See
-   * g_application_id_is_valid().
+   * If non-empty, the application ID must be valid. See Gio::Application::id_is_valid().
    *
    * If no application ID is given then some features (most notably application
    * uniqueness) will be disabled. A null application ID is only allowed with
@@ -157,9 +157,9 @@ public:
   /** Creates a new Application instance.
    *
    * This initializes gtkmm (and the GObject type system) for you.
+   * The global locale is set as specified by Glib::set_init_to_users_preferred_locale().
    *
-   * If non-empty, the application ID must be valid.  See
-   * g_application_id_is_valid().
+   * If non-empty, the application ID must be valid. See Gio::Application::id_is_valid().
    *
    * If no application ID is given then some features (most notably application
    * uniqueness) will be disabled. A null application ID is only allowed with
@@ -176,6 +176,7 @@ public:
    *
    * This initializes gtkmm (and the GObject type system) for you, parsing the
    * standard GTK+ command line arguments.
+   * The global locale is set as specified by Glib::set_init_to_users_preferred_locale().
    *
    * You could instead use the constructor that takes no command line arguments
    * and later supply the command line arguments to run().
@@ -184,8 +185,7 @@ public:
    * can instead be achieved by setting suitable environment variables
    * such as G_DEBUG.
    *
-   * If non-empty, the application ID must be valid.  See
-   * g_application_id_is_valid().
+   * If non-empty, the application ID must be valid. See Gio::Application::id_is_valid().
    *
    * If no application ID is given then some features (most notably application
    * uniqueness) will be disabled. A null application ID is only allowed with


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