Hello to everyone.
I have sent a patch for the bug #34506 that solves the "context"
problem, but I think that is a first approach, because it looks for it
in every tag. I have spent a couple of days looking into the Glade code
and I have some ideas:
* The first one is extend start_element_handler in x-glade.c to
support new GtkBuilder features. It could also solve bug #29216 and add
support to <item> elements, and it is easy to do, but this is not a
beautiful function actually.
* The second one is modify x-glade.h and split glade support in two
extractor functions, one for GtkBuilder and one for the old way (or also
split Glade1 and Glade2). This also could be achieved creating a new
extractor (i.e. x-gtkbuilder.{c,h}) or refactoring the whole module and
possibly extracting libexpat support. Anyway, the context extraction
from Glade2 files (#34506) should be fixed because it is not a new
feature of GtkBuilder.
Also, It seems that libexpat is used nowhere except here, but I have
seen that libxml is used in gnulib. At least, libexpat code should be in
a separate header.
I will thank any advices if I have to sign anything to contribute with a
bigger code. I also could help with the refactor of other parsers that I
have seen you are going to do.
Cheers.
Miguel Ángel Arruga Vivas
diff --git a/gettext-tools/src/x-glade.c b/gettext-tools/src/x-glade.c
index 11f8397..d052aee 100644
--- a/gettext-tools/src/x-glade.c
+++ b/gettext-tools/src/x-glade.c
@@ -386,6 +386,7 @@ struct element_state
{
bool extract_string;
char *extracted_comment;
+ const char *extracted_context;
int lineno;
char *buffer;
size_t bufmax;
@@ -411,6 +412,26 @@ ensure_stack_size (size_t size)
static size_t stack_depth;
+static const char *
+extract_context (const char **attributes)
+{
+ bool found_context = false;
+ const char ** attp = attributes;
+ const char * context = NULL;
+
+ while (!found_context && (*attp != NULL))
+ {
+ if (strcmp (attp[0], "context") == 0)
+ {
+ found_context = true;
+ context = attp[1];
+ }
+ attp += 2;
+ }
+
+ return context;
+}
+
/* Callback called when <element> is seen. */
static void
start_element_handler (void *userData, const char *name,
@@ -429,6 +450,7 @@ start_element_handler (void *userData, const char *name,
p = &stack[stack_depth];
p->extract_string = extract_all;
p->extracted_comment = NULL;
+ p->extracted_context = extract_context (attributes);
/* In Glade 1, a few specific elements are translatable. */
if (!p->extract_string)
p->extract_string =
@@ -470,11 +492,14 @@ start_element_handler (void *userData, const char *name,
if (strcmp (attp[1], "") != 0)
{
lex_pos_ty pos;
+ char * context = (p->extracted_context != NULL)
+ ? xstrdup (p->extracted_context)
+ : NULL;
pos.file_name = logical_file_name;
pos.line_number = XML_GetCurrentLineNumber (parser);
- remember_a_message (mlp, NULL, xstrdup (attp[1]),
+ remember_a_message (mlp, context, xstrdup (attp[1]),
null_context, &pos,
NULL, savable_comment);
}
@@ -504,6 +529,9 @@ end_element_handler (void *userData, const char *name)
if (p->buflen > 0)
{
lex_pos_ty pos;
+ char * context = (p->extracted_context != NULL)
+ ? xstrdup (p->extracted_context)
+ : NULL;
if (p->buflen == p->bufmax)
p->buffer = (char *) xrealloc (p->buffer, p->buflen + 1);
@@ -512,8 +540,8 @@ end_element_handler (void *userData, const char *name)
pos.file_name = logical_file_name;
pos.line_number = p->lineno;
- remember_a_message (mlp, NULL, p->buffer, null_context, &pos,
- p->extracted_comment, savable_comment);
+ remember_a_message (mlp, context, p->buffer, null_context, &pos,
+ p->extracted_comment, savable_comment);
p->buffer = NULL;
}
}