google-translate 0.7.2 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +5 -1
- data/Rakefile +10 -4
- data/VERSION +1 -1
- data/google-translate.gemspec +27 -20
- data/lib/google_translate.rb +39 -54
- data/spec/spec_helper.rb +2 -2
- data/spec/translate_spec.rb +6 -8
- metadata +86 -84
data/CHANGES
CHANGED
data/Rakefile
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
# Rakefile for google-translate
|
2
2
|
|
3
|
-
require 'rubygems'
|
3
|
+
require 'rubygems' unless RUBY_VERSION =~ /1.9.*/
|
4
|
+
|
4
5
|
require 'rake/gempackagetask'
|
5
6
|
require 'rake/testtask'
|
6
|
-
require '
|
7
|
+
require 'rspec/core/rake_task'
|
7
8
|
require 'rake/rdoctask'
|
8
9
|
require 'rcov/rcovtask'
|
9
10
|
|
@@ -67,8 +68,13 @@ task :"run:gem" do
|
|
67
68
|
puts ruby("#{command}")
|
68
69
|
end
|
69
70
|
|
70
|
-
Spec::Rake::SpecTask.new do |task|
|
71
|
-
task.libs << 'lib'
|
71
|
+
#Spec::Rake::SpecTask.new do |task|
|
72
|
+
# task.libs << 'lib'
|
73
|
+
# task.pattern = 'spec/**/*_spec.rb'
|
74
|
+
# task.verbose = false
|
75
|
+
#end
|
76
|
+
|
77
|
+
RSpec::Core::RakeTask.new do |task|
|
72
78
|
task.pattern = 'spec/**/*_spec.rb'
|
73
79
|
task.verbose = false
|
74
80
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.8.0
|
data/google-translate.gemspec
CHANGED
@@ -1,60 +1,67 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{google-translate}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.8.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = [
|
12
|
-
s.date = %q{
|
11
|
+
s.authors = [%q{Alexander Shvets}]
|
12
|
+
s.date = %q{2011-10-12}
|
13
13
|
s.description = %q{Simple client for Google Translate API.}
|
14
14
|
s.email = %q{alexander.shvets@gmail.com}
|
15
|
-
s.executables = [
|
15
|
+
s.executables = [%q{translate}, %q{t}]
|
16
16
|
s.extra_rdoc_files = [
|
17
17
|
"README"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
"CHANGES",
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
21
|
+
"README",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"bin/t",
|
25
|
+
"bin/t.bat",
|
26
|
+
"bin/translate",
|
27
|
+
"bin/translate.bat",
|
28
|
+
"google-translate.gemspec",
|
29
|
+
"lib/google_translate.rb"
|
30
30
|
]
|
31
31
|
s.homepage = %q{http://github.com/shvets/google-translate}
|
32
|
-
s.
|
33
|
-
s.
|
34
|
-
s.
|
35
|
-
s.rubygems_version = %q{1.3.7}
|
32
|
+
s.require_paths = [%q{lib}]
|
33
|
+
s.requirements = [%q{none}]
|
34
|
+
s.rubygems_version = %q{1.8.6}
|
36
35
|
s.summary = %q{Simple client for Google Translate API (Summary).}
|
37
36
|
s.test_files = [
|
38
37
|
"spec/spec_helper.rb",
|
39
|
-
|
38
|
+
"spec/translate_spec.rb"
|
40
39
|
]
|
41
40
|
|
42
41
|
if s.respond_to? :specification_version then
|
43
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
42
|
s.specification_version = 3
|
45
43
|
|
46
44
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_runtime_dependency(%q<json_pure>, [">= 0"])
|
46
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
47
|
+
s.add_development_dependency(%q<gemcutter>, [">= 0"])
|
47
48
|
s.add_runtime_dependency(%q<json_pure>, [">= 1.1.4"])
|
48
49
|
s.add_runtime_dependency(%q<json_pure>, [">= 0"])
|
49
50
|
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
50
51
|
s.add_development_dependency(%q<gemcutter>, [">= 0"])
|
51
52
|
else
|
53
|
+
s.add_dependency(%q<json_pure>, [">= 0"])
|
54
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
55
|
+
s.add_dependency(%q<gemcutter>, [">= 0"])
|
52
56
|
s.add_dependency(%q<json_pure>, [">= 1.1.4"])
|
53
57
|
s.add_dependency(%q<json_pure>, [">= 0"])
|
54
58
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
55
59
|
s.add_dependency(%q<gemcutter>, [">= 0"])
|
56
60
|
end
|
57
61
|
else
|
62
|
+
s.add_dependency(%q<json_pure>, [">= 0"])
|
63
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
64
|
+
s.add_dependency(%q<gemcutter>, [">= 0"])
|
58
65
|
s.add_dependency(%q<json_pure>, [">= 1.1.4"])
|
59
66
|
s.add_dependency(%q<json_pure>, [">= 0"])
|
60
67
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
data/lib/google_translate.rb
CHANGED
@@ -5,6 +5,7 @@ require 'cgi'
|
|
5
5
|
require 'json'
|
6
6
|
|
7
7
|
module Google
|
8
|
+
|
8
9
|
Language = Struct.new(:name, :code) do
|
9
10
|
def to_s
|
10
11
|
"(" + code + ": " + name + ")"
|
@@ -12,6 +13,8 @@ module Google
|
|
12
13
|
end
|
13
14
|
|
14
15
|
class Translator
|
16
|
+
GOOGLE_TRANSLATE_SERVICE_URL = "http://translate.google.com"
|
17
|
+
|
15
18
|
def self.Exception(*names)
|
16
19
|
cl = Module === self ? self : Object
|
17
20
|
names.each {|n| cl.const_set(n, Class.new(Exception))}
|
@@ -20,79 +23,57 @@ module Google
|
|
20
23
|
Exception :MissingFromLanguage, :MissingToLanguage, :MissingTextLanguage,
|
21
24
|
:TranslateServerIsDown, :InvalidResponse, :MissingText, :MissingTestText
|
22
25
|
|
23
|
-
URL_STRING = "http://ajax.googleapis.com/ajax/services/language/"
|
24
|
-
URL2_STRING = "http://translate.google.com"
|
25
|
-
|
26
26
|
def translate(from, to, from_text, options={})
|
27
27
|
raise(MissingFromLanguage) if from.nil?
|
28
28
|
raise(MissingToLanguage) if to.nil?
|
29
29
|
raise(MissingTextLanguage) if from_text.nil?
|
30
30
|
|
31
|
-
request = URL_STRING + "translate?v=1.0&langpair=#{from}%7C#{to}&q=" + CGI.escape(from_text)
|
32
|
-
|
33
31
|
begin
|
34
|
-
|
32
|
+
url = GOOGLE_TRANSLATE_SERVICE_URL + "/translate_a/t?client=t&text=#{from_text}&hl=#{from}&tl=#{to}"
|
33
|
+
# &sl=auto&multires=1&prev=btn&ssel=0&tsel=4&uptl=ru&alttl=en&sc=1
|
35
34
|
|
36
|
-
|
35
|
+
open(URI.escape(url)) do |stream|
|
36
|
+
content = stream.read
|
37
37
|
|
38
|
-
|
38
|
+
s = content.split(',').collect {|s| s == '' ? "\"\"" : s}.join(",")
|
39
39
|
|
40
|
-
|
40
|
+
result = JSON.parse(s)
|
41
41
|
|
42
|
-
|
43
|
-
rescue OpenURI::HTTPError
|
44
|
-
raise(TranslateServerIsDown)
|
45
|
-
end
|
46
|
-
end
|
42
|
+
raise(TranslateServerIsDown) if (!result || result.empty?)
|
47
43
|
|
48
|
-
|
49
|
-
raise(MissingTestText) if test_text.nil?
|
44
|
+
# raise(InvalidResponse, result["responseDetails"]) if response.code.to_i != 200 # success
|
50
45
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
raise(TranslateServerIsDown) if response.empty?
|
58
|
-
|
59
|
-
response_data
|
60
|
-
rescue OpenURI::HTTPError
|
61
|
-
raise(TranslateServerIsDown)
|
46
|
+
to_text = result[0][0][0]
|
47
|
+
translit = result[0][0][2]
|
48
|
+
(options[:html]) ? CGI.unescapeHTML(translit) : translit
|
49
|
+
end
|
50
|
+
rescue Exception => e
|
51
|
+
raise(TranslateServerIsDown)
|
62
52
|
end
|
63
53
|
end
|
54
|
+
|
55
|
+
# def detect_language(test_text)
|
56
|
+
|
57
|
+
# raise(MissingTestText) if test_text.nil?
|
58
|
+
|
59
|
+
# begin
|
60
|
+
# response = self.class.get("/detect", :query => {:q => test_text})
|
61
|
+
# response = (response && response.parsed_response) ? response.parsed_response : nil
|
62
|
+
# raise(TranslateServerIsDown) if (!response || response.empty?)
|
63
|
+
# response_data = response["responseData"]
|
64
|
+
# return response_data
|
65
|
+
# rescue HTTParty::ResponseError
|
66
|
+
# raise(TranslateServerIsDown)
|
67
|
+
# end
|
68
|
+
#end
|
64
69
|
|
65
70
|
def supported_languages
|
66
|
-
|
71
|
+
fetch_languages(GOOGLE_TRANSLATE_SERVICE_URL , [])
|
67
72
|
end
|
68
73
|
|
69
74
|
private
|
70
75
|
|
71
|
-
def
|
72
|
-
words = str.split('_')
|
73
|
-
|
74
|
-
words[0] + words[1..-1].map {|w| w.capitalize}.join
|
75
|
-
end
|
76
|
-
|
77
|
-
def call_service(request, keys)
|
78
|
-
response = {}
|
79
|
-
|
80
|
-
open(request) do |stream|
|
81
|
-
content = stream.read
|
82
|
-
|
83
|
-
unless content.nil?
|
84
|
-
json = JSON.parse(content)
|
85
|
-
|
86
|
-
keys.each do |key|
|
87
|
-
response[key] = json[camelize(key.to_s)]
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
response
|
93
|
-
end
|
94
|
-
|
95
|
-
def call_service2(request, keys)
|
76
|
+
def fetch_languages(request, keys)
|
96
77
|
response = {}
|
97
78
|
|
98
79
|
open(URI.escape(request)) do |stream|
|
@@ -132,9 +113,13 @@ module Google
|
|
132
113
|
end
|
133
114
|
|
134
115
|
re2 = /<option(\s*)value="([a-z|A-Z|-]*)">([a-z|A-Z|\(|\)|\s]*)<\/option>/
|
135
|
-
|
136
116
|
matches = text.gsub(/selected/i, '').squeeze.scan(re2)
|
137
117
|
|
118
|
+
if matches.size == 0
|
119
|
+
re2 = /<option(\s*)value=([a-z|A-Z|-]*)>([a-z|A-Z|\(|\)|\s]*)<\/option>/
|
120
|
+
matches = text.gsub(/selected/i, '').squeeze.scan(re2)
|
121
|
+
end
|
122
|
+
|
138
123
|
matches.each do |m|
|
139
124
|
languages << Language.new(m[2], m[1])
|
140
125
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/translate_spec.rb
CHANGED
@@ -27,15 +27,13 @@ module Google
|
|
27
27
|
|
28
28
|
it "should translate test string from one language to another" do
|
29
29
|
r = @translator.translate(:en, :ru, "hello world!")
|
30
|
-
|
31
30
|
puts r
|
32
|
-
|
33
31
|
r.size.should be > 0
|
34
32
|
end
|
35
33
|
|
36
|
-
it "should detect the language of a string" do
|
37
|
-
|
38
|
-
end
|
34
|
+
#it "should detect the language of a string" do
|
35
|
+
# @translator.detect_language("bonjour tout le monde")['language'].should be == "fr"
|
36
|
+
#end
|
39
37
|
|
40
38
|
it "should raise an error if no string" do
|
41
39
|
lambda {
|
@@ -43,9 +41,9 @@ module Google
|
|
43
41
|
}.should raise_error
|
44
42
|
end
|
45
43
|
|
46
|
-
it "should return unreliable flag if language is not recognized" do
|
47
|
-
|
48
|
-
end
|
44
|
+
#it "should return unreliable flag if language is not recognized" do
|
45
|
+
# @translator.detect_language("azafretmkldt")['isReliable'].should be_false
|
46
|
+
#end
|
49
47
|
|
50
48
|
it "should return list of supported languages" do
|
51
49
|
languages = @translator.supported_languages
|
metadata
CHANGED
@@ -1,91 +1,102 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-translate
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 7
|
9
|
-
- 2
|
10
|
-
version: 0.7.2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.0
|
5
|
+
prerelease:
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Alexander Shvets
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
date: 2011-10-12 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json_pure
|
16
|
+
requirement: &2156925300 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
+
version_requirements: *2156925300
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: jeweler
|
27
|
+
requirement: &2156924080 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2156924080
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: gemcutter
|
38
|
+
requirement: &2156922920 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2156922920
|
47
|
+
- !ruby/object:Gem::Dependency
|
24
48
|
name: json_pure
|
25
|
-
|
49
|
+
requirement: &2156921920 !ruby/object:Gem::Requirement
|
26
50
|
none: false
|
27
|
-
requirements:
|
28
|
-
- -
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
hash: 27
|
31
|
-
segments:
|
32
|
-
- 1
|
33
|
-
- 1
|
34
|
-
- 4
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
35
54
|
version: 1.1.4
|
36
|
-
requirement: *id001
|
37
|
-
- !ruby/object:Gem::Dependency
|
38
55
|
type: :runtime
|
39
56
|
prerelease: false
|
57
|
+
version_requirements: *2156921920
|
58
|
+
- !ruby/object:Gem::Dependency
|
40
59
|
name: json_pure
|
41
|
-
|
60
|
+
requirement: &2156921080 !ruby/object:Gem::Requirement
|
42
61
|
none: false
|
43
|
-
requirements:
|
44
|
-
- -
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
|
47
|
-
|
48
|
-
- 0
|
49
|
-
version: "0"
|
50
|
-
requirement: *id002
|
51
|
-
- !ruby/object:Gem::Dependency
|
52
|
-
type: :development
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :runtime
|
53
67
|
prerelease: false
|
68
|
+
version_requirements: *2156921080
|
69
|
+
- !ruby/object:Gem::Dependency
|
54
70
|
name: jeweler
|
55
|
-
|
71
|
+
requirement: &2156919920 !ruby/object:Gem::Requirement
|
56
72
|
none: false
|
57
|
-
requirements:
|
58
|
-
- -
|
59
|
-
- !ruby/object:Gem::Version
|
60
|
-
|
61
|
-
segments:
|
62
|
-
- 0
|
63
|
-
version: "0"
|
64
|
-
requirement: *id003
|
65
|
-
- !ruby/object:Gem::Dependency
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
66
77
|
type: :development
|
67
78
|
prerelease: false
|
79
|
+
version_requirements: *2156919920
|
80
|
+
- !ruby/object:Gem::Dependency
|
68
81
|
name: gemcutter
|
69
|
-
|
82
|
+
requirement: &2156918840 !ruby/object:Gem::Requirement
|
70
83
|
none: false
|
71
|
-
requirements:
|
72
|
-
- -
|
73
|
-
- !ruby/object:Gem::Version
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
requirement: *id004
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *2156918840
|
79
91
|
description: Simple client for Google Translate API.
|
80
92
|
email: alexander.shvets@gmail.com
|
81
|
-
executables:
|
93
|
+
executables:
|
82
94
|
- translate
|
83
95
|
- t
|
84
96
|
extensions: []
|
85
|
-
|
86
|
-
extra_rdoc_files:
|
97
|
+
extra_rdoc_files:
|
87
98
|
- README
|
88
|
-
files:
|
99
|
+
files:
|
89
100
|
- CHANGES
|
90
101
|
- README
|
91
102
|
- Rakefile
|
@@ -98,40 +109,31 @@ files:
|
|
98
109
|
- lib/google_translate.rb
|
99
110
|
- spec/spec_helper.rb
|
100
111
|
- spec/translate_spec.rb
|
101
|
-
has_rdoc: true
|
102
112
|
homepage: http://github.com/shvets/google-translate
|
103
113
|
licenses: []
|
104
|
-
|
105
114
|
post_install_message:
|
106
|
-
rdoc_options:
|
107
|
-
|
108
|
-
require_paths:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
109
117
|
- lib
|
110
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
119
|
none: false
|
112
|
-
requirements:
|
113
|
-
- -
|
114
|
-
- !ruby/object:Gem::Version
|
115
|
-
|
116
|
-
|
117
|
-
- 0
|
118
|
-
version: "0"
|
119
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ! '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
125
|
none: false
|
121
|
-
requirements:
|
122
|
-
- -
|
123
|
-
- !ruby/object:Gem::Version
|
124
|
-
|
125
|
-
|
126
|
-
- 0
|
127
|
-
version: "0"
|
128
|
-
requirements:
|
126
|
+
requirements:
|
127
|
+
- - ! '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements:
|
129
131
|
- none
|
130
132
|
rubyforge_project:
|
131
|
-
rubygems_version: 1.
|
133
|
+
rubygems_version: 1.8.6
|
132
134
|
signing_key:
|
133
135
|
specification_version: 3
|
134
136
|
summary: Simple client for Google Translate API (Summary).
|
135
|
-
test_files:
|
137
|
+
test_files:
|
136
138
|
- spec/spec_helper.rb
|
137
139
|
- spec/translate_spec.rb
|