Recently I've started to play with po4a while trying to
internationalize a bunch of articles in HTML format. One of the major
obstacles for making the job done is this: po4a considers as
translatable (almost) only the text contained between the tags of an
element. This is OK, but sometimes is not enough, since in HTML some
attributes are intended to contain a human-readable text. For
example:
<link rel="stylesheet" type="text/css" href="/new.css"
title= "All-new style of mine">
<a href="/" accesskey="1" title="Go to the project
homepage">
The value of the `title' attribute here should be translatable too.
In fact, HTML.pm already does this by treating the IMG's `alt'
attribute specially and thus making it's value translatable. Maybe
IMG's `alt' is the most common case for an attribute that needs
translation, but there are three other elements that also can contain
`alt': INPUT, AREA and APPLET. They deserve the same treatment as
IMG.
Also, it would be nice if `title' attribute is considered translatable
too (keeping in mind that any element can contain a `title'
attribute).
A patch that solves those two problems is attached.
--- Locale/Po4a/Html.pm.orig 2007-08-04 10:08:46.000000000 +0300
+++ Locale/Po4a/Html.pm 2007-08-05 00:40:18.000000000 +0300
@@ -111,27 +111,29 @@
} elsif ($token->[0] eq 'S') {
push @type,$token->[1];
my $text = get_tag( $token );
- if ( $token->[1] eq 'img' ) {
- my %attr = %{$token->[2]};
- for my $a (qw/title alt/) {
- my $content = $attr{$a};
- if (defined $content) {
- $content = trim($content);
- my $translated = $self->translate(
- $content,
- "FIXME:0",
- "img_$a"
- );
- $attr{$a} = $translated;
- }
- }
- my ($closing) = ( $text =~ /(\s*\/?>)/ );
- # reconstruct the tag from scratch
- delete $attr{'/'}; # Parser thinks closing / in XHTML is an
attribute
- $text = "<img";
- $text .= " $_=\"$attr{$_}\"" foreach keys %attr;
- $text .= $closing;
- }
+ my $tag = $token->[1];
+ my @trans_attr = (( $tag eq 'img' ) || ( $tag eq 'input' ) ||
+ ( $tag eq 'area' ) || ( $tag eq 'applet'))
+ ? qw/title alt/ : qw/title/;
+ my %attr = %{$token->[2]};
+ for my $a (@trans_attr) {
+ my $content = $attr{$a};
+ if (defined $content) {
+ $content = trim($content);
+ my $translated = $self->translate(
+ $content,
+ "FIXME:0",
+ "${tag}_$a"
+ );
+ $attr{$a} = $translated;
+ }
+ }
+ my ($closing) = ( $text =~ /(\s*\/?>)/ );
+ # reconstruct the tag from scratch
+ delete $attr{'/'}; # Parser thinks closing / in XHTML is an attribute
+ $text = "<$tag";
+ $text .= " $_=\"$attr{$_}\"" foreach keys %attr;
+ $text .= $closing;
$self->pushline( $text );
} elsif ($token->[0] eq 'E') {
pop @type;