From 3e5d1bd6ff5e52d4d5862dc001cec0fa6e2ae2f0 Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Wed, 5 Jun 2019 16:34:57 +0800 Subject: [PATCH 1/3] glib-private.h: Set the appropriate string for setlocale() On Windows, in particular the CRTs on and after Visual Studio 2012, it is not enough just to do setlocale (LC_ALL, "") to set the default system locale, which results in the tools that use the translated messages to show unreadable messages when running the commmand line tools. This adds an entry in glib-private.h.in which denotes the appropriate string to use for setlocale() to set the default system locale by setting it to ".ACP" if we are building on Windows and "" for other systems (as we are doing now). The tools in gio/ will be updated in the next commit to make use of this entry so that the translated messages can be shown correctly. --- glib/glib-private.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/glib/glib-private.h b/glib/glib-private.h index d0099236f..8b7ab132e 100644 --- a/glib/glib-private.h +++ b/glib/glib-private.h @@ -84,4 +84,25 @@ typedef struct { GLIB_AVAILABLE_IN_ALL GLibPrivateVTable *glib__private__ (void); +/* Please see following for the use of ".ACP" over "" + * on Windows, although both are accepted at compile-time + * but "" renders translated console messages unreadable if + * built with Visual Studio 2012 and later (this is, unfortunately, + * undocumented): + * + * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale + * https://gitlab.gnome.org/GNOME/glib/merge_requests/895#note_525881 + * https://gitlab.gnome.org/GNOME/glib/merge_requests/895#note_525900 + * + * Additional related items: + * https://stackoverflow.com/questions/22604329/php-5-5-setlocale-not-working-in-cli-on-windows + * https://bugs.php.net/bug.php?id=66265 + */ + +#ifdef G_OS_WIN32 +# define GLIB_DEFAULT_LOCALE ".ACP" +#else +# define GLIB_DEFAULT_LOCALE "" +#endif + #endif /* __GLIB_PRIVATE_H__ */ From 9f709fe1e918e24500250db815f52886bf8ed56a Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Wed, 5 Jun 2019 16:42:30 +0800 Subject: [PATCH 2/3] gio tools: Use the proper string for default locale for setlocale() This makes use of the string we now have from glib-private.h in the last commit so that setlocale() sets the default system locale correctly and therefore show the translated messages properly. Fixes issue #1169. --- gio/gio-querymodules.c | 4 +++- gio/gio-tool.c | 4 ++-- gio/glib-compile-resources.c | 5 +---- gio/glib-compile-schemas.c | 5 +---- gio/gresource-tool.c | 4 +--- gio/gsettings-tool.c | 4 +--- 6 files changed, 9 insertions(+), 17 deletions(-) diff --git a/gio/gio-querymodules.c b/gio/gio-querymodules.c index 74c659462..eb5094f4d 100644 --- a/gio/gio-querymodules.c +++ b/gio/gio-querymodules.c @@ -26,6 +26,8 @@ #include #include +#include "glib/glib-private.h" + static gboolean is_valid_module_name (const gchar *basename) { @@ -160,7 +162,7 @@ main (gint argc, return 1; } - setlocale (LC_ALL, ""); + setlocale (LC_ALL, GLIB_DEFAULT_LOCALE); /* Be defensive and ensure we're linked to GObject */ g_type_ensure (G_TYPE_OBJECT); diff --git a/gio/gio-tool.c b/gio/gio-tool.c index 847a2188b..abcd46d05 100644 --- a/gio/gio-tool.c +++ b/gio/gio-tool.c @@ -28,7 +28,7 @@ #include #include "gio-tool.h" - +#include "glib/glib-private.h" void print_error (const gchar *format, ...) @@ -253,7 +253,7 @@ main (int argc, char **argv) const char *command; gboolean do_help; - setlocale (LC_ALL, ""); + setlocale (LC_ALL, GLIB_DEFAULT_LOCALE); textdomain (GETTEXT_PACKAGE); bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR); diff --git a/gio/glib-compile-resources.c b/gio/glib-compile-resources.c index 399c567d2..ccffbce2c 100644 --- a/gio/glib-compile-resources.c +++ b/gio/glib-compile-resources.c @@ -43,10 +43,7 @@ #include "gvdb/gvdb-builder.h" #include "gconstructor_as_data.h" - -#ifdef G_OS_WIN32 #include "glib/glib-private.h" -#endif typedef struct { @@ -754,7 +751,7 @@ main (int argc, char **argv) gchar *tmp; #endif - setlocale (LC_ALL, ""); + setlocale (LC_ALL, GLIB_DEFAULT_LOCALE); textdomain (GETTEXT_PACKAGE); #ifdef G_OS_WIN32 diff --git a/gio/glib-compile-schemas.c b/gio/glib-compile-schemas.c index f8b5b6825..f22265b82 100644 --- a/gio/glib-compile-schemas.c +++ b/gio/glib-compile-schemas.c @@ -29,10 +29,7 @@ #include "gvdb/gvdb-builder.h" #include "strinfo.c" - -#ifdef G_OS_WIN32 #include "glib/glib-private.h" -#endif static void strip_string (GString *string) @@ -2188,7 +2185,7 @@ main (int argc, char **argv) gchar *tmp = NULL; #endif - setlocale (LC_ALL, ""); + setlocale (LC_ALL, GLIB_DEFAULT_LOCALE); textdomain (GETTEXT_PACKAGE); #ifdef G_OS_WIN32 diff --git a/gio/gresource-tool.c b/gio/gresource-tool.c index d1de02631..1914c5228 100644 --- a/gio/gresource-tool.c +++ b/gio/gresource-tool.c @@ -41,9 +41,7 @@ #include #include -#ifdef G_OS_WIN32 #include "glib/glib-private.h" -#endif #if defined(HAVE_LIBELF) && defined(HAVE_MMAP) #define USE_LIBELF @@ -610,7 +608,7 @@ main (int argc, char *argv[]) gchar *tmp; #endif - setlocale (LC_ALL, ""); + setlocale (LC_ALL, GLIB_DEFAULT_LOCALE); textdomain (GETTEXT_PACKAGE); #ifdef G_OS_WIN32 diff --git a/gio/gsettings-tool.c b/gio/gsettings-tool.c index b7dd9d8fb..3096f9d9b 100644 --- a/gio/gsettings-tool.c +++ b/gio/gsettings-tool.c @@ -25,9 +25,7 @@ #include #include -#ifdef G_OS_WIN32 #include "glib/glib-private.h" -#endif static GSettingsSchemaSource *global_schema_source; static GSettings *global_settings; @@ -743,7 +741,7 @@ main (int argc, char **argv) gchar *tmp; #endif - setlocale (LC_ALL, ""); + setlocale (LC_ALL, GLIB_DEFAULT_LOCALE); textdomain (GETTEXT_PACKAGE); #ifdef G_OS_WIN32 From 3046f7b3bb7123c93265a99c4a4db9d2902465e1 Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Fri, 14 Jun 2019 12:34:58 +0800 Subject: [PATCH 3/3] gio-tool.c: Don't hardcode localedir on Windows We ought to construct the localedir based on the location of the GLib DLL, like what the other tools do. --- gio/gio-tool.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/gio/gio-tool.c b/gio/gio-tool.c index abcd46d05..c0aec7492 100644 --- a/gio/gio-tool.c +++ b/gio/gio-tool.c @@ -253,9 +253,20 @@ main (int argc, char **argv) const char *command; gboolean do_help; +#ifdef G_OS_WIN32 + gchar *localedir; +#endif + setlocale (LC_ALL, GLIB_DEFAULT_LOCALE); textdomain (GETTEXT_PACKAGE); + +#ifdef G_OS_WIN32 + localedir = _glib_get_locale_dir (); + bindtextdomain (GETTEXT_PACKAGE, localedir); + g_free (localedir); +#else bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR); +#endif #ifdef HAVE_BIND_TEXTDOMAIN_CODESET bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");