Changeset 1250

Show
Ignore:
Timestamp:
08/09/06 19:23:42
Author:
charsbar
Message:

Filter::Babelfish: autoload Filter::GuessLanguage?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/deps/Filter-Babelfish.yaml

    r1206 r1250  
    33depends:  
    44  WWW::Babelfish: 0 
     5  Locale::Language: 0 
  • trunk/plagger/lib/Plagger/Plugin/Filter/Babelfish.pm

    r1187 r1250  
    88use Encode qw(encode_utf8); 
    99 
     10use Locale::Language; 
     11 
    1012sub register { 
    1113    my($self, $context) = @_; 
     14 
     15    $context->autoload_plugin('Filter::GuessLanguage'); 
    1216    $context->register_hook( 
    1317        $self, 
     18        'plugin.init'        => \&connect_to_babelfish, 
    1419        'update.entry.fixup' => \&update, 
    1520    ); 
     21} 
     22 
     23sub rule_hook { 'update.entry.fixup' } 
     24 
     25sub connect_to_babelfish { 
     26    my($self, $context, $args) = @_; 
     27 
     28    my $service = $self->conf->{service} || 'Babelfish'; 
     29 
     30    $context->log(debug => "hello, $service"); 
     31 
     32    my $ua = Plagger::UserAgent->new; 
     33    $self->{babelfish}->{translator} = new WWW::Babelfish( 
     34        service => $service, 
     35        agent => $ua->agent 
     36    ); 
     37    unless (defined $self->{babelfish}->{translator}) { 
     38        $context->log(error => "$service is not available"); 
     39        return; 
     40    } 
    1641} 
    1742 
     
    1944    my($self, $context, $args) = @_; 
    2045 
    21     my $service = $self->conf->{service} || 'Babelfish'; 
    22  
    23     my $ua = Plagger::UserAgent->new; 
    24     my $translator = new WWW::Babelfish( 
    25         service => $service, 
    26         agent => $ua->agent 
    27     ); 
    28     unless (defined $translator) { 
    29         $context->log(error => "Babelfish is not available"); 
     46    my $translator = $self->{babelfish}->{translator} or return; 
     47    my $language   = $self->conf->{source} || code2language( 
     48        $args->{entry}->{language} || $args->{feed}->language 
     49    ) or do { 
     50        $context->log(warn => "can't identify language"); 
    3051        return; 
    31     } 
     52    }; 
    3253 
    3354    my $title    = $args->{entry}->title; 
    34     my $title_tr = $self->translate($translator, $title); 
     55    my $title_tr = $self->translate($translator, $title, $language); 
    3556    unless (defined $title_tr) { 
    3657        $context->log(error => "Translation failed: " . $translator->error); 
    3758        return; 
    3859    } 
    39     if ($self->conf->{prepend_original}) { 
    40         $title_tr = $title . "\n\n" . $title_tr; 
    41         $context->log(debug => "prepended original title"); 
    42     } 
     60    $title_tr = $title . "\n\n" . $title_tr if $self->conf->{prepend_original}; 
    4361 
    4462    $args->{entry}->title($title_tr); 
     
    4765 
    4866    my $body    = $args->{entry}->body; 
    49     my $body_tr = $self->translate($translator, $body); 
     67    my $body_tr = $self->translate($translator, $body, $language); 
    5068    unless (defined $body_tr) { 
    5169        $context->log(error => "Translation failed: " . $translator->error); 
     
    6179 
    6280sub translate { 
    63     my ($self, $translator, $text) = @_; 
     81    my ($self, $translator, $text, $language) = @_; 
    6482 
    65     my $source      = $self->conf->{source}      || 'English'; 
    66     my $destination = $self->conf->{destination} || 'Japanese'; 
     83    my $destination = $self->conf->{destination} or do { 
     84      Plagger->context->log(error => "set destination language"); 
     85      return; 
     86    }; 
     87 
     88    unless ($translator->languagepairs->{$language}->{$destination}) { 
     89      Plagger->context->log( 
     90          error => "unsupported combination: $language to $destination" 
     91      ); 
     92      return; 
     93    } 
    6794 
    6895    my $key = md5_hex(encode_utf8($text)); 
    6996 
    7097    return $self->cache->get_callback( 
    71         "babelfish:$key", 
     98        "babelfish-$key-$destination", 
    7299        sub { 
    73100            $translator->translate( 
    74                 source => $source, 
     101                source      => $language, 
    75102                destination => $destination, 
    76103                text => $text, 
     
    113140Defaults to 'Babelfish'. 
    114141 
    115 =item source 
     142=item source (optional) 
    116143 
    117 Which language the original entry is. 
    118 Defaults to 'English'. 
     144Which language the feeds/entries are. Will be guessed if you don't specify. 
    119145 
    120146=item destination 
    121147 
    122 Which language the translated entry should be. 
    123 Defaults to 'Japanese'. 
     148Which language the feeds/entries should be translated to. 
    124149 
    125150=item prepend_original 
  • trunk/plagger/t/plugins/Filter-Babelfish/babelfish.t

    r1206 r1250  
    77test_requires_network; 
    88 
    9 plan tests => 6
     9plan tests => 8
    1010 
    1111run_eval_expected; 
     
    7171like $context->update->feeds->[0]->entries->[0]->title, qr/猫.*[cC]at/s; 
    7272like $context->update->feeds->[0]->entries->[0]->body, qr/犬.*[dD]og/s; 
     73 
     74=== Test with Babelfish w GuessLanguage 
     75--- input config 
     76global: 
     77  cache: 
     78    class: Plagger::Cache::Null 
     79  log: 
     80    level: error 
     81plugins: 
     82  - module: Subscription::Config 
     83    config: 
     84      feed: 
     85        - file://$FindBin::Bin/../../samples/babelfish.xml 
     86  - module: Filter::Babelfish 
     87    config: 
     88#      source: Japanese 
     89      destination: English 
     90      service: Google 
     91      prepend_original: 0 
     92--- expected 
     93like $context->update->feeds->[0]->entries->[0]->title, qr/[cC]at/; 
     94like $context->update->feeds->[0]->entries->[0]->body, qr/[dD]og/; 
     95 
  • trunk/plagger/t/samples/babelfish.xml

    r1188 r1250  
    88<link>http://plagger.org/</link> 
    99</item> 
     10<item> 
     11<title>エンコード確定用</title> 
     12<description>日本語のテスト</description> 
     13<link>http://plagger.org/</link> 
     14</item> 
    1015</channel> 
    1116</rss>