multilanguage e-mails, something I would like to share

Dj YB <[email protected]>
Newsgroups gmane.comp.kde.users.pim
Message-ID <[email protected]>
Hello,

some time ago I have been looking for a way to translate messages inside kmail 
as I am not native english speaker and most of the mails I get are in english.

So, using google translate, tw, perl, and kmail filters, here is a solution:

[note]
if you have no need for messages translation you can stop here
[/note]

create a new filter (lets call it "Translate"), set the filter criteria to 
something that will be always true like "subject" not equal "123123123999".

add an action: "Pipe Through" "~/bin/mail_translate.pl"
in the advanced tab un-check all but "Add this filter to the Apply filter menu"

save the attached file in your ~/bin
edit the script and look for "translate.google.com.en-he", change "en-he" to 
translate from whatever language you want to whatever language you want.

chmod a+x mail_translate.pl

to translate a message:
right click on it and select "Apply Filter"->"Filter Translate"

the script will do one of two things (as far as I planned and tested):
if the message is multipart with a text/plain part, a new text/plain part will 
be added containing the translation of the original text/plain part.

if the message is single part then a "--TRANSLATION--" (in the translated 
language) separator  and the translation of the original message body will be 
added to the end of the original message body.

as always you should not trust strangers offering you scripts, and now is no 
different.
check the code and if you can\want please improve it.

Hope someone finds that useful (except for me),
YB.

_______________________________________________
KDE PIM users mailing list
[email protected]
https://mail.kde.org/mailman/listinfo/kdepim-users
mail_translate.pl (application/x-perl, 1.1 KB)
#!/usr/bin/perl

use Email::MIME;
use Encode;

my $line;
my $message='';
while ($line=<>)
{
    $message.=$line;
}

my $email = Email::MIME->new($message);

my @parts = $email->parts; # These will be Email::MIME objects, too.
my $decoded = $email->body;
my $content_type = $email->content_type;

if (@parts > 1)
{
    for $parsed(@parts)
    {
	my @parts = $parsed->parts; # These will be Email::MIME objects, too.
	my $decoded = $parsed->body;
	my $non_decoded = $parsed->body_raw;
	if ( $parsed->content_type =~ m[text/plain] )
	{
	    my $text_body=$decoded;
	    my $text_raw=$non_decoded;
	    my $text_body=`tw translate.google.com.en-he "$decoded"`;
	    my $single = Email::MIME->create(
		body       => "$text_body",
	    );
	    $single->content_type_set( 'text/plain' );
	    $single->charset_set( 'utf8' );
	    my @list;
	    $list[@list]=$single;
	    $email->parts_add( \@list );
	}
    }
}
else
{
    my $translated=`tw translate.google.com.en-he "$decoded"`;
    if (($translated ne $decoded) && ($translated ne ''))
    {
	$email->body_set($decoded."\n\n--TRANSLATION--\n\n".$translated );
	$email->charset_set( 'utf8' );
    }
}

print $email->as_string;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.