[gnumeric] Funcs: improve locale handling when dealing with help texts.



commit 114c5f698da8620c4dfe41289fc6cfb474b16931
Author: Morten Welinder <terra gnome org>
Date:   Wed Mar 30 10:40:14 2011 -0400

    Funcs: improve locale handling when dealing with help texts.

 ChangeLog  |    8 ++++
 src/func.c |  118 ++++++++++++++++++++++++++++++++++++++----------------------
 src/func.h |    1 +
 3 files changed, 84 insertions(+), 43 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 0f00412..0b9a832 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2011-03-30  Morten Welinder  <terra gnome org>
 
+	* src/func.c (dump_externals): Properly use translation domain.
+	(function_dump_defs): Ditto.
+	(function_def_create_arg_names): Ditto.
+	(gnm_func_get_description): Ditto.
+	(gnm_func_get_arg_description): Ditto.
+	(gnm_func_get_name): Get the translated name from
+	GNM_FUNC_HELP_NAME.
+
 	* src/parse-util.h (GnmConventions): Add field for localized
 	function names.
 
diff --git a/src/func.c b/src/func.c
index 31da915..31d0d22 100644
--- a/src/func.c
+++ b/src/func.c
@@ -35,6 +35,8 @@
 #include <string.h>
 #include <stdlib.h>
 
+#define F2(func,s) dgettext ((func)->textdomain->str, (s))
+
 static GList	    *categories;
 static SymbolTable  *global_symbol_table;
 static GnmFuncGroup *unknown_cat;
