|
有人习惯用linux系统,习惯用命令行的吗?
动机:经常需要 德 英 中的互翻, 手头软件不方便,加之用惯了命令行
用法:
tr.pl 要查的词 怎么查
怎么查为:
de(德翻英)
dz (德中)
ez (英中)
ed (英德)
代码如下, 用的是google的translate
要先安装REST::Google::Translate;
用cpan
sudo cpan
install REST::Google::Translate
- #! /usr/bin/env perl
- use warnings;
- use strict;
- use REST::Google::Translate;
- use UTF8;
- use Encode;
- my $word;
- my $tt;
- $word = shift;
- $tt = shift;
- my $lp;
- if ($tt eq "ed"){
- $lp = 'en|de';
- }elsif ($tt eq "ez"){
- $lp = 'en|zh';
- }elsif ($tt eq "dz"){
- $lp = 'de|zh';
- }elsif ($tt eq "de"){
- $lp = 'de|en';
- }else{
- $lp = 'en|zh';
- }
- REST::Google::Translate->http_referer('http://example.com');
- my $res = REST::Google::Translate->new(
- q => $word,
- langpair => $lp
- );
- die "response status failure" if $res->responseStatus != 200;
- my $translated = $res->responseData->translatedText;
- binmode STDOUT, ":utf8";
- printf "translation: %s\n", $translated;
复制代码 |
|