to_lang 0.0.1
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/.gitignore +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +19 -0
- data/README.textile +39 -0
- data/Rakefile +2 -0
- data/lib/to_lang.rb +19 -0
- data/lib/to_lang/connector.rb +39 -0
- data/lib/to_lang/version.rb +3 -0
- data/to_lang.gemspec +23 -0
- metadata +91 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
to_lang (0.0.1)
|
5
|
+
httparty (~> 0.6.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
crack (0.1.8)
|
11
|
+
httparty (0.6.1)
|
12
|
+
crack (= 0.1.8)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
httparty (~> 0.6.1)
|
19
|
+
to_lang!
|
data/README.textile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
h1. to_lang
|
2
|
+
|
3
|
+
*to_lang* is a Ruby gem that uses the Google Translate API to allow you to perform a translation directly on a string.
|
4
|
+
|
5
|
+
h2. Usage
|
6
|
+
|
7
|
+
To use *to_lang*, require the library, then call @ToLang.start@ with your Google Translate API key to initialize the connector and include the methods in @String@. Then you have access to @String#translate@.
|
8
|
+
|
9
|
+
bq. *translate(to, *options)*
|
10
|
+
_to_ (required): a string indicating the language to translate to, e.g. "es"
|
11
|
+
_options[:from]_ (optional): a string indicating the language of the original string, used when automatic language detection is not accurate
|
12
|
+
|
13
|
+
h2. Examples
|
14
|
+
|
15
|
+
Load and initialize *to_lang*:
|
16
|
+
|
17
|
+
bq. require 'to_lang'
|
18
|
+
ToLang.start('YOUR_GOOGLE_TRANSLATE_API_KEY')
|
19
|
+
|
20
|
+
Translate some text to Spanish:
|
21
|
+
|
22
|
+
bq. "Very cool gem!".translate('es')
|
23
|
+
# "Muy fresco joya!"
|
24
|
+
|
25
|
+
If the source string is of an ambiguous language, the translation might not work as expected. In this case, you can specify the source language explicitly with the @:from@ option:
|
26
|
+
|
27
|
+
bq. "a pie".translate('es')
|
28
|
+
# "a pie"
|
29
|
+
"a pie".translate('es', :from => 'en')
|
30
|
+
# "un pastel"
|
31
|
+
|
32
|
+
h2. Roadmap
|
33
|
+
|
34
|
+
- Add tests for current code. *to_lang* is in its infancy so use at your own risk.
|
35
|
+
- Add automagic sugar methods for translating to specific languages, such as @to_spanish@ and @to_spanish_from_english@
|
36
|
+
|
37
|
+
h2. Feedback and Contributions
|
38
|
+
|
39
|
+
Feedback is appreciated, as are patch contributions. Feel free to fork the project and send me a pull request.
|
data/Rakefile
ADDED
data/lib/to_lang.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.expand_path("../to_lang/connector", __FILE__)
|
2
|
+
|
3
|
+
module ToLang
|
4
|
+
def self.start(key)
|
5
|
+
@@connector = ToLang::Connector.new(key)
|
6
|
+
|
7
|
+
String.send(:include, InstanceMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.connector
|
11
|
+
@@connector
|
12
|
+
end
|
13
|
+
|
14
|
+
module InstanceMethods
|
15
|
+
def translate(target, *args)
|
16
|
+
ToLang.connector.request(self, target, *args)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module ToLang
|
5
|
+
class Connector
|
6
|
+
include HTTParty
|
7
|
+
|
8
|
+
API_URL = "https://www.googleapis.com/language/translate/v2"
|
9
|
+
|
10
|
+
attr_reader :key
|
11
|
+
|
12
|
+
def initialize(key)
|
13
|
+
@key = key
|
14
|
+
end
|
15
|
+
|
16
|
+
def request(q, target, *args)
|
17
|
+
response = self.class.get request_url(q, target, *args)
|
18
|
+
response.parsed_response["data"]["translations"][0]["translatedText"]
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def request_url(q, target, *args)
|
24
|
+
options = extract_options(*args)
|
25
|
+
source = options[:from]
|
26
|
+
url = "#{API_URL}?key=#{@key}&q=#{URI.escape(q)}&target=#{target}"
|
27
|
+
url += "&source=#{source}" if source
|
28
|
+
url
|
29
|
+
end
|
30
|
+
|
31
|
+
def extract_options(*args)
|
32
|
+
if args.last.is_a? Hash
|
33
|
+
args.pop
|
34
|
+
else
|
35
|
+
{}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/to_lang.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "to_lang/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "to_lang"
|
7
|
+
s.version = ToLang::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Jimmy Cuadra"]
|
10
|
+
s.email = ["jimmy@jimmycuadra.com"]
|
11
|
+
s.homepage = "http://github.com/jimmycuadra/to_lang"
|
12
|
+
s.summary = %q{A Ruby client for the Google Translate API built directly into String}
|
13
|
+
s.description = %q{A Ruby client for the Google Translate API built directly into String}
|
14
|
+
|
15
|
+
s.rubyforge_project = "to_lang"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency "httparty", "~> 0.6.1"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: to_lang
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jimmy Cuadra
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-02 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: httparty
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 5
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 6
|
33
|
+
- 1
|
34
|
+
version: 0.6.1
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: A Ruby client for the Google Translate API built directly into String
|
38
|
+
email:
|
39
|
+
- jimmy@jimmycuadra.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- Gemfile.lock
|
50
|
+
- README.textile
|
51
|
+
- Rakefile
|
52
|
+
- lib/to_lang.rb
|
53
|
+
- lib/to_lang/connector.rb
|
54
|
+
- lib/to_lang/version.rb
|
55
|
+
- to_lang.gemspec
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/jimmycuadra/to_lang
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project: to_lang
|
86
|
+
rubygems_version: 1.3.7
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: A Ruby client for the Google Translate API built directly into String
|
90
|
+
test_files: []
|
91
|
+
|