Permalink
Browse files

port from google codes

  • Loading branch information...
0 parents commit 63c6430861a3f619b89899df024d7e641262d580 Ye Dingding committed Dec 16, 2008
Showing with 507 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +10 −0 History.txt
  3. +20 −0 MIT-LICENSE
  4. +11 −0 Manifest.txt
  5. +25 −0 README.txt
  6. +12 −0 Rakefile
  7. +37 −0 bin/rtranslate
  8. +23 −0 lib/rtranslate.rb
  9. +145 −0 lib/rtranslate/language.rb
  10. +137 −0 lib/rtranslate/rtranslate.rb
  11. +39 −0 rtranslate.gemspec
  12. +45 −0 tests/ts_translate.rb
@@ -0,0 +1,3 @@
+.svn
+**/.svn
+**/**/.svn
@@ -0,0 +1,10 @@
+v1.0 Dec 16th, 2008
+* using the Google AJAX Language API instead of scrape
+* add the batch translation support
+* refactor the whole project
+
+v0.2 Nov 16, 2007
+* add a command line tool.
+
+v0.1 Nov 15, 2007
+* initial release
@@ -0,0 +1,20 @@
+Copyright (c) 2007 Dingding Ye
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,11 @@
+History.txt
+MIT-LICENSE
+Manifest.txt
+README.txt
+Rakefile
+bin/rtranslate
+lib/rtranslate.rb
+lib/rtranslate/language.rb
+lib/rtranslate/rtranslate.rb
+rtranslate.gemspec
+tests/ts_translate.rb
@@ -0,0 +1,25 @@
+= google-api-transalte-ruby
+A simple, unofficial, ruby client API for using Google Translate.
+
+== Install
+
+sudo gem install sishen-rtranslate
+
+== Usage
+1.
+{{{
+require "rtranslate"
+result = Translate.t("xxx", Language::English, Language::CHINESE_SIMPLIFIED)
+}}}
+
+2.
+{{{
+$rtranslate -f en -t zh-CN xxx
+}}}
+
+Check test for more examples
+
+== COPYRIGHT
+
+Copyright (c) 2007 Dingding Ye <yedingding@gmail.com>
+Distributed under MIT License
@@ -0,0 +1,12 @@
+require 'rubygems'
+require 'hoe'
+
+Hoe.new('rtranslate', '1.0') do |p|
+ p.rubyforge_name = 'rtranslate'
+ p.author = 'Ye Dingding'
+ p.email = 'yedingding@gmail.com'
+ p.url = 'http://sishen.lifegoo.com'
+ p.summary = 'Google translate ruby client api'
+ p.description = 'A simple, unofficial, ruby client API for using Google Translate.'
+ p.extra_deps = [['hpricot', '>= 0.4'], ['json', '>= 1.1.3']]
+end
@@ -0,0 +1,37 @@
+#!/usr/bin/env ruby
+
+$:.unshift(File::join(File::dirname(File::dirname(__FILE__)), "lib"))
+
+require 'rtranslate'
+require 'optparse'
+
+options = {}
+opts = OptionParser.new do |opts|
+ opts.banner = <<-EOF
+ Usage:
+ rtranslate -f lang -t lang text
+ EOF
+
+ opts.on("-fLANG", "--from LANG", "From Language LANG") do |x|
+ options[:from] = x
+ end
+
+ opts.on("-tLANG", "--to LANG", "To Language LANG") do |x|
+ options[:to] = x
+ end
+
+ opts.on("-h", "--help", "Show this message") do
+ puts opts
+ exit
+ end
+end
+
+opts.parse!(ARGV)
+text = ARGV.shift
+
+if options[:from].nil? || options[:to].nil? || text.empty?
+ puts opts
+ exit
+end
+
+STDOUT.puts(Translate.t(text, options[:from], options[:to]))
@@ -0,0 +1,23 @@
+require File.join(File.dirname(__FILE__), 'rtranslate/language')
+require File.join(File.dirname(__FILE__), 'rtranslate/rtranslate')
+require 'uri'
+require 'open-uri'
+
+begin
+ require 'hpricot'
+rescue LoadError
+ require 'rubygems'
+ require 'hpricot'
+end
+
+$KCODE = 'u'
+
+include Translate
+def Translate.t(text, from, to)
+ begin
+ RTranslate.translate(text, from, to)
+ rescue
+ "Error: " + $!
+ end
+end
+
@@ -0,0 +1,145 @@
+module Google
+ module Language
+ Languages = {
+ 'af' => 'AFRIKAANS',
+ 'sq' => 'ALBANIAN',
+ 'am' => 'AMHARIC',
+ 'ar' => 'ARABIC',
+ 'hy' => 'ARMENIAN',
+ 'az' => 'AZERBAIJANI',
+
+ 'eu' => 'BASQUE',
+ 'be' => 'BELARUSIAN',
+ 'bn' => 'BENGALI',
+ 'bh' => 'BIHARI',
+ 'bg' => 'BULGARIAN',
+ 'my' => 'BURMESE',
+
+ 'ca' => 'CATALAN',
+ 'chr' => 'CHEROKEE',
+ 'zh' => 'CHINESE',
+ 'zh-CN' => 'CHINESE_SIMPLIFIED',
+ 'zh-TW' => 'CHINESE_TRADITIONAL',
+ 'hr' => 'CROATIAN',
+ 'cs' => 'CZECH',
+
+ 'da' => 'DANISH',
+ 'dv' => 'DHIVEHI',
+ 'nl' => 'DUTCH',
+
+ 'en' => 'ENGLISH',
+ 'eo' => 'ESPERANTO',
+ 'et' => 'ESTONIAN',
+
+ 'tl' => 'FILIPINO',
+ 'fi' => 'FINNISH',
+ 'fr' => 'FRENCH',
+
+ 'gl' => 'GALICIAN',
+ 'ka' => 'GEORGIAN',
+ 'de' => 'GERMAN',
+ 'el' => 'GREEK',
+ 'gn' => 'GUARANI',
+ 'gu' => 'GUJARATI',
+
+ 'iw' => 'HEBREW',
+ 'hi' => 'HINDI',
+ 'hu' => 'HUNGARIAN',
+
+ 'is' => 'ICELANDIC',
+ 'id' => 'INDONESIAN',
+ 'iu' => 'INUKTITUT',
+ 'it' => 'ITALIAN',
+
+ 'ja' => 'JAPANESE',
+
+ 'kn' => 'KANNADA',
+ 'kk' => 'KAZAKH',
+ 'km' => 'KHMER',
+ 'ko' => 'KOREAN',
+ 'ku' => 'KURDISH',
+ 'ky' => 'KYRGYZ',
+
+ 'lo' => 'LAOTHIAN',
+ 'lv' => 'LATVIAN',
+ 'lt' => 'LITHUANIAN',
+
+ 'mk' => 'MACEDONIAN',
+ 'ms' => 'MALAY',
+ 'ml' => 'MALAYALAM',
+ 'mt' => 'MALTESE',
+ 'mr' => 'MARATHI',
+ 'mn' => 'MONGOLIAN',
+
+ 'ne' => 'NEPALI',
+ 'no' => 'NORWEGIAN',
+
+ 'or' => 'ORIYA',
+
+ 'ps' => 'PASHTO',
+ 'fa' => 'PERSIAN',
+ 'pl' => 'POLISH',
+ 'pt-PT' => 'PORTUGUESE',
+ 'pa' => 'PUNJABI',
+
+ 'ro' => 'ROMANIAN',
+ 'ru' => 'RUSSIAN',
+
+ 'sa' => 'SANSKRIT',
+ 'sr' => 'SERBIAN',
+ 'sd' => 'SINDHI',
+ 'si' => 'SINHALESE',
+ 'sk' => 'SLOVAK',
+ 'sl' => 'SLOVENIAN',
+ 'es' => 'SPANISH',
+ 'sw' => 'SWAHILI',
+ 'sv' => 'SWEDISH',
+
+ 'tg' => 'TAJIK',
+ 'ta' => 'TAMIL',
+ 'tl' => 'TAGALOG',
+ 'te' => 'TELUGU',
+ 'th' => 'THAI',
+ 'bo' => 'TIBETAN',
+ 'tr' => 'TURKISH',
+
+ 'uk' => 'UKRAINIAN',
+ 'ur' => 'URDU',
+ 'uz' => 'UZBEK',
+ 'ug' => 'UIGHUR',
+
+ 'vi' => 'VIETNAMESE',
+
+ '' => 'UNKNOWN'
+ }
+
+ # judge whether the language is supported by google translate
+ def supported?(language)
+ if Languages.key?(language) || Languages.value?(language.upcase)
+ true
+ else
+ false
+ end
+ end
+ module_function :supported?
+
+ # get the abbrev of the language
+ def abbrev(language)
+ if supported?(language)
+ if Languages.key?(language)
+ language
+ else
+ language.upcase!
+ Languages.each do |k,v|
+ if v == language
+ return k
+ end
+ end
+ end
+ else
+ nil
+ end
+ end
+ module_function :abbrev
+ end
+end
Oops, something went wrong.

0 comments on commit 63c6430

Please sign in to comment.