写了个小小的翻译工具

有时候真的是很需要个翻译,特别是个美女翻译。。。最近这几天我似乎一直比较淫荡。。。

这个东西是用ruby写的,是个在线翻译的,用的google的API。本来考虑的是用C语言,可是没找到相关资料学习,同时自己最近主要学习rails,不想再写C语言把自己的思维弄乱了。。。

发现TX有一个用Python实现调用Google翻译,他那时写的时候估计google还只有js的api可以用,不过现在google已经有其他语言可以调用的google翻译API了,返回結果是个JSON,代码很简单:

require 'net/http'
connection = Net::HTTP.new( 'ajax.googleapis.com' )
path= "/ajax/services/language/translate?v=1.0&langpair=es|en&q=gustas"
resp, data = connection.get(path)

将会返回这样一个json

{"responseData": {"translatedText":"likes"}, 
"responseDetails": null, 
"responseStatus": 200}

怎么样,非常方便,其实这样就算是写完了。。。不过为了表现出这是一个小工具,我还是死不要脸的给加了很多脏代码,有兴趣的可以看看,真心的欢迎有人来指导,

#!/usr/bin/ruby

require "net/http"
require 'open-uri'

class GoogleDictionary
  HOST = 'ajax.googleapis.com'
  SERVICE_URL = '/ajax/services/language/translate?v=1.0&'

  attr_accessor :from, :to
  attr_reader :data

  def initialize()
    @connection = Net::HTTP.new(HOST)
    @from = nil
    @to = 'en'
  end

  def translate(query_data = '')
    path =URI.escape(SERVICE_URL + 
                     "q=#{query_data}&langpair=#{@from}|#{@to}")
    resp, data = @connection.get(path)
    @data = eval data.gsub!(':', '=>').gsub!('null', 'nil')
  end

  def result
    if @data['responseStatus'] && @data['responseStatus'] == 200
      @from ||= @data['responseData']['detectedSourceLanguage']
      @data['responseData']['translatedText']
    else
      'MESSAGE: ERROR DETECTED !!!'
    end
  end
end

d = GoogleDictionary.new

case len = ARGV.size
when 0
when 1, 2
  query_data = ARGV[0]
else
  query_data = ARGV[0]
  ARGV[1, len-2].each_with_index do |v, i|
    case v
    when 'from', 'f', '-f'
      d.from = ARGV[i+2]
    when 'to', 't', '-t'
      d.to = ARGV[i+2]
    end
  end
end

if query_data
  d.translate query_data
  puts d.result
  
  puts URI.escape "http://www.google.com/dictionary?aq=f&q="+
    "#{query_data}&langpair=#{d.from}|#{d.to}" if ARGV[-1] == 'd'
else
  puts "MESSAGE: NO WORDS TO TRANSLATE !!!"
end

说一下这个东西的用法,把它保存为一个文件translate.rb,然后就给它执行权限

chmod +x translate.rb

为了自己使用上的方便,在~/.bashrc中加两个alias

alias translate='/path/to/translate.rb'
alias t='translate'

1、默认的情况下是自动检测原来的语言,然后翻译成英語

[mydream@archlinux ~]$ translate 我爱Linux
I love Linux

2、也可以指定翻译成其他语言

[mydream@archlinux ~]$ translate 我爱Linux to ja
私は、Linuxの愛

3、还可以指定源语言

[mydream@archlinux ~]$ translate 我爱Linux from zh to ko
난 리눅스를 사랑

4、另外在最后加一个参数d,代表dictionary,可以同时生成一个到google dictionary的查询链接(注意google的dictionary和translate不是同一个东西),可以点开它在网页上查看詳細情况,包括发音什么的:

[mydream@archlinux ~]$ t internationalization f en t zh d
国际化
http://www.google.com/dictionary?aq=f&q=internationalization&langpair=en%7Czh

5、在4中使用的命令和参数都是translate,from和to的首字母,这样使用起来其实还是非常不错的。。。

小小提示:给自己的终端加个快捷键,这样会比较方便

10 COMMENTS LEAVE

  1. ABitNo
    REPLY ABitNo said on 2009-09-18 at 15:11
    @lotus

    不知道是什么浏览器?
    如果是IE,我完全没有考虑过。。。

  2. lotus
    REPLY lotus said on 2009-09-16 at 05:39

    貌似我用的老爷版本的浏览器打开贵站排版有点有问题诶````

  3. ABitNo
    REPLY ABitNo said on 2009-09-13 at 03:50
    @HicroKee

    stardict的那个坏了,不能用的。
    不管怎么说,我最喜欢我写的这个,非常方便,哈哈

  4. HicroKee
    REPLY HicroKee said on 2009-09-13 at 02:05

    linuxを愛してる。

    话说stardict就能调用~不过DIY大爱~

  5. ABitNo
    REPLY ABitNo said on 2009-09-12 at 05:25
    @kangzj

    这个东西也能淫荡???
    原来一些出自己淫荡之人的代码会同样淫荡的。。。

  6. kangzj
    REPLY kangzj said on 2009-09-12 at 04:08

    这个工具很淫荡,嘻嘻嘻嘻

  7. ABitNo
    REPLY ABitNo said on 2009-09-12 at 02:16
    @xifs

    貌似我一直都在学ruby,这个还真不错。。。

  8. xifs
    REPLY xifs said on 2009-09-11 at 20:12

    又转学ruby了阿?

  9. ABitNo
    REPLY ABitNo said on 2009-09-11 at 08:27
    @soquick

    这想法不错啊,有时间就整一个看看。。。

  10. soquick
    REPLY soquick said on 2009-09-11 at 08:25

    俺还以为命令执行出来一个美女!嘿嘿!~

LEAVE YOUR COMMENTS cancel