free_google_translate 0.1 → 0.2.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.
- checksums.yaml +4 -4
- data/.DS_Store +0 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +36 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/free_google_translate.gemspec +36 -0
- data/lib/.DS_Store +0 -0
- data/lib/free_google_translate.rb +67 -0
- data/lib/{languages.rb → free_google_translate/language.rb} +1 -5
- data/lib/free_google_translate/version.rb +3 -0
- metadata +70 -12
- data/lib/google_translate.rb +0 -68
- data/lib/google_translate_error.rb +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bbc788cc842f367813bdbb5c5add68930df9fc9a
|
4
|
+
data.tar.gz: 53b0d14b53dcde7d442478a3a16bb364d3d54a80
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30ae766ac73d547528f1e1ea3dbfa0472191985040b7e0ece9a72aed3c916dd7c8cd9306904b8fd5daf6dd8e524c269bfe63473780ff5051dc38c9acfebf6b88
|
7
|
+
data.tar.gz: a503cc0804865fc1c1146e40014e598edb44808764e2717e13baf5f4ca3451f4360af2147485b9cb9e2b0d9ad5bad96a84edc685e2f26ab4b0c09c54c999cd6a
|
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at TODO: Write your email address. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
## A free and unlimited API for Google Translate without authentication
|
2
|
+
Yes, you can using this gem without any authentication or API key.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'free_google_translate'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install free_google_translate
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
```ruby
|
22
|
+
GoogleTranslate.translate from: "en", to: "vi", text: "hello"
|
23
|
+
```
|
24
|
+
|
25
|
+
Without `from` param, Google Translate will auto detects your source language.
|
26
|
+
```ruby
|
27
|
+
GoogleTranslate.translate to: "vi", text: "hello"
|
28
|
+
```
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nhieu92/free_google_translate. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
33
|
+
|
34
|
+
## License
|
35
|
+
|
36
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "free_google_translate"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "free_google_translate/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "free_google_translate"
|
8
|
+
spec.version = FreeGoogleTranslate::VERSION
|
9
|
+
spec.authors = ["Hieu Nguyen"]
|
10
|
+
spec.email = ["nhieu92@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A free and unlimited API for Google Translate without authentication.}
|
13
|
+
spec.description = %q{A free and unlimited API for Google Translate without authentication or API key.}
|
14
|
+
spec.homepage = "https://github.com/nhieu92/free_google_translate"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
36
|
+
end
|
data/lib/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require "free_google_translate/version"
|
2
|
+
require "free_google_translate/language"
|
3
|
+
|
4
|
+
require "net/http"
|
5
|
+
require "json"
|
6
|
+
|
7
|
+
class GoogleTranslate
|
8
|
+
GOOGLE_TRANSLATE_URI = "https://translate.googleapis.com/translate_a/single".freeze
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def translate params
|
12
|
+
res = make_request build_translate_query params
|
13
|
+
@translated = JSON.parse(res.body).flatten[0]
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def get_source_language params
|
18
|
+
if params[:from]
|
19
|
+
if full_language_name params[:from]
|
20
|
+
params[:from]
|
21
|
+
else
|
22
|
+
raise StandardError, "Source language is invalid"
|
23
|
+
end
|
24
|
+
else
|
25
|
+
"auto"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_target_language params
|
30
|
+
if full_language_name params[:to]
|
31
|
+
params[:to]
|
32
|
+
else
|
33
|
+
raise StandardError, "Target language is invalid"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_text params
|
38
|
+
if params[:text]
|
39
|
+
params[:text]
|
40
|
+
else
|
41
|
+
raise StandardError, "Text can not empty"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def build_translate_query params
|
46
|
+
{
|
47
|
+
client: "gtx",
|
48
|
+
sl: get_source_language(params),
|
49
|
+
tl: get_target_language(params),
|
50
|
+
dt: "t",
|
51
|
+
q: get_text(params),
|
52
|
+
le: "UTF-8",
|
53
|
+
oe: "UTF-8"
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
def make_request query
|
58
|
+
uri = URI GOOGLE_TRANSLATE_URI
|
59
|
+
uri.query = URI.encode_www_form query
|
60
|
+
res = Net::HTTP.get_response uri
|
61
|
+
end
|
62
|
+
|
63
|
+
def full_language_name short_language_name
|
64
|
+
FreeGoogleTranslate::LANGS[short_language_name]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module FreeGoogleTranslate
|
2
2
|
LANGS = {
|
3
3
|
"auto" => "Automatic",
|
4
4
|
"af" => "Afrikaans",
|
@@ -106,8 +106,4 @@ module Languages
|
|
106
106
|
"yo" => "Yoruba",
|
107
107
|
"zu" => "Zulu"
|
108
108
|
}
|
109
|
-
|
110
|
-
def full_language_name short_language_name
|
111
|
-
LANGS[short_language_name]
|
112
|
-
end
|
113
109
|
end
|
metadata
CHANGED
@@ -1,28 +1,86 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: free_google_translate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hieu Nguyen
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-07-
|
12
|
-
dependencies:
|
13
|
-
|
14
|
-
|
11
|
+
date: 2017-07-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: A free and unlimited API for Google Translate without authentication
|
56
|
+
or API key.
|
57
|
+
email:
|
58
|
+
- nhieu92@gmail.com
|
15
59
|
executables: []
|
16
60
|
extensions: []
|
17
61
|
extra_rdoc_files: []
|
18
62
|
files:
|
19
|
-
-
|
20
|
-
-
|
21
|
-
-
|
22
|
-
|
63
|
+
- ".DS_Store"
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- ".travis.yml"
|
67
|
+
- CODE_OF_CONDUCT.md
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- bin/console
|
73
|
+
- bin/setup
|
74
|
+
- free_google_translate.gemspec
|
75
|
+
- lib/.DS_Store
|
76
|
+
- lib/free_google_translate.rb
|
77
|
+
- lib/free_google_translate/language.rb
|
78
|
+
- lib/free_google_translate/version.rb
|
79
|
+
homepage: https://github.com/nhieu92/free_google_translate
|
23
80
|
licenses:
|
24
81
|
- MIT
|
25
|
-
metadata:
|
82
|
+
metadata:
|
83
|
+
allowed_push_host: 'TODO: Set to ''http://mygemserver.com'''
|
26
84
|
post_install_message:
|
27
85
|
rdoc_options: []
|
28
86
|
require_paths:
|
@@ -42,5 +100,5 @@ rubyforge_project:
|
|
42
100
|
rubygems_version: 2.6.12
|
43
101
|
signing_key:
|
44
102
|
specification_version: 4
|
45
|
-
summary:
|
103
|
+
summary: A free and unlimited API for Google Translate without authentication.
|
46
104
|
test_files: []
|
data/lib/google_translate.rb
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
require "net/http"
|
2
|
-
require "google_translate_error"
|
3
|
-
require "json"
|
4
|
-
require "languages"
|
5
|
-
|
6
|
-
class GoogleTranslate
|
7
|
-
include Languages
|
8
|
-
|
9
|
-
attr_reader :from, :to, :text, :uri, :translated
|
10
|
-
|
11
|
-
GOOGLE_TRANSLATE_URI = "https://translate.googleapis.com/translate_a/single".freeze
|
12
|
-
|
13
|
-
def initialize params
|
14
|
-
@from = get_source_language params
|
15
|
-
@to = get_target_language params
|
16
|
-
@text = get_text params
|
17
|
-
@uri = URI GOOGLE_TRANSLATE_URI
|
18
|
-
end
|
19
|
-
|
20
|
-
def translate
|
21
|
-
if @translated
|
22
|
-
return @translated
|
23
|
-
else
|
24
|
-
res = make_request build_query(dt: "t")
|
25
|
-
@translated = JSON.parse(res.body).flatten[0]
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
def get_source_language params
|
31
|
-
params[:from] || "auto"
|
32
|
-
end
|
33
|
-
|
34
|
-
def get_target_language params
|
35
|
-
stripped_target_language = params[:to].to_s.strip
|
36
|
-
if full_language_name stripped_target_language
|
37
|
-
stripped_target_language
|
38
|
-
else
|
39
|
-
raise GoogleTranslateError, "Target language is invalid"
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def get_text params
|
44
|
-
stripped_text = params[:text].to_s.strip
|
45
|
-
if stripped_text.empty?
|
46
|
-
raise GoogleTranslateError, "Text can not empty"
|
47
|
-
else
|
48
|
-
stripped_text
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def build_query dt: "t"
|
53
|
-
{
|
54
|
-
client: "gtx",
|
55
|
-
sl: @from,
|
56
|
-
tl: @to,
|
57
|
-
dt: dt,
|
58
|
-
q: @text,
|
59
|
-
le: "UTF-8",
|
60
|
-
oe: "UTF-8"
|
61
|
-
}
|
62
|
-
end
|
63
|
-
|
64
|
-
def make_request query
|
65
|
-
uri.query = URI.encode_www_form query
|
66
|
-
res = Net::HTTP.get_response uri
|
67
|
-
end
|
68
|
-
end
|