Added g_disable_setlocale().
authorYair Hershkovitz <yairhr@gmail.com>
Fri, 16 May 2008 22:52:42 +0000 (22:52 +0000)
committerYair Hershkovitz <yairhr@src.gnome.org>
Fri, 16 May 2008 22:52:42 +0000 (22:52 +0000)
2008-05-17  Yair Hershkovitz  <yairhr@gmail.com>

* 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

ChangeLog
glib/gi18n.c
glib/gi18n.h
glib/glib.symbols

index 79a72eb..07c4d50 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2008-05-17  Yair Hershkovitz  <yairhr@gmail.com>
+
+       * 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  <tml@novell.com>
 
        * config.h.win32.in: Update to match the configure-produced one.
index 471e5cb..b855036 100644 (file)
 #include <string.h>
 #include <locale.h>
 
-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 <literal>setlocale (LC_ALL, "")</literal>. 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);
 }
 
 /**
index 17f06f9..f4c3bcb 100644 (file)
@@ -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
 
index 5e5a0b7..fd8e8c4 100644 (file)
@@ -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