bdude-rosettastone 0.1.0
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/VERSION.yml +4 -0
- data/lib/rosettastone.rb +52 -0
- data/test/rosettastone_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +65 -0
data/VERSION.yml
ADDED
data/lib/rosettastone.rb
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require 'httparty'
|
|
2
|
+
|
|
3
|
+
# This is the main class that exposes all of the functionality for
|
|
4
|
+
# translating text with the Google Translation API.
|
|
5
|
+
#
|
|
6
|
+
# Please note that attribution to Google is a requirement for using
|
|
7
|
+
# this API.
|
|
8
|
+
#
|
|
9
|
+
# == Language Detection
|
|
10
|
+
# This API implementation supports language detection via the API,
|
|
11
|
+
# however this is adivsed against unless absolutely necessary as although
|
|
12
|
+
# the API generally works as required, it is not always 100% correct.
|
|
13
|
+
class Translator
|
|
14
|
+
|
|
15
|
+
attr_accessor :text, :from_lang, :to_lang
|
|
16
|
+
|
|
17
|
+
include HTTParty
|
|
18
|
+
base_uri 'ajax.googleapis.com/ajax/services/language'
|
|
19
|
+
format :json
|
|
20
|
+
|
|
21
|
+
# Creates a new instance of the Translator class.
|
|
22
|
+
#
|
|
23
|
+
# Parameters:
|
|
24
|
+
# * <tt>text</tt> - The text that is to be translated.
|
|
25
|
+
# * <tt>from_lang</tt> - The language from which the text is to be translated,
|
|
26
|
+
# if missing the Translator will try to guess it via the API.
|
|
27
|
+
def initialize(text, from_lang = nil)
|
|
28
|
+
@text = text
|
|
29
|
+
@from_lang = from_lang
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Translates text to a specified language.
|
|
33
|
+
def translate(to)
|
|
34
|
+
# Do we need to ask Google about the from_lang
|
|
35
|
+
if @from_lang == nil
|
|
36
|
+
@from_lang = detect_language(@text)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
@to_lang = to
|
|
40
|
+
|
|
41
|
+
response = self.class.get('/translate', :query => {:v => "1.0", :q => @text, :langpair => "#{@from_lang}|#{@to_lang}"})
|
|
42
|
+
return response["responseData"]["translatedText"]
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def detect_language(input)
|
|
47
|
+
response = self.class.get('/detect', :query => {:v => "1.0", :q => input})
|
|
48
|
+
return response["responseData"]["language"]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: bdude-rosettastone
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Bryce
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-02-07 00:00:00 -08:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: httparty
|
|
17
|
+
version_requirement:
|
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
19
|
+
requirements:
|
|
20
|
+
- - ">="
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: "0"
|
|
23
|
+
version:
|
|
24
|
+
description: TODO
|
|
25
|
+
email: brycer22@gmail.com
|
|
26
|
+
executables: []
|
|
27
|
+
|
|
28
|
+
extensions: []
|
|
29
|
+
|
|
30
|
+
extra_rdoc_files: []
|
|
31
|
+
|
|
32
|
+
files:
|
|
33
|
+
- VERSION.yml
|
|
34
|
+
- lib/rosettastone.rb
|
|
35
|
+
- test/rosettastone_test.rb
|
|
36
|
+
- test/test_helper.rb
|
|
37
|
+
has_rdoc: true
|
|
38
|
+
homepage: http://github.com/bdude/rosettastone
|
|
39
|
+
post_install_message:
|
|
40
|
+
rdoc_options:
|
|
41
|
+
- --inline-source
|
|
42
|
+
- --charset=UTF-8
|
|
43
|
+
require_paths:
|
|
44
|
+
- lib
|
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - ">="
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: "0"
|
|
50
|
+
version:
|
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: "0"
|
|
56
|
+
version:
|
|
57
|
+
requirements: []
|
|
58
|
+
|
|
59
|
+
rubyforge_project:
|
|
60
|
+
rubygems_version: 1.2.0
|
|
61
|
+
signing_key:
|
|
62
|
+
specification_version: 2
|
|
63
|
+
summary: A ruby interface to the Google Translation API.
|
|
64
|
+
test_files: []
|
|
65
|
+
|