sermoa-ruby_translate 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ruby_translate.rb +70 -0
- metadata +62 -0
@@ -0,0 +1,70 @@
|
|
1
|
+
# RubyTranslate provides a simple wrapper around the Google Translate API for detecting and translating languages
|
2
|
+
#
|
3
|
+
# Author:: Aimee Daniells (mailto:aimee@edendevelopment.co.uk)
|
4
|
+
# License:: MIT License
|
5
|
+
|
6
|
+
require 'net/http'
|
7
|
+
require 'uri'
|
8
|
+
require 'cgi'
|
9
|
+
require 'json'
|
10
|
+
|
11
|
+
# Uses Google Translate to detect and translate languages.
|
12
|
+
#
|
13
|
+
# Google documentation here:
|
14
|
+
# http://code.google.com/apis/ajaxlanguage/documentation/reference.html#_intro_fonje
|
15
|
+
module RubyTranslate
|
16
|
+
|
17
|
+
# Translate a string from one language to another.
|
18
|
+
# The <tt>from</tt> parameter is optional -
|
19
|
+
# if you don't specify it, detect will be used to guess.
|
20
|
+
#
|
21
|
+
# Examples:
|
22
|
+
#
|
23
|
+
# RubyTranslate.translate("Mein Luftkissenfahrzeug ist voller Aale", "en")
|
24
|
+
# => "My hovercraft is full of eels"
|
25
|
+
#
|
26
|
+
# RubyTranslate.translate("Il y a un singe qui vole dans l'arbre.", "en")
|
27
|
+
# => "There is a flying monkey in the tree."
|
28
|
+
#
|
29
|
+
# RubyTranslate.translate("They are singing in St Peter's Square", "it")
|
30
|
+
# => "Essi sono il canto, in Piazza San Pietro"
|
31
|
+
#
|
32
|
+
# RubyTranslate.translate("Eu queria um outro pedacinho de Apfelstrudel, por favor", "sv", "pt")
|
33
|
+
# => "Jag ville ha en bit av Apfelstrudel, tack"
|
34
|
+
#
|
35
|
+
# Google Translate is not always perfect! Hopefully you already know that!
|
36
|
+
#
|
37
|
+
def self.translate(text, to, from=nil)
|
38
|
+
from = detect(text) if from.nil?
|
39
|
+
json = run('translate', {:langpair => "#{from}|#{to}", :q => text})
|
40
|
+
return json['responseData']['translatedText'] if json['responseStatus'] == 200
|
41
|
+
raise StandardError, json['responseDetails']
|
42
|
+
end
|
43
|
+
|
44
|
+
# Detect a language given a string of text.
|
45
|
+
#
|
46
|
+
# Examples:
|
47
|
+
#
|
48
|
+
# RubyTranslate.detect("Mein Luftkissenfahrzeug ist voller Aale")
|
49
|
+
# => "de"
|
50
|
+
#
|
51
|
+
# RubyTranslate.detect("Safisha viatu yangu mara moja!")
|
52
|
+
# => "sw"
|
53
|
+
#
|
54
|
+
def self.detect(text)
|
55
|
+
json = run('detect', {:q => text})
|
56
|
+
return json['responseData']['language'] if json['responseStatus'] == 200
|
57
|
+
raise StandardError, json['responseDetails']
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def self.run(service, parameters)
|
63
|
+
base = 'http://ajax.googleapis.com/ajax/services/language'
|
64
|
+
parameters.merge!({:v => 1.0})
|
65
|
+
query = parameters.map{|k,v| "#{k}=#{CGI.escape(v.to_s)}"}.join('&')
|
66
|
+
response = Net::HTTP.get_response(URI.parse("#{base}/#{service}?#{query}"))
|
67
|
+
JSON.parse(response.body)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sermoa-ruby_translate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aimee Daniells
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-15 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: json
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.1.3
|
24
|
+
version:
|
25
|
+
description: RubyTranslate provides a simple wrapper around the Google Translate API for detecting and translating languages.
|
26
|
+
email: aimee@edendevelopment.co.uk
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- lib/ruby_translate.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://www.edendevelopment.co.uk
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.2.0
|
58
|
+
signing_key:
|
59
|
+
specification_version: 2
|
60
|
+
summary: An easy way to access the Google Translate API via Ruby.
|
61
|
+
test_files: []
|
62
|
+
|