Perl用ブログも始めましたPerl勉強メモ
2009-12-23
■[vim][perl]vimでgoogle翻訳(perl編)

※2009/12/29追記
「'」「"」のエスケープ処理を追記しました。
※2009/12/27追記
setlocal modifiable
を追記しました。
探してみた
vimで翻訳できないかと探してみたら、以下のページがヒットしました。
http://blog.blueblack.net/item_410
でもこれ別ファイルにrubyのスクリプト用意しないといけないんですよねー。ちょっと面倒。
で、たまたま以下のページを見つけて「ワンライナーで出来るのでは?」と。
Google AJAX Language API で翻訳する:楽:勝手Perlリファレンス
http://perl.bayashi.jp/archives/entry/subroutine/get-something/2008/002309.html
初vimscript
上記urlを参考にvimscirptを作ってみました。
vimscriptくせがあって書きずらい…
あとWebService::Simpleが必要なので
cpan WebService::Simple
しておきましょう。
"Google翻訳 function! Google_translate(trn) range setlocal previewheight=6 let tmp = @@ silent normal gvy let tx = @@ let tx = substitute(tx, "\n", ' ', 'g') let tx = substitute(tx, '"', '""', 'g') let tx = substitute(tx, "'", "''", 'g') let dst = "GoogleTranslateOutput" " preview silent execute ":pedit! " . dst wincmd P setlocal buftype=nofile setlocal noswapfile setlocal syntax=none setlocal bufhidden=delete setlocal wrap setlocal modifiable " perl one liner silent execute ':%! perl -MWebService::Simple -e ''exit if @ARGV==0;my $t = WebService::Simple->new(base_url => "http://ajax.googleapis.com/ajax/services/language/translate",response_parser=>"JSON",params=>{v=>"1.0",langpair=>"'.a:trn.'",});my $r=$t->get({q=>$ARGV[0],}); print $r->parse_response->{responseData}->{translatedText};'' ''' . tx . ''' 2>&1 ' wincmd P endfunction "<Leader>teで英語→日本語翻訳 vnoremap <silent> <Leader>te :call Google_translate("en\|ja")<CR> "<Leader>tjで日本語→英語翻訳 vnoremap <silent> <Leader>tj :call Google_translate("ja\|en")<CR>
こいつを.vimrcに入れといて、ビジュアルモードでテキストを選択して
英語→日本語翻訳は「<Leader>te」
日本語→英語翻訳は「<Leader>tj」
で翻訳できます。*1
せっかくなので相互変換できるようにしてみました。
文字コードによっては化けそうな気もしますが。
まあ自分の環境では問題なく変換できてるので良しとします!
コメントを書く
トラックバック - http://d.hatena.ne.jp/toritori0318/20091223/1261582734
リンク元
- 92 http://pipes.yahoo.com/pipes/pipe.info?_id=tDfBdGWF3RGl9XNm1L3fcQ
- 27 http://www.r-labs.org/wiki/r-labs/Estimate_timelog
- 10 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja-JP-mac:official&hs=jRI&q=redmine+wiki+sidebar&btnG=検索&lr=lang_ja&aq=0&oq=redmine+wiki+si
- 7 http://74.125.153.132/search?q=cache:sVwgnl-7GMcJ:d.hatena.ne.jp/toritori0318/20080823/1219500946+mongrel_rails+cluster+自動起動&cd=2&hl=ja&ct=clnk&gl=jp&lr=lang_ja&client=firefox-a
- 4 http://blog.goo.ne.jp/toritori0318/c/f03cc06f5e23052ce39136c3d741b38e
- 4 http://it.blogmura.com/program/
- 4 http://www.google.co.jp/search?hl=ja&q=redmine+予定+実績&sourceid=navclient-ff&rlz=1B5_____jaJP332&ie=UTF-8
- 4 http://www.google.co.jp/search?hl=ja&source=hp&q=xen+インストール&lr=&aq=3&oq=xen
- 4 http://www.google.com/search?hl=ja&lr=lang_ja&ie=UTF-8&oe=UTF-8&q=Redmine+チケット+優先度+色づけ&num=50
- 3 http://twitter.com/mest_bot




