Added g_disable_setlocale().

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
This commit is contained in:
Yair Hershkovitz 2008-05-16 22:52:42 +00:00 committed by Yair Hershkovitz
parent a6cd26d656
commit 49734ebc7f
4 changed files with 65 additions and 17 deletions

View File

@ -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.

View File

@ -31,29 +31,57 @@
#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);
if (!*gettext (""))
{
g_should_translate = FALSE;
g_warning ("textdomain() must be called before glib i18n initialization");
goto out;
}
if (!*gettext ("") && (!locale || strcmp (locale, "C")))
{
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);
}
/**

View File

@ -43,6 +43,7 @@ G_CONST_RETURN gchar *g_dpgettext (const gchar *domain,
gsize msgidoffset);
void g_i18n_init (void);
void g_disable_setlocale (void);
G_END_DECLS

View 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