From 45c38b44229f6a329bc1865e6b6b7347e21d8392 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 3 Feb 2005 17:46:40 +0000 Subject: [PATCH] New function which allows to change the function used for translation 2005-02-03 Matthias Clasen * gtk/gtk.symbols: * gtk/gtkstock.h: * gtk/gtkstock.c (gtk_stock_set_translate_func): New function which allows to change the function used for translation stock labels, on a per-domain basis. Use this functionality to switch the GTK+ stock items to use Q_() prefixed msgids. (#166179, Funda Wang) --- ChangeLog | 8 ++ ChangeLog.pre-2-10 | 8 ++ ChangeLog.pre-2-8 | 8 ++ docs/reference/ChangeLog | 4 + docs/reference/gtk/gtk-sections.txt | 1 + gtk/gtk.symbols | 1 + gtk/gtkstock.c | 142 ++++++++++++++++++++++++---- gtk/gtkstock.h | 5 + 8 files changed, 156 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7cda5d5f62..a02f78353c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2005-02-03 Matthias Clasen + * gtk/gtk.symbols: + * gtk/gtkstock.h: + * gtk/gtkstock.c (gtk_stock_set_translate_func): + New function which allows to change the function used for + translation stock labels, on a per-domain basis. Use this + functionality to switch the GTK+ stock items to use Q_() + prefixed msgids. (#166179, Funda Wang) + * gdk/gdkkeynames.c: Make the gdk_key array const (#166075, Tommi Komulainen) diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 7cda5d5f62..a02f78353c 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,5 +1,13 @@ 2005-02-03 Matthias Clasen + * gtk/gtk.symbols: + * gtk/gtkstock.h: + * gtk/gtkstock.c (gtk_stock_set_translate_func): + New function which allows to change the function used for + translation stock labels, on a per-domain basis. Use this + functionality to switch the GTK+ stock items to use Q_() + prefixed msgids. (#166179, Funda Wang) + * gdk/gdkkeynames.c: Make the gdk_key array const (#166075, Tommi Komulainen) diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 7cda5d5f62..a02f78353c 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,5 +1,13 @@ 2005-02-03 Matthias Clasen + * gtk/gtk.symbols: + * gtk/gtkstock.h: + * gtk/gtkstock.c (gtk_stock_set_translate_func): + New function which allows to change the function used for + translation stock labels, on a per-domain basis. Use this + functionality to switch the GTK+ stock items to use Q_() + prefixed msgids. (#166179, Funda Wang) + * gdk/gdkkeynames.c: Make the gdk_key array const (#166075, Tommi Komulainen) diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index 40f7c5488d..56308930ca 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,7 @@ +2005-02-03 Matthias Clasen + + * gtk/gtk-sections.txt: Add gtk_stock_set_translate_func. + 2005-02-02 Matthias Clasen * gdk/tmpl/event_structs.sgml: diff --git a/docs/reference/gtk/gtk-sections.txt b/docs/reference/gtk/gtk-sections.txt index 1f3b72c830..67d90fcd69 100644 --- a/docs/reference/gtk/gtk-sections.txt +++ b/docs/reference/gtk/gtk-sections.txt @@ -5363,6 +5363,7 @@ gtk_stock_item_copy gtk_stock_item_free gtk_stock_list_ids gtk_stock_lookup +gtk_stock_set_translate_func GTK_STOCK_ABOUT GTK_STOCK_ADD diff --git a/gtk/gtk.symbols b/gtk/gtk.symbols index 902da3b546..bd5c90e63b 100644 --- a/gtk/gtk.symbols +++ b/gtk/gtk.symbols @@ -1946,6 +1946,7 @@ gtk_stock_item_copy gtk_stock_item_free gtk_stock_list_ids gtk_stock_lookup +gtk_stock_set_translate_func gtk_style_apply_default_background gtk_style_attach gtk_style_copy diff --git a/gtk/gtkstock.c b/gtk/gtkstock.c index 247ac94b15..606df1b300 100644 --- a/gtk/gtkstock.c +++ b/gtk/gtkstock.c @@ -33,6 +33,7 @@ #include "gtkintl.h" #include +static GHashTable *translate_hash = NULL; static GHashTable *stock_hash = NULL; static void init_stock_hash (void); @@ -41,6 +42,14 @@ static void init_stock_hash (void); */ #define NON_STATIC_MASK (1 << 29) +typedef struct _GtkStockTranslateFunc GtkStockTranslateFunc; +struct _GtkStockTranslateFunc +{ + GtkTranslateFunc func; + gpointer data; + GtkDestroyNotify notify; +}; + static void real_add (const GtkStockItem *items, guint n_items, @@ -155,7 +164,17 @@ gtk_stock_lookup (const gchar *stock_id, *item = *found; item->modifier &= ~NON_STATIC_MASK; if (item->label) - item->label = dgettext (item->translation_domain, item->label); + { + GtkStockTranslateFunc *translate; + + translate = (GtkStockTranslateFunc *) + g_hash_table_lookup (translate_hash, item->translation_domain); + + if (translate != NULL && translate->func != NULL) + item->label = (* translate->func) (item->label, translate->data); + else + item->label = dgettext (item->translation_domain, item->label); + } } return found != NULL; @@ -308,14 +327,22 @@ static const GtkStockItem builtin_items [] = { GTK_STOCK_FIND, N_("_Find"), GDK_CONTROL_MASK, 'f', GETTEXT_PACKAGE }, { GTK_STOCK_FIND_AND_REPLACE, N_("Find and _Replace"), GDK_CONTROL_MASK, 'r', GETTEXT_PACKAGE }, { GTK_STOCK_FLOPPY, N_("_Floppy"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_GOTO_BOTTOM, N_("_Bottom"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_GOTO_FIRST, N_("_First"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_GOTO_LAST, N_("_Last"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_GOTO_TOP, N_("_Top"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_GO_BACK, N_("_Back"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_GO_DOWN, N_("_Down"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_GO_FORWARD, N_("_Forward"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_GO_UP, N_("_Up"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_GOTO_BOTTOM, N_("Navigation|_Bottom"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_GOTO_FIRST, N_("Navigation|_First"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_GOTO_LAST, N_("Navigation|_Last"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_GOTO_TOP, N_("Navigation|_Top"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_GO_BACK, N_("Navigation|_Back"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_GO_DOWN, N_("Navigation|_Down"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_GO_FORWARD, N_("Navigation|_Forward"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_GO_UP, N_("Navigation|_Up"), 0, 0, GETTEXT_PACKAGE }, { GTK_STOCK_HARDDISK, N_("_Harddisk"), 0, 0, GETTEXT_PACKAGE }, { GTK_STOCK_HELP, N_("_Help"), GDK_CONTROL_MASK, 'h', GETTEXT_PACKAGE }, { GTK_STOCK_HOME, N_("_Home"), 0, 0, GETTEXT_PACKAGE }, @@ -324,18 +351,31 @@ static const GtkStockItem builtin_items [] = { GTK_STOCK_INDEX, N_("_Index"), 0, 0, GETTEXT_PACKAGE }, { GTK_STOCK_ITALIC, N_("_Italic"), 0, 0, GETTEXT_PACKAGE }, { GTK_STOCK_JUMP_TO, N_("_Jump to"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_JUSTIFY_CENTER, N_("_Center"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_JUSTIFY_FILL, N_("_Fill"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_JUSTIFY_LEFT, N_("_Left"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_JUSTIFY_RIGHT, N_("_Right"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_MEDIA_FORWARD, N_("_Forward"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_MEDIA_NEXT, N_("_Next"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_MEDIA_PAUSE, N_("P_ause"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_MEDIA_PLAY, N_("_Play"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_MEDIA_PREVIOUS, N_("Pre_vious"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_MEDIA_RECORD, N_("_Record"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_MEDIA_REWIND, N_("R_ewind"), 0, 0, GETTEXT_PACKAGE }, - { GTK_STOCK_MEDIA_STOP, N_("_Stop"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_JUSTIFY_CENTER, N_("Justify|_Center"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_JUSTIFY_FILL, N_("Justify|_Fill"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_JUSTIFY_LEFT, N_("Justify|_Left"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_JUSTIFY_RIGHT, N_("Justify|_Right"), 0, 0, GETTEXT_PACKAGE }, + + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_MEDIA_FORWARD, N_("Media|_Forward"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_MEDIA_NEXT, N_("Media|_Next"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_MEDIA_PAUSE, N_("Media|P_ause"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_MEDIA_PLAY, N_("Media|_Play"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_MEDIA_PREVIOUS, N_("Media|Pre_vious"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_MEDIA_RECORD, N_("Media|_Record"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_MEDIA_REWIND, N_("Media|R_ewind"), 0, 0, GETTEXT_PACKAGE }, + /* translators, strip the prefix up to and including the first | */ + { GTK_STOCK_MEDIA_STOP, N_("Media|_Stop"), 0, 0, GETTEXT_PACKAGE }, { GTK_STOCK_NETWORK, N_("_Network"), 0, 0, GETTEXT_PACKAGE }, { GTK_STOCK_NEW, N_("_New"), GDK_CONTROL_MASK, 'n', GETTEXT_PACKAGE }, { GTK_STOCK_NO, N_("_No"), 0, 0, GETTEXT_PACKAGE }, @@ -370,6 +410,56 @@ static const GtkStockItem builtin_items [] = { GTK_STOCK_ZOOM_OUT, N_("Zoom _Out"), 0, 0, GETTEXT_PACKAGE } }; +/** + * gtk_stock_set_translate_func: + * @func: a #GtkTranslateFunc + * @data: data to pass to @func + * @notify: a #GtkDestroyNotify that is called when @data is + * no longer needed + * + * Sets a function to be used for translating the @label of + * a stock item. + * + * If no function is registered for a translation domain, + * dgettext() is used. + * + * Since: 2.8 + * + */ +void +gtk_stock_set_translate_func (const gchar *domain, + GtkTranslateFunc func, + gpointer data, + GtkDestroyNotify notify) +{ + GtkStockTranslateFunc *translate; + + translate = (GtkStockTranslateFunc *) + g_hash_table_lookup (translate_hash, domain); + + if (translate) + { + if (translate->notify) + (* translate->notify) (translate->data); + } + else + translate = g_new0 (GtkStockTranslateFunc, 1); + + translate->func = func; + translate->data = data; + translate->notify = notify; + + g_hash_table_insert (translate_hash, domain, translate); +} + +static gchar * +sgettext_swapped (const gchar *msgid, + const gchar *domainname) +{ + return g_strip_context (msgid, dgettext (domainname, msgid)); +} + + static void init_stock_hash (void) { @@ -379,4 +469,14 @@ init_stock_hash (void) gtk_stock_add_static (builtin_items, G_N_ELEMENTS (builtin_items)); } + + if (translate_hash == NULL) + { + translate_hash = g_hash_table_new (g_str_hash, g_str_equal); + + gtk_stock_set_translate_func (GETTEXT_PACKAGE, + sgettext_swapped, + GETTEXT_PACKAGE, + NULL); + } } diff --git a/gtk/gtkstock.h b/gtk/gtkstock.h index 419cc5075a..d079a1aa21 100644 --- a/gtk/gtkstock.h +++ b/gtk/gtkstock.h @@ -29,6 +29,7 @@ #include +#include /* for GtkTranslateFunc */ #ifdef __cplusplus extern "C" { @@ -60,6 +61,10 @@ GSList* gtk_stock_list_ids (void); GtkStockItem *gtk_stock_item_copy (const GtkStockItem *item); void gtk_stock_item_free (GtkStockItem *item); +void gtk_stock_set_translate_func (const gchar *domain, + GtkTranslateFunc func, + gpointer data, + GtkDestroyNotify notify); /* Stock IDs (not all are stock items; some are images only) */ #define GTK_STOCK_DIALOG_AUTHENTICATION \