Commit ec20d0fa authored by Christian Hergert's avatar Christian Hergert

i18n: simplify i18n for use within libpeas

We don't need a public (but not exposed via headers) function in our ABI
to do gettext. Instead, we can just wrap _() to use g_dgettext() with a
predefined GETTEXT_PACKAGE.

Additionally, instead of requiring callers to check/initialize the
libpeas gettext textdomain, we can use a static constructor to set that
up once at startup.

Related !24

Fixes #35
parent a0664d35
......@@ -5,10 +5,10 @@ gtk_prefix = gtk_dep.get_pkgconfig_variable('prefix')
gtk_docpath = join_paths(gtk_prefix, 'share', 'gtk-doc', 'html')
libpeas_reference_ignored_h = [
'gconstructor.h',
'peas-debug.h',
'peas-dirs.h',
'peas-engine-priv.h',
'peas-i18n.h',
'peas-introspection.h',
'peas-marshal.h',
'peas-plugin-info-priv.h',
......
......@@ -23,7 +23,7 @@
#include <config.h>
#endif
#include <libpeas/peas-i18n.h>
#include <libpeas/peas-i18n-priv.h>
#include <libpeas/peas-plugin-info.h>
#include "peas-gtk-disable-plugins-dialog.h"
......
......@@ -29,7 +29,7 @@
#include <string.h>
#include <libpeas/peas-engine.h>
#include <libpeas/peas-i18n.h>
#include <libpeas/peas-i18n-priv.h>
#include "peas-gtk-plugin-manager-view.h"
#include "peas-gtk-disable-plugins-dialog.h"
......
......@@ -35,7 +35,7 @@
#include <libpeas/peas-engine.h>
#include <libpeas/peas-plugin-info.h>
#include <libpeas/peas-i18n.h>
#include <libpeas/peas-i18n-priv.h>
#include "peas-gtk-plugin-manager.h"
#include "peas-gtk-plugin-manager-view.h"
......
/*
If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
destructors, in a sane way, including e.g. on library unload. If not you're on
your own.
Some compilers need #pragma to handle this, which does not work with macros,
so the way you need to use this is (for constructors):
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
#endif
G_DEFINE_CONSTRUCTOR(my_constructor)
static void my_constructor(void) {
...
}
*/
#ifndef __GTK_DOC_IGNORE__
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
#define G_HAS_CONSTRUCTORS 1
#define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
#define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
#elif defined (_MSC_VER) && (_MSC_VER >= 1500)
/* Visual studio 2008 and later has _Pragma */
#define G_HAS_CONSTRUCTORS 1
#define G_DEFINE_CONSTRUCTOR(_func) \
static void _func(void); \
static int _func ## _wrapper(void) { _func(); return 0; } \
__pragma(section(".CRT$XCU",read)) \
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _wrapper;
#define G_DEFINE_DESTRUCTOR(_func) \
static void _func(void); \
static int _func ## _constructor(void) { atexit (_func); return 0; } \
__pragma(section(".CRT$XCU",read)) \
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
#elif defined (_MSC_VER)
#define G_HAS_CONSTRUCTORS 1
/* Pre Visual studio 2008 must use #pragma section */
#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
section(".CRT$XCU",read)
#define G_DEFINE_CONSTRUCTOR(_func) \
static void _func(void); \
static int _func ## _wrapper(void) { _func(); return 0; } \
__declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
section(".CRT$XCU",read)
#define G_DEFINE_DESTRUCTOR(_func) \
static void _func(void); \
static int _func ## _constructor(void) { atexit (_func); return 0; } \
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
#elif defined(__SUNPRO_C)
/* This is not tested, but i believe it should work, based on:
* http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
*/
#define G_HAS_CONSTRUCTORS 1
#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
init(_func)
#define G_DEFINE_CONSTRUCTOR(_func) \
static void _func(void);
#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
fini(_func)
#define G_DEFINE_DESTRUCTOR(_func) \
static void _func(void);
#else
/* constructors not supported for this compiler */
#endif
#endif /* __GTK_DOC_IGNORE__ */
......@@ -25,7 +25,7 @@
#include <string.h>
#include "peas-i18n.h"
#include "peas-i18n-priv.h"
#include "peas-engine.h"
#include "peas-engine-priv.h"
#include "peas-plugin-info-priv.h"
......
......@@ -25,7 +25,7 @@
#include "peas-extension-set.h"
#include "peas-i18n.h"
#include "peas-i18n-priv.h"
#include "peas-introspection.h"
#include "peas-plugin-info.h"
#include "peas-marshal.h"
......
......@@ -16,60 +16,17 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* Handles all of the internationalization configuration options.
* Author: Tom Tromey <tromey@creche.cygnus.com>
*
* This is a modified version of gtksourceview-i18n.h
*/
#ifndef __PEAS_18N_H__
#define __PEAS_18N_H__
#ifndef __PEAS_I18N_PRIV_H__
#define __PEAS_I18N_PRIV_H__
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "peas-version-macros.h"
#include <glib.h>
G_BEGIN_DECLS
#ifdef ENABLE_NLS
# include <libintl.h>
# undef _
# define _(String) peas_gettext (String)
# undef N_
# ifdef gettext_noop
# define N_(String) gettext_noop (String)
# else
# define N_(String) (String)
# endif
#else
/* Stubs that do something close enough. */
# undef textdomain
# define textdomain(String) (String)
# undef gettext
# define gettext(String) (String)
# undef dgettext
# define dgettext(Domain,Message) (Message)
# undef dcgettext
# define dcgettext(Domain,Message,Type) (Message)
# undef bindtextdomain
# define bindtextdomain(Domain,Directory) (Domain)
# undef bind_textdomain_codeset
# define bind_textdomain_codeset(Domain,CodeSet) (Domain)
# undef _
# define _(String) (String)
# undef N_
# define N_(String) (String)
#endif
PEAS_AVAILABLE_IN_ALL
const gchar *peas_gettext (const char *msgid) G_GNUC_FORMAT(1);
/* not really I18N-related, but also a string marker macro */
#define I_(string) g_intern_static_string (string)
#define _(s) g_dgettext(GETTEXT_PACKAGE, s)
#define I_(s) g_intern_string(s)
G_END_DECLS
#endif /* __PEAS_I18N_H__ */
#endif /* __PEAS_I18N_PRIV_H__ */
......@@ -21,38 +21,29 @@
#include <config.h>
#endif
#include <glib/gi18n.h>
#include <string.h>
#include "peas-i18n.h"
#include "peas-dirs.h"
/**
* peas_gettext:
* @msgid: The string to be translated
*
* Returns the translated string from the libpeas translations.
* This is an internal function and should only be used by
* the internals of libpeas (such as libpeas or libpeas-gtk).
*
* Returns: the transation of @msgid to the current locale
*/
const gchar *
peas_gettext (const gchar *msgid)
{
static gboolean initialized = FALSE;
#include "gconstructor.h"
if (G_UNLIKELY (!initialized))
{
gchar *locale_dir;
locale_dir = peas_dirs_get_locale_dir ();
#if defined (G_HAS_CONSTRUCTORS)
# ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
# pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(peas_init_ctor)
# endif
G_DEFINE_CONSTRUCTOR(peas_init_ctor)
#else
# error Your platform/compiler is missing constructor support
#endif
(void) bindtextdomain (GETTEXT_PACKAGE, locale_dir);
g_free (locale_dir);
static void
peas_init_ctor (void)
{
gchar *locale_dir = peas_dirs_get_locale_dir ();
(void) bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
initialized = TRUE;
}
bindtextdomain (GETTEXT_PACKAGE, locale_dir);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
return g_dgettext (GETTEXT_PACKAGE, msgid);
g_free (locale_dir);
}
......@@ -23,9 +23,8 @@
#include "config.h"
#include <string.h>
#include <glib.h>
#include "peas-i18n.h"
#include "peas-i18n-priv.h"
#include "peas-plugin-info-priv.h"
#include "peas-utils.h"
......
......@@ -23,10 +23,11 @@
#include <config.h>
#endif
#include <gtk/gtk.h>
#include <girepository.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <libpeas/peas.h>
#include <libpeas/peas-i18n.h>
#include <libpeas-gtk/peas-gtk.h>
#include "peas-demo-window.h"
......
......@@ -19,14 +19,12 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "config.h"
#include <glib.h>
#include <gtk/gtk.h>
#include <libpeas/peas.h>
#include <libpeas/peas-i18n.h>
#include <libpeas/peas-i18n-priv.h>
#include <libpeas-gtk/peas-gtk.h>
#include "testing/testing.h"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment