Created
March 27, 2011 02:01
-
-
Save eggman/888843 to your computer and use it in GitHub Desktop.
Plagger::Plugin::Filter::GoogleTranslate - translate via Google Translate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Plagger::Plugin::Filter::GoogleTranslate; | |
use strict; | |
use base qw( Plagger::Plugin ); | |
use Lingua::Translate; | |
use Digest::MD5 qw(md5_hex); | |
use Encode qw(encode_utf8); | |
use Locale::Language; | |
sub register { | |
my($self, $context) = @_; | |
$context->register_hook( | |
$self, | |
'plugin.init' => \&connect_to_GoogleTranslate, | |
'update.entry.fixup' => \&update, | |
); | |
} | |
sub rule_hook { 'update.entry.fixup' } | |
sub connect_to_GoogleTranslate { | |
my($self, $context, $args) = @_; | |
Lingua::Translate::config | |
( | |
back_end => 'Google', | |
referer => $self->conf->{referer}, | |
format => 'text', | |
userip => '192.168.1.1', | |
); | |
$self->{translator} = Lingua::Translate::Google->new | |
( | |
src => $self->conf->{source}, | |
dest => $self->conf->{destination}, | |
); | |
unless (defined $self->{translator}) { | |
$context->log(error => "Google Translate is not available"); | |
return; | |
} | |
} | |
sub update { | |
my($self, $context, $args) = @_; | |
my $translator = $self->{translator} or return; | |
my $title = $args->{entry}->title->data; | |
my $title_tr = $translator->translate($translator, $title); | |
unless (defined $title_tr) { | |
$context->log(error => "Translation failed: " . $translator->error); | |
return; | |
} | |
$title_tr = $title . "\n\n" . $title_tr if $self->conf->{prepend_original}; | |
$args->{entry}->title($title_tr); | |
my $body = $args->{entry}->body->data; | |
my $body_tr = $self->translate($translator, $body); | |
unless (defined $body_tr) { | |
$context->log(error => "Translation failed: " . $translator->error); | |
return; | |
} | |
if ($self->conf->{prepend_original}) { | |
$body_tr = $body . "\n\n" . $body_tr; | |
$context->log(debug => "prepended original body"); | |
} | |
$args->{entry}->body($body_tr); | |
} | |
sub translate { | |
my ($self, $translator, $text) = @_; | |
my $destination = $self->conf->{destination} or do { | |
Plagger->context->log(error => "set destination language"); | |
return; | |
}; | |
my $key = md5_hex(encode_utf8($text)); | |
return $self->cache->get_callback( | |
"google-translate-$key-$destination", | |
sub { | |
$translator->translate($text); | |
}, | |
'3 days' | |
); | |
} | |
1; | |
__END__ | |
=head1 NAME | |
Plagger::Plugin::Filter::GoogleTranslate - translate via use Lingua::Translate::GoogleTranslate | |
=head1 SYNOPSIS | |
- module: Filter::GoogleTranslate | |
config: | |
source: en | |
destination: jp | |
prepend_original: 0 | |
=head1 DESCRIPTION | |
This plugin translates each entry body via Google Translate. | |
See L<Lingua::Translate::GoogleTranslate> for details. | |
=head1 CONFIG | |
=over 4 | |
=item source (optional) | |
Which language the feeds/entries are. Will be guessed if you don't specify. | |
=item destination | |
Which language the feeds/entries should be translated to. | |
=item prepend_original | |
When set to 1, prepends original entry body. Defaults to 0. | |
=back | |
=head1 AUTHOR | |
eggman | |
=head1 SEE ALSO | |
L<Plagger>, L<Lingua::Translate::GoogleTranslate> | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks https://translate.how/ will view it.