[Po4a-devel]Make attribute translation more flexible

Francois Gouget fgouget@codeweavers.com
Fri Jul 1 09:30:28 UTC 2005


This is a multi-part message in MIME format.
--------------040609030903090609060408
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit


I was not completely happy with my previous attribute translation patch:
  1) the user should be able to control whether he gets msgid "lang=en" 
or msgid "en"
  2) Sgml.pm should behave the same as Xml.pm by default, i.e. generate 
msgid "en"
  3) Xml.pm uses the 'attributes' option while Sgml.pm uses 'attribute'

So here is a patch which tries to address each of these issues:

  3) The rational behind choosing 'attribute' for Sgml.pm was that the 
other options are singular. But in fact they are not nouns so there's no 
singular/plural consideration. Then it's not inconsistent to use 
'attributes' and it would match Xml.pm so it would be better.

  1) The idea is to have two options. The 'attributes' option determines 
which attributes should be translated and in which context while the 
'qualify' option determines which attributes should get a qualified msgid.
     So 'attributes=lang' would generate msgid "en" while 
'attributes=lang qualify=lang' would generate msgid "lang=en".
     Note that the qualify option only takes a list of attribute names 
without a tag context because it would be inconsistent to generate 
different msgids for a given attribute depending on the context.

  2) I modified Sgml.pm so the lang attribute is not qualified by default.

Further it would be nice to add the same capabilities to Xml.pm but I 
had trouble figuring out how so it will go in a separate patch.


Changelog:

  * lib/Locale/Po4a/Sgml.pm

    Francois Gouget <fgouget@codeweavers.com>
    Add a 'qualify' option to denote which attributes should get an 
msgid  'qualified' by the attribute name.
    Don't qualify the lang attribute for consistency with Xml.pm
    Rename the 'attribute' option to 'attributes' for consistency with 
Xml.pm.


-- 
Francois Gouget
fgouget@codeweavers.com


--------------040609030903090609060408
Content-Type: text/plain;
 name="po4a-20050628-attributes.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="po4a-20050628-attributes.diff"

Index: lib/Locale/Po4a/Sgml.pm
===================================================================
RCS file: /cvsroot/po4a/po4a/lib/Locale/Po4a/Sgml.pm,v
retrieving revision 1.61
diff -u -p -r1.61 Sgml.pm
--- lib/Locale/Po4a/Sgml.pm	16 Jun 2005 19:24:25 -0000	1.61
+++ lib/Locale/Po4a/Sgml.pm	28 Jun 2005 16:48:12 -0000
@@ -82,7 +82,7 @@ they can be part of an msgid. For exampl
 for this category since putting it in the translate section would create
 msgids not being whole sentences, which is bad.
 
-=item attribute
+=item attributes
 
 A space separated list of attributes that need to be translated. You can
 specify the attributes by their name (for example, "lang"), but you can also
@@ -94,6 +94,12 @@ The tag names are actually regular expre
 like E<lt>aaa|bbbbE<gt>lang to only translate lang attributes that are in
 an E<lt>aaaE<gt> or a E<lt>bbbE<gt> tag.
 
+=item qualify
+
+A space separated list of attributes for which the translation must be
+qualified by the attribute name. Note that this setting only takes effect
+if the attribute is specified in the 'attributes' list too.
+
 =item force
 
 Proceed even if the DTD is unknown.
