Changeset 1180

Show
Ignore:
Timestamp:
07/29/06 10:38:57
Author:
charsbar
Message:

Filter::Babelfish: now caches translation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/lib/Plagger/Plugin/Filter/Babelfish.pm

    r1179 r1180  
    33use base qw( Plagger::Plugin ); 
    44 
     5our $VERSION = '0.03'; 
     6 
    57use Plagger::UserAgent; 
    68use WWW::Babelfish; 
     9use Digest::MD5 qw(md5_hex); 
     10use Encode qw(encode_utf8); 
    711 
    812sub register { 
     
    1822 
    1923    my $service = $self->conf->{service} || 'Babelfish'; 
    20     my $source  = $self->conf->{source}  || 'English'; 
    21     my $destination = $self->conf->{destination} || 'Japanese'; 
    2224 
    2325    my $ua = Plagger::UserAgent->new; 
     
    3133    } 
    3234 
    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); 
    4037    unless (defined $title_tr) { 
    4138        $context->log(error => "Translation failed: " . $translator->error); 
     
    4845    sleep 1; 
    4946 
    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); 
    5749    unless (defined $body_tr) { 
    5850        $context->log(error => "Translation failed: " . $translator->error); 
     
    6254 
    6355    $args->{entry}->body($body_tr); 
     56} 
     57 
     58sub 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    ); 
    6478} 
    6579