google_translate 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/EXAMPLES +11 -6
- data/lib/google_translate.rb +53 -4
- metadata +1 -1
data/EXAMPLES
CHANGED
@@ -6,11 +6,16 @@
|
|
6
6
|
|
7
7
|
tr = Google::Translate.new
|
8
8
|
|
9
|
-
# from English to
|
10
|
-
tr.translate :from => "en", :to => "
|
11
|
-
=>
|
9
|
+
# from English to German
|
10
|
+
tr.translate :from => "en", :to => "es", :text => "Hello, World!"
|
11
|
+
=> Hallo, Welt!
|
12
|
+
|
13
|
+
# from English to Spanish
|
14
|
+
tr.translate :from => "en", :to => "hi", :text => "Hello, World!"
|
15
|
+
=> Hola, Mundo!
|
16
|
+
|
17
|
+
|
18
|
+
# WWW::Mechanize constructor usage
|
19
|
+
tr = Google::Translate.new { |agent| agent.user_agent_alias = 'Mac Safari' }
|
12
20
|
|
13
|
-
# from English to Italian
|
14
|
-
tr.translate :from => "en", :to => "it", :text => "Hello, World!"
|
15
|
-
=> "Ciao, mondo!"
|
16
21
|
|
data/lib/google_translate.rb
CHANGED
@@ -1,23 +1,72 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'mechanize'
|
3
|
+
require 'iconv'
|
3
4
|
|
4
5
|
module Google
|
5
6
|
class Translate < WWW::Mechanize
|
7
|
+
|
8
|
+
LANG = { :bg => { :in => 'iso-8859-5', :out => 'utf-8', :lang => 'Bulgarian' },
|
9
|
+
:ja => { :in => 'shift-jis', :out => 'utf-8', :lang => 'Japanese' },
|
10
|
+
:ru => { :in => 'koi8-r', :out => 'utf-8', :lang => 'Russian' },
|
11
|
+
:ko => { :in => 'euc-kr', :out => 'utf-8', :lang => 'Korean' },
|
12
|
+
:ar => { :in => 'iso-8859-6', :out => 'utf-8', :lang => 'Arabic' },
|
13
|
+
:hr => { :in => 'iso-8859-2', :out => 'utf-8', :lang => 'Croatian' },
|
14
|
+
:cs => { :in => 'iso-8859-2', :out => 'utf-8', :lang => 'Czech' },
|
15
|
+
:pl => { :in => 'iso-8859-2', :out => 'utf-8', :lang => 'Polish' },
|
16
|
+
:da => { :in => 'iso-8859-1', :out => 'utf-8', :lang => 'Danish' },
|
17
|
+
:nl => { :in => 'iso-8859-1', :out => 'utf-8', :lang => 'Dutch' },
|
18
|
+
:en => { :in => 'iso-8859-1', :out => 'utf-8', :lang => 'English' },
|
19
|
+
:fi => { :in => 'iso-8859-1', :out => 'utf-8', :lang => 'Finnish' },
|
20
|
+
:fr => { :in => 'iso-8859-1', :out => 'utf-8', :lang => 'French' },
|
21
|
+
:de => { :in => 'iso-8859-1', :out => 'utf-8', :lang => 'German' },
|
22
|
+
:it => { :in => 'iso-8859-1', :out => 'utf-8', :lang => 'Italian' },
|
23
|
+
:no => { :in => 'iso-8859-1', :out => 'utf-8', :lang => 'Norwegian' },
|
24
|
+
:es => { :in => 'iso-8859-1', :out => 'utf-8', :lang => 'Spanish' },
|
25
|
+
:pt => { :in => 'iso-8859-1', :out => 'utf-8', :lang => 'Portuguese' },
|
26
|
+
:el => { :in => 'iso-8859-7', :out => 'utf-8', :lang => 'Greek' },
|
27
|
+
:hi => { :in => 'iso-8859-13', :out => 'utf-8', :lang => 'Hindi' },
|
28
|
+
:ro => { :in => 'iso-8859-2', :out => 'utf-8', :lang => 'Romanian' },
|
29
|
+
:"zh-CN" => { :in => 'gb18030', :out => 'utf-8', :lang => 'Chinese Simplified' },
|
30
|
+
:"zh-TW" => { :in => 'bit5', :out => 'utf-8', :lang => 'Chinese Traditional' } }
|
31
|
+
|
32
|
+
URL = "http://translate.google.com/translate_t"
|
33
|
+
|
34
|
+
# Takes source and destination languages, and text.
|
35
|
+
# Returns utf-8 encoded string.
|
36
|
+
# translate(:from => 'en', :to => 'ja', :text => 'Hello, World!')
|
6
37
|
def translate(params={})
|
7
38
|
from = params[:from]
|
8
39
|
to = params[:to]
|
9
40
|
text = params[:text]
|
41
|
+
|
10
42
|
if !text or text.empty?
|
11
43
|
raise ArgumentError, "No text given", caller
|
12
44
|
end
|
13
|
-
|
14
|
-
|
15
|
-
"
|
16
|
-
|
45
|
+
|
46
|
+
if !LANG.key?(to.to_sym)
|
47
|
+
raise ArgumentError, "Invalid 'to' language given", caller
|
48
|
+
end
|
49
|
+
|
50
|
+
if !LANG.key?(from.to_sym)
|
51
|
+
raise ArgumentError, "Invalid 'from' language given", caller
|
52
|
+
end
|
53
|
+
|
54
|
+
result = self.post(URL,
|
55
|
+
"sl" => from,
|
56
|
+
"tl" => to,
|
57
|
+
"text" => text).search("#result_box").inner_html
|
58
|
+
|
59
|
+
Iconv.new(LANG[to.to_sym][:out], LANG[to.to_sym][:in]).iconv(result)
|
17
60
|
end
|
18
61
|
|
19
62
|
def new(*args)
|
20
63
|
super
|
21
64
|
end
|
65
|
+
|
66
|
+
# Returns the languages' names, their abbreviations and Google encoding.
|
67
|
+
def self.languages
|
68
|
+
langs = LANG.sort_by{ |e| e[1][:lang] }
|
69
|
+
langs.each{ |e| print e[0], "\t", e[1][:lang], "\n" }
|
70
|
+
end
|
22
71
|
end
|
23
72
|
end
|