带 -n 是屏幕的notify 提示。否则是cli输出,用于irc的bot翻译。无参数时,自动取鼠标选择的文字。带 -1 是为了bot服务的。
代码: 全选
☎ cat sdcv.pl
#!/usr/bin/perl
use Getopt::Long;
# 参数:单行输出选择。屏幕提示输出选择。
GetOptions('1' => \$oneline, 'n'=>\$notify);
my $out,$in;
# 无参数时,使用剪贴板内容。
$in=$ARGV[0]; if(!$in){$in=`xsel -o`;} if(!$in){exit;}
open(SDCV,"sdcv -n $in|");
my $r;
while($l=<SDCV>){
if($l!~/^$/){$r=$l;chomp($r);$r=~s/-->//;}
else{$out="$r --> ";last;}
}
while($l=<SDCV>){
if($l=~/相关|^$/){
close(SDCV);
if($notify){`notify-send -i '/home/exp/媒体/128软件png/pidgin.png' 'sdcv翻译' "$out"`;}
else{print $out;}
exit;
}
chomp($l) if($oneline);
$out.=" ► $l";
}
代码: 全选
☎ cat g-translate.pl
#!/usr/bin/perl
use Getopt::Long;
GetOptions('n'=>\$notify);
my $out,$in,$str;
# 无参数时,使用剪贴板内容。
$in=join('+',@ARGV);if(!$in){$in=`xsel -o`;} if(!$in){exit;}
$in=`echo "$in"|uni2ascii -a J -s`;
$in=~s/ /+/g; $in=~s/["']//g;
chomp $in;
if($in=~/%/){$str="zh-CN%7Cen";}else{$str="en%7Czh-CN";}
$out="curl -e http://www.my-ajax-site.com 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$in&langpair=$str' 2>/dev/null";
$out=`$out`;
$out=~/translatedText":"(.*?)"/;
if($notify){`notify-send 'google翻译' "$1"`;}
else{print $1;}