kilinrax has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to write a script do the following:
- Read in an .unl file of translations from one language to another
- Read in a consistent-format XML file, and parse it
- Apply the translations to some attributes and text elements in the file
- Write the file back out, with all latin-1 characters escaped in numeric form (e.g 'é' -> 'é'
Currently, I'm trying to do this with XML::Twig, but only the attributes are coming out escaped properly, not the text.
Apply translations to parts of element 'foo':open( TRANSLATIONS, $translations_file ) or die "Could not open '$tran +slations_file': $!"; while( <TRANSLATIONS> ) { if( my ($word, $translation) = split /\|/ ) { push( @translations, { match => qr/\b$word\b/i, replacement => $translation, } ); } } close( TRANSLATIONS ); my $twig= new XML::Twig( twig_handlers => { 'FOO' => \&foo }, pretty_print => 'indented', output_filter => 'safe', ); $twig->parsefile( $xml_file ); $twig->print;
sub foo { my ($t, $elt)= @_; # apply translations to the name, and text of all descendant nodes if( my $name = $elt->att('NAME') ) { $elt->set_att( 'NAME', apply_translations($name) ); } my @descendants = $elt->descendants( 'BAR' ); foreach my $descendant (@descendants) { if( my $text = $descendant->text ) { $descendant->set_text( apply_translations( $text ) ); } } } sub apply_translations { my $text = shift; foreach (@translations) { $text =~ s/$_->{match}/$_->{replacement}/g; } return $text; }
|
---> |
|
I've also tried using XML::SAX to do the same thing, but it barfs on the xml version/encoding line, complaining 'Only ASCII encoding allowed without perl 5.7.2 or higher. You tried: iso-8859-1'.
Unfortunately another application running on this machine (not mine, so don't ask me why) requires perl 5.6.0 or 5.6.1 , so upgrading perl would be problematic at the very least.
I'm somewhat loathe to start hacking on a new version using a different module without being sure it's support for latin-1 is better than those I've tried so far.
Does anyone have any experience of doing anything like this, and have recommendations/advice they'd care to share?