Changeset 1180
- Timestamp:
- 07/29/06 10:38:57
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/plagger/lib/Plagger/Plugin/Filter/Babelfish.pm
r1179 r1180 3 3 use base qw( Plagger::Plugin ); 4 4 5 our $VERSION = '0.03'; 6 5 7 use Plagger::UserAgent; 6 8 use WWW::Babelfish; 9 use Digest::MD5 qw(md5_hex); 10 use Encode qw(encode_utf8); 7 11 8 12 sub register { … … 18 22 19 23 my $service = $self->conf->{service} || 'Babelfish'; 20 my $source = $self->conf->{source} || 'English';21 my $destination = $self->conf->{destination} || 'Japanese';22 24 23 25 my $ua = Plagger::UserAgent->new; … … 31 33 } 32 34 33 my $title = $args->{entry}->title; 34 my $title_tr = $translator->translate( 35 source => $source, 36 destination => $destination, 37 text => $title, 38 delimiter => "\n\n", 39 ); 35 my $title = $args->{entry}->title; 36 my $title_tr = $self->translate($translator, $title); 40 37 unless (defined $title_tr) { 41 38 $context->log(error => "Translation failed: " . $translator->error); … … 48 45 sleep 1; 49 46 50 my $body = $args->{entry}->body; 51 my $body_tr = $translator->translate( 52 source => $source, 53 destination => $destination, 54 text => $body, 55 delimiter => "\n\n", 56 ); 47 my $body = $args->{entry}->body; 48 my $body_tr = $self->translate($translator, $body); 57 49 unless (defined $body_tr) { 58 50 $context->log(error => "Translation failed: " . $translator->error); … … 62 54 63 55 $args->{entry}->body($body_tr); 56 } 57 58 sub translate { 59 my ($self, $translator, $text) = @_; 60 61 my $source = $self->conf->{source} || 'English'; 62 my $destination = $self->conf->{destination} || 'Japanese'; 63 64 my $key = md5_hex(encode_utf8($text)); 65 66 return $self->cache->get_callback( 67 "babelfish:$key", 68 sub { 69 $translator->translate( 70 source => $source, 71 destination => $destination, 72 text => $text, 73 delimiter => "\n\n", 74 ); 75 }, 76 '3 days' 77 ); 64 78 } 65 79