@@ -285,13 +291,13 @@ sub set_tags_kind {
     my $self=shift;
     my (%kinds)=@_;
 
-    foreach (qw(translate empty section verbatim ignore attribute)) {
+    foreach (qw(translate empty section verbatim ignore attributes qualify)) {
 	$self->{SGML}->{k}{$_} = $self->{options}{$_} ? $self->{options}{$_}.' ' : '';
     }
     
     foreach (keys %kinds) {
 	die "po4a::sgml: internal error: set_tags_kind called with unrecognized arg $_"
-	    if ($_ !~ /^(translate|empty|verbatim|ignore|indent|attribute)$/);
+	    if ($_ !~ /^(translate|empty|verbatim|ignore|indent|attributes|qualify)$/);
 	
 	$self->{SGML}->{k}{$_} .= $kinds{$_};
     }    
@@ -438,7 +445,7 @@ sub parse_file {
 	                                    "wordasword ".
 	                                    "xref ".
                                             "year",
-	                     "attribute" => "<(article|book)>lang");
+                             "attributes" =>"<(article|book)>lang");
 
     } else {
 	if ($self->{options}{'force'}) {
@@ -628,7 +645,7 @@ sub parse_file {
     open (IN,$cmd) || die wrap_mod("po4a::sgml", dgettext("po4a", "Can't run nsgmls: %s"), $!);
 
     # The kind of tags
-    my (%translate,%empty,%verbatim,%indent,%exist,%attribute);
+    my (%translate,%empty,%verbatim,%indent,%exist,%attribute,%qualify);
     foreach (split(/ /, ($self->{SGML}->{k}{'translate'}||'') )) {
 	$translate{uc $_} = 1;
 	$indent{uc $_} = 1;
@@ -651,7 +668,7 @@ sub parse_file {
     foreach (split(/ /, ($self->{SGML}->{k}{'ignore'}) || '')) {
 	$exist{uc $_} = 1;
     }
-    foreach (split(/ /, ($self->{SGML}->{k}{'attribute'}) || '')) {
+    foreach (split(/ /, ($self->{SGML}->{k}{'attributes'} || ''))) {
         my ($attr, $tags);
         if (m/(^.*>)(\w+)/)
         {
@@ -672,6 +689,9 @@ sub parse_file {
             $attribute{$attr} = $tags;
         }
     }
+    foreach (split(/ /, ($self->{SGML}->{k}{'qualify'}) || '')) {
+        $qualify{uc $_} = 1;
+    }
 
 
     # What to do before parsing
@@ -737,8 +757,9 @@ sub parse_file {
                 if ($val->type() eq 'CDATA' ||
 		    $val->type() eq 'IMPLIED') {
 		    if (defined $value && length($value)) {
-                        my $name=lc $attr;
-                        if (exists $attribute{uc($attr)}) {
+                        my $lattr=lc $attr;
+                        my $uattr=uc $attr;
+                        if (exists $attribute{$uattr}) {
                             my $context="";
                             foreach my $o (@open) {
                                 next if (!defined $o or $o =~ m%^</%);
@@ -747,12 +768,16 @@ sub parse_file {
                             }
                             $context=join("", $context,
                                           "<", lc($event->data->name()), ">");
-                            if ($context =~ /^($attribute{uc($attr)})$/) {
-                                my $translated = $self->translate("$name=$value", $ref, "attribute $context$name");
-                                if ($translated =~ s/^$name=//) {
-                                    $value=$translated;
+                            if ($context =~ /^($attribute{$uattr})$/) {
+                                if ($qualify{$uattr}) {
+                                    my $translated = $self->translate("$lattr=$value", $ref, "attribute $context$lattr");
+                                    if ($translated =~ s/^$lattr=//) {
+                                        $value=$translated;
+                                    } else {
+                                        die wrap_mod("po4a::sgml", dgettext("po4a", "bad translation '%s' for '%s' in '%s'"), $translated, "$context$lattr", $ref);
+                                    }
                                 } else {
-                                    die wrap_mod("po4a::sgml", dgettext("po4a", "bad translation '%s' for '%s' in '%s'"), $translated, "$context$name", $ref);
+                                    $value = $self->translate($value, $ref, "attribute $context$lattr");
                                 }
                             }
                         }
@@ -761,7 +786,7 @@ sub parse_file {
 			} else {
 			    $value = '"'.$value.'"';
 			}
-			$tag .= ' '.lc($attr).'='.$value;
+			$tag .= " $lattr=$value";
 		    }
 		} elsif ($val->type() eq 'NOTATION') {
 		} else {

--------------040609030903090609060408--




More information about the Po4a-devel mailing list