@@ -158,7 +160,7 @@ dump_externals (GPtrArray *defs, FILE *out)
 		int j;
 
 		for (j = 0; fd->help[j].type != GNM_FUNC_HELP_END; j++) {
-			const char *s = _(fd->help[j].text);
+			const char *s = F2(fd, fd->help[j].text);
 
 			switch (fd->help[j].type) {
 			case GNM_FUNC_HELP_EXTREF:
@@ -333,14 +335,14 @@ function_dump_defs (char const *filename, int dump_type)
 			GString *seealso = g_string_new (NULL);
 
 			fprintf (output_file, "@CATEGORY=%s\n",
-				 _(fd->fn_group->display_name->str));
+				 F2(fd, fd->fn_group->display_name->str));
 			for (i = 0;
 			     fd->help[i].type != GNM_FUNC_HELP_END;
 			     i++) {
 				switch (fd->help[i].type) {
 				case GNM_FUNC_HELP_NAME: {
 					char *short_desc;
-					char *name = split_at_colon (_(fd->help[i].text), &short_desc);
+					char *name = split_at_colon (F2(fd, fd->help[i].text), &short_desc);
 					fprintf (output_file,
 						 "@FUNCTION=%s\n",
 						 name);
@@ -355,21 +357,21 @@ function_dump_defs (char const *filename, int dump_type)
 				case GNM_FUNC_HELP_SEEALSO:
 					if (seealso->len > 0)
 						g_string_append (seealso, ",");
-					g_string_append (seealso, _(fd->help[i].text));
+					g_string_append (seealso, F2(fd, fd->help[i].text));
 					break;
 				case GNM_FUNC_HELP_DESCRIPTION:
 					if (desc->len > 0)
 						g_string_append (desc, "\n");
-					g_string_append (desc, _(fd->help[i].text));
+					g_string_append (desc, F2(fd, fd->help[i].text));
 					break;
 				case GNM_FUNC_HELP_NOTE:
 					if (note->len > 0)
 						g_string_append (note, " ");
-					g_string_append (note, _(fd->help[i].text));
+					g_string_append (note, F2(fd, fd->help[i].text));
 					break;
 				case GNM_FUNC_HELP_ARG: {
 					char *argdesc;
-					char *name = split_at_colon (_(fd->help[i].text), &argdesc);
+					char *name = split_at_colon (F2(fd, fd->help[i].text), &argdesc);
 					if (first_arg)
 						first_arg = FALSE;
 					else
@@ -388,12 +390,12 @@ function_dump_defs (char const *filename, int dump_type)
 				case GNM_FUNC_HELP_ODF:
 					if (odf->len > 0)
 						g_string_append (odf, " ");
-					g_string_append (odf, _(fd->help[i].text));
+					g_string_append (odf, F2(fd, fd->help[i].text));
 					break;
 				case GNM_FUNC_HELP_EXCEL:
 					if (excel->len > 0)
 						g_string_append (excel, " ");
-					g_string_append (excel, _(fd->help[i].text));
+					g_string_append (excel, F2(fd, fd->help[i].text));
 					break;
 
 				case GNM_FUNC_HELP_EXTREF:
@@ -875,15 +877,16 @@ function_def_create_arg_names (GnmFunc const *fn_def)
 	g_return_val_if_fail (fn_def != NULL, NULL);
 
 	ptr = g_ptr_array_new ();
-	if (fn_def->help != NULL)
-		for (i = 0;
-		     fn_def->help[i].type != GNM_FUNC_HELP_END;
-		     i++) {
-			if (fn_def->help[i].type == GNM_FUNC_HELP_ARG)
-				g_ptr_array_add
-					(ptr, split_at_colon
-					 (_(fn_def->help[i].text), NULL));
-		}
+	for (i = 0;
+	     fn_def->help && fn_def->help[i].type != GNM_FUNC_HELP_END;
+	     i++) {
+		if (fn_def->help[i].type != GNM_FUNC_HELP_ARG)
+			continue;
+
+		g_ptr_array_add
+			(ptr, split_at_colon
+			 (F2(fn_def, fn_def->help[i].text), NULL));
+	}
 	return ptr;
 }
 
@@ -960,6 +963,7 @@ gnm_func_free (GnmFunc *func)
 
 	if (func->textdomain)
 		go_string_unref (func->textdomain);
+	g_free (func->localized_name);
 
 	if (func->arg_names_p) {
 		g_ptr_array_foreach (func->arg_names_p, (GFunc) g_free, NULL);
@@ -1035,6 +1039,7 @@ gnm_func_add (GnmFuncGroup *fn_group,
 	func->flags		= desc->flags;
 	func->impl_status	= desc->impl_status;
 	func->test_status	= desc->test_status;
+	func->localized_name    = NULL;
 
 	func->user_data		= NULL;
 	func->ref_count		= 0;
@@ -1195,11 +1200,36 @@ gnm_func_set_user_data (GnmFunc *func, gpointer user_data)
 char const *
 gnm_func_get_name (GnmFunc const *func, gboolean localized_function_names)
 {
+	int i;
+
 	g_return_val_if_fail (func != NULL, NULL);
 
-	return localized_function_names
-		? dgettext (func->textdomain->str, func->name)
-		: func->name;
+	if (!localized_function_names)
+		return func->name;
+
+	gnm_func_load_if_stub ((GnmFunc *)func);
+
+	for (i = 0;
+	     (func->localized_name == NULL &&
+	      func->help &&
+	      func->help[i].type != GNM_FUNC_HELP_END);
+	     i++) {
+		const char *s, *sl;
+		char *U;
+		if (func->help[i].type != GNM_FUNC_HELP_NAME)
+			continue;
+
+		s = func->help[i].text;
+		sl = F2 (func, s);
+		if (s == sl) /* String not actually translated. */
+			continue;
+
+		U = split_at_colon (F2 (func, s), NULL);
+		((GnmFunc *)func)->localized_name = U ? g_utf8_strdown (U, -1) : NULL;
+		g_free (U);
+	}
+
+	return func->localized_name;
 }
 
 /**
@@ -1217,16 +1247,17 @@ gnm_func_get_description (GnmFunc const *fn_def)
 
 	gnm_func_load_if_stub ((GnmFunc *)fn_def);
 
-	if (fn_def->help != NULL)
-		for (i = 0;
-		     fn_def->help[i].type != GNM_FUNC_HELP_END;
-		     i++) {
-			if (fn_def->help[i].type == GNM_FUNC_HELP_NAME) {
-				gchar const *desc;
-				desc = strchr (_(fn_def->help[i].text), ':');
-				return desc ? (desc + 1) : "";
-			}
-		}
+	for (i = 0;
+	     fn_def->help && fn_def->help[i].type != GNM_FUNC_HELP_END;
+	     i++) {
+		const char *desc;
+
+		if (fn_def->help[i].type != GNM_FUNC_HELP_NAME)
+			continue;
+
+		desc = strchr (F2 (fn_def, fn_def->help[i].text), ':');
+		return desc ? (desc + 1) : "";
+	}
 	return "";
 }
 
@@ -1386,23 +1417,24 @@ function_def_get_arg_name (GnmFunc const *fn_def, guint arg_idx)
 char const*
 gnm_func_get_arg_description (GnmFunc const *fn_def, guint arg_idx)
 {
-	guint arg = 0;
 	gint i;
 	g_return_val_if_fail (fn_def != NULL, NULL);
 
 	gnm_func_load_if_stub ((GnmFunc *)fn_def);
 
-	if (fn_def->help != NULL)
-		for (i = 0;
-		     fn_def->help[i].type != GNM_FUNC_HELP_END;
-		     i++) {
-			if (fn_def->help[i].type == GNM_FUNC_HELP_ARG
-			    && arg++ == arg_idx) {
-				gchar const *desc;
-				desc = strchr (_(fn_def->help[i].text), ':');
-				return desc ? (desc + 1) : "";
-			}
-		}
+	for (i = 0;
+	     fn_def->help && fn_def->help[i].type != GNM_FUNC_HELP_END;
+	     i++) {
+		gchar const *desc;
+
+		if (fn_def->help[i].type != GNM_FUNC_HELP_ARG)
+			continue;
+		if (--arg_idx)
+			continue;
+
+		desc = strchr (F2 (fn_def, fn_def->help[i].text), ':');
+		return desc ? (desc + 1) : "";
+	}
 
 	return "";
 }
diff --git a/src/func.h b/src/func.h
index a5ef56e..ab40680 100644
--- a/src/func.h
+++ b/src/func.h
@@ -193,6 +193,7 @@ struct _GnmFunc {
 	GPtrArray  *arg_names_p;
 	GnmFuncHelp const *help;
 	GOString *textdomain;
+	char *localized_name;
 	GnmFuncType fn_type;
 	union {
 		GnmFuncNodes nodes;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]