From: Yair Hershkovitz Date: Fri, 16 May 2008 22:52:42 +0000 (+0000) Subject: Added g_disable_setlocale(). X-Git-Url: http://git.openbox.org/?a=commitdiff_plain;h=49734ebc7f70e142da5039e426df6fc31b03c629;p=dana%2Fcg-glib.git Added g_disable_setlocale(). 2008-05-17 Yair Hershkovitz * glib/glib.symbols: * glib/gi18n.h: Added g_disable_setlocale(). * glib/gi18n.c: Added g_disable_setlocale() API to disable setting the locale in g_i18n_init(). Dont disable translations if textdomain was not set before calling g_i18n_init(). Dont disable translations if the locale is "C". svn path=/trunk/; revision=6894 --- diff --git a/ChangeLog b/ChangeLog index 79a72eb3..07c4d50d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2008-05-17 Yair Hershkovitz + + * glib/glib.symbols: + * glib/gi18n.h: Added g_disable_setlocale(). + + * glib/gi18n.c: Added g_disable_setlocale() API to disable setting + the locale in g_i18n_init(). Dont disable translations if textdomain + was not set before calling g_i18n_init(). Dont disable translations if + the locale is "C". + 2008-05-16 Tor Lillqvist * config.h.win32.in: Update to match the configure-produced one. diff --git a/glib/gi18n.c b/glib/gi18n.c index 471e5cb8..b8550367 100644 --- a/glib/gi18n.c +++ b/glib/gi18n.c @@ -31,29 +31,57 @@ #include #include -static gboolean g_should_translate = TRUE; +static gboolean should_translate = TRUE; +static gboolean do_setlocale = TRUE; +static gboolean initialized = FALSE; void g_i18n_init (void) { - gchar *domain, *default_domain; + gchar *domain, *default_domain, *locale; - setlocale (LC_ALL, ""); + initialized = TRUE; + + locale = setlocale (LC_ALL, do_setlocale ? "" : NULL); domain = g_strdup (textdomain (NULL)); default_domain = g_strdup (textdomain ("")); textdomain (domain); if (!strcmp (domain, default_domain)) - g_warning ("textdomain() must be called before glib i18n initialization"); - - g_free (domain); - g_free (default_domain); + { + g_warning ("textdomain() must be called before glib i18n initialization"); + goto out; + } - if (!*gettext ("")) + if (!*gettext ("") && (!locale || strcmp (locale, "C"))) { - g_should_translate = FALSE; + should_translate = FALSE; g_warning ("No translation is available for the requested locale."); } + +out: + g_free (domain); + g_free (default_domain); +} + +/** + * g_disable_setlocale: + * + * Prevents g_i18n_init() from automatically + * calling setlocale (LC_ALL, ""). You would + * want to use this function if you wanted to set the locale for + * your program to something other than the user's locale, or if + * you wanted to set different values for different locale categories. + * + * Most programs should not need to call this function. + **/ +void +g_disable_setlocale (void) +{ + if (initialized) + g_warning ("g_disable_setlocale() must be called before g_i18n_init()"); + + do_setlocale = FALSE; } /** @@ -73,10 +101,14 @@ g_i18n_init (void) const gchar * g_gettext (const gchar *msgid) { - if (g_should_translate) - return gettext (msgid); - else + if (!initialized) + goto out; + + if (!should_translate) return msgid; + +out: + return gettext (msgid); } /** @@ -99,10 +131,14 @@ const gchar * g_dgettext (const gchar *domain, const gchar *msgid) { - if (g_should_translate) - return dgettext (domain, msgid); - else + if (!initialized) + goto out; + + if (!should_translate) return msgid; + +out: + return dgettext (domain, msgid); } /** diff --git a/glib/gi18n.h b/glib/gi18n.h index 17f06f95..f4c3bcb1 100644 --- a/glib/gi18n.h +++ b/glib/gi18n.h @@ -42,7 +42,8 @@ G_CONST_RETURN gchar *g_dpgettext (const gchar *domain, const gchar *msgctxtid, gsize msgidoffset); -void g_i18n_init (void); +void g_i18n_init (void); +void g_disable_setlocale (void); G_END_DECLS diff --git a/glib/glib.symbols b/glib/glib.symbols index 5e5a0b74..fd8e8c43 100644 --- a/glib/glib.symbols +++ b/glib/glib.symbols @@ -1585,6 +1585,7 @@ g_win32_locale_filename_from_utf8 #if IN_HEADER(__G_I18N_H__) #if IN_FILE(__G_I18N_C__) g_i18n_init +g_disable_setlocale g_gettext g_dgettext g_dpgettext