User:Pipesmoker/SyntaxHighlighting

From KDE Wiki Sandbox
#!/usr/bin/perl

use Email::MIME;

my $from_language = 'en';
my $to_language = 'he';
my $translation_command = "translate ";
my $languages_pair = "-f $from_language -t $to_language";
my $line;
my $message='';

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

my $email = Email::MIME->new($message);
my @parts = $email->parts;
my $separator = `echo "translation" | $translation_command -f en -t "$to_language" -`;
chop $separator;
rec_parts ($email,@parts);

sub rec_parts
{
    my $parent = $_[0];
    my @parts = $_[1];
    for $part(@parts)
    {
	my @sub_parts = $part->parts;
	if (@sub_parts > 1)
	{
	    rec_parts ($part,@sub_parts);
	}
	my $type=$part->content_type;
	if (( $part->content_type =~ m[text/plain]gi )||( $part->content_type eq ''))
	{
	    my $body = $part->body;
	    
	    $body=~s/['"`]*//g;
	    my $translation_body=`echo "$body" | $translation_command $languages_pair -`;
	    $translation_body=~s/['"`]*//g;
	    my $msg=$body."\n\n---"."$separator"."---\n\n".$translation_body;
	    `kdialog --title "$separator" --msgbox "$msg"`;
	}
    }
}
print $email->as_string;