Hello to everyone.

Reading through HACKINGs of other GNU projects I have noticed one thing:
The first thing to do is create or modify an existing test case, so I
have done it. This is a new test case to test context extraction in
glade files. It is taken almost enterely from xgettext-glade-4. It is
worse than I thought at first place. In the (old) DTD
http://developer.gnome.org/libglade/unstable/libglade-dtd.html you can
read this:
"""
<!-- context indicates that the value has a |-separated 
     context which must be stripped before use, look up
g_strip_context() 
     in the GLib API documentation for details.-->
<!ATTLIST property
  name CDATA #REQUIRED
  type CDATA #IMPLIED
  translatable (yes|no) 'no'
  context (yes|no) 'no'
  comments CDATA #IMPLIED
  agent CDATA #IMPLIED >
"""

But for GtkBuilder you can read at
http://developer.gnome.org/gtk3/3.4/GtkBuilder.html that attribute
"context" contains _the context_:
"""
property = element property {
  attribute name { text },
  attribute translatable { "yes" | "no" } ?,
  attribute comments { text } ?,
  attribute context { text } ?,
  text ?
}
"""

This means that my first patch was wrong, this were not Glade2 context
support. The attached patch fixes Glade context support and It is based
on xgettext.c, because Glade uses the same format as Glib macro
Q_(String) http://developer.gnome.org/glib/2.30/glib-I18N.html#Q-:CAPS
but maybe It could be extracted to a common function.

On the other hand, this means that context support in GtkBuilder is not
supported yet and need to be worked on.

I look forward to hearing from you :)
Cheers.
Miguel

Attachment: xgettext-glade-5
Description: application/shellscript

diff --git a/gettext-tools/src/x-glade.c b/gettext-tools/src/x-glade.c
index 11f8397..1834d6c 100644
--- a/gettext-tools/src/x-glade.c
+++ b/gettext-tools/src/x-glade.c
@@ -385,6 +385,7 @@ static XML_Parser parser;
 struct element_state
 {
   bool extract_string;
+  bool extract_context;
   char *extracted_comment;
   int lineno;
   char *buffer;
@@ -428,6 +429,7 @@ start_element_handler (void *userData, const char *name,
 
   p = &stack[stack_depth];
   p->extract_string = extract_all;
+  p->extract_context = false;
   p->extracted_comment = NULL;
   /* In Glade 1, a few specific elements are translatable.  */
   if (!p->extract_string)
@@ -443,6 +445,7 @@ start_element_handler (void *userData, const char *name,
       && (strcmp (name, "property") == 0 || strcmp (name, "atkproperty") == 0))
     {
       bool has_translatable = false;
+      bool has_context = false; // Default is 'no'
       const char *extracted_comment = NULL;
       const char **attp = attributes;
       while (*attp != NULL)
@@ -451,9 +454,12 @@ start_element_handler (void *userData, const char *name,
             has_translatable = (strcmp (attp[1], "yes") == 0);
           else if (strcmp (attp[0], "comments") == 0)
             extracted_comment = attp[1];
+          else if (strcmp (attp[0], "context") == 0)
+            has_context = (strcmp (attp[1], "yes") == 0);
           attp += 2;
         }
       p->extract_string = has_translatable;
+      p->extract_context = has_context;
       p->extracted_comment =
         (has_translatable && extracted_comment != NULL
          ? xstrdup (extracted_comment)
@@ -512,9 +518,38 @@ 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);
-          p->buffer = NULL;
+          if (p->extract_context)
+            {
+              const char *separator = strchr (p->buffer, '|');
+
+              if (separator == NULL)
+                {
+                  error_with_progname = false;
+                  error_at_line (0, 0, logical_file_name, p->lineno,
+                                 _("warning: missing |-context in string `%s'"),
+                                 p->buffer);
+                  error_with_progname = true;
+                }
+              else
+                {
+                  size_t context_len = separator - p->buffer;
+                  char *context = XNMALLOC (context_len + 1, char);
+                  char *message = xstrdup (separator + 1);
+
+                  memcpy (context, p->buffer, context_len);
+                  context[context_len] = '\0';
+
+                  remember_a_message (mlp, context, message, null_context,
+                                      &pos, p->extracted_comment,
+                                      savable_comment);
+                }
+            }
+          else
+            {
+              remember_a_message (mlp, NULL, p->buffer, null_context, &pos,
+                                  p->extracted_comment, savable_comment);
+              p->buffer = NULL;
+            }
         }
     }
 

Reply via email to