google-translate-free 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.MD +0 -0
- data/LICENSE +21 -0
- data/README.md +1 -0
- data/lib/translate.rb +118 -0
- data/lib/translate_lib/exception.rb +7 -0
- data/lib/translate_lib/validation.rb +32 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 61878476dbea93fa674c06ec15e5447c79d59534aac01e45767467dde0964186
|
4
|
+
data.tar.gz: 3d46fe34808ad0391f7ddd3d43357fdaacb023270ab3e72e4e32f2a46a6c361b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4b1d3d8c3af406f0b003dc47b9c723363051a149487608b1340b0ffffdcfd9aa7b2e024b96d0389c54b77a7ba079fb49a7da088b3a071f41e36223fd3414b4b7
|
7
|
+
data.tar.gz: c6576a7a50fa7d15f1a9e02b2a404584b638ff547aab17aafdc387e43b682df4324d9a85afc6c49986ad1d8ad3a67307e7050748e975ed72257698ba117fc4da
|
data/CHANGELOG.MD
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) [2022] [datpmt]
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# google-translate-free
|
data/lib/translate.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'net/http'
|
3
|
+
require 'json'
|
4
|
+
require_relative 'translate_lib/validation'
|
5
|
+
|
6
|
+
module Translate
|
7
|
+
def self.translate(string, to_lang, from_lang = :auto)
|
8
|
+
validate_language_code(from_lang, to_lang)
|
9
|
+
encode = encode_url(string)
|
10
|
+
uri = URI("https://translate.googleapis.com/translate_a/t?client=gtx&sl=#{from_lang}&tl=#{to_lang}&dt=t&q=#{encode}")
|
11
|
+
result = net_get(uri)&.first
|
12
|
+
from_lang.to_sym == :auto ? result&.first : result
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.alternate_translations(string, to_lang, from_lang = :auto)
|
16
|
+
validate_language_code(from_lang, to_lang)
|
17
|
+
uri = URI(url(:at, encode_url(string), from_lang, to_lang))
|
18
|
+
result = net_get(uri).dig(5, 0, 2)
|
19
|
+
return if result.nil?
|
20
|
+
|
21
|
+
result.map { |arr| arr[0].downcase }.uniq
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.definitions(keyword)
|
25
|
+
# [ preposition: [desc ex sysnonyms] ]
|
26
|
+
uri = URI(url(:md, keyword, :auto))
|
27
|
+
result = net_get(uri)[12]
|
28
|
+
return if result.nil?
|
29
|
+
|
30
|
+
result.map do |preposition|
|
31
|
+
pre = preposition[0]
|
32
|
+
{
|
33
|
+
"#{pre}": preposition[1].map do |arr|
|
34
|
+
code = arr.last
|
35
|
+
[arr[0].capitalize, examples(keyword, code)&.first, synonyms(pre, keyword, code)]
|
36
|
+
end
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.synonyms(pre, keyword, code)
|
42
|
+
uri = URI(url(:ss, keyword, :auto))
|
43
|
+
result = net_get(uri)[11]
|
44
|
+
return if result.nil?
|
45
|
+
|
46
|
+
rs_response = ''
|
47
|
+
|
48
|
+
result.each do |preposition|
|
49
|
+
rs_response = preposition[1].map { |arr| arr[0] if arr[1] == code }.compact.join(', ') if pre == preposition[0]
|
50
|
+
end
|
51
|
+
|
52
|
+
rs_response == '' ? nil : rs_response
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.examples(keyword, code = nil)
|
56
|
+
uri = URI(url(:ex, keyword, :auto))
|
57
|
+
result = net_get(uri)[13]
|
58
|
+
return unless result
|
59
|
+
|
60
|
+
last_rs = if code.nil?
|
61
|
+
result[0].map { |ex| ex[0] }
|
62
|
+
else
|
63
|
+
result[0].map { |ex| ex[0] if ex.last == code }.compact
|
64
|
+
end
|
65
|
+
last_rs.map do |ex|
|
66
|
+
"#{ex.capitalize}."
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.transliteration(string)
|
71
|
+
encode = encode_url(string)
|
72
|
+
uri = URI(url(:rm, encode, :auto))
|
73
|
+
net_get(uri)&.dig(0, 0, 3)
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.suggest(string)
|
77
|
+
encode = encode_url(string)
|
78
|
+
uri = URI(url(:qca, encode, :auto))
|
79
|
+
net_get(uri)&.dig(7, 1) || string
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.see_more(keyword)
|
83
|
+
# ex [kids -> kid, tables -> table]
|
84
|
+
encode = encode_url(keyword)
|
85
|
+
uri = URI(url(:rw, encode, :auto))
|
86
|
+
net_get(uri)&.dig(14, 0, 0) || keyword
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.detection(string)
|
90
|
+
uri = URI("https://translate.google.com/translate_a/single?client=gtx&sl=auto&q=#{encode_url(string)}")
|
91
|
+
response = net_get(uri)
|
92
|
+
[response&.dig(2), response&.dig(6)]
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.url(dt_value, q_value, from_lang, to_lang = nil)
|
96
|
+
"https://translate.googleapis.com/translate_a/single?client=gtx&sl=#{from_lang}&tl=#{to_lang}&dt=#{dt_value}&q=#{q_value}"
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.encode_url(string)
|
100
|
+
TranslateLib::Validation.validate_string(string)
|
101
|
+
CGI.escape(string)
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.net_get(uri)
|
105
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
106
|
+
http.use_ssl = true
|
107
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
108
|
+
|
109
|
+
response = http.request(request)
|
110
|
+
JSON.parse(response.body)
|
111
|
+
rescue StandardError => e
|
112
|
+
raise StandardError, e.message
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.validate_language_code(from_lang, to_lang)
|
116
|
+
TranslateLib::Validation.validate_language_code(from_lang, to_lang)
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative 'exception'
|
2
|
+
|
3
|
+
module TranslateLib
|
4
|
+
class Validation
|
5
|
+
ALLOW_LANGUAGE_CODE = %w[
|
6
|
+
auto
|
7
|
+
af sq am ar hy as ay az bm eu be bn bho bs
|
8
|
+
bg ca ceb zh-cn zh-tw zh co hr cs da dv doi nl en
|
9
|
+
eo et ee fil fi fr fy gl ka de el gn gu ht
|
10
|
+
ha haw he hi hmn hu is ig ilo id ga it ja jv jw
|
11
|
+
kn kk km rw gom ko kri ku ckb ky lo la lv ln
|
12
|
+
lt lg lb mk mai mg ms ml mt mi mr mni-mtei lus
|
13
|
+
mn my ne no ny or om ps fa pl pt pa qu ro ru
|
14
|
+
sm sa gd nso sr st sn sd si sk sl so es su sw
|
15
|
+
sv tl tg ta tt te th ti ts tr tk ak uk ur ug
|
16
|
+
uz vi cy xh yi yo zu
|
17
|
+
].freeze
|
18
|
+
|
19
|
+
def self.validate_string(string)
|
20
|
+
raise_error!('Translate strings shorter than or equal to 5000 characters.') if string.length > 5000
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.validate_language_code(from_lang, to_lang)
|
24
|
+
raise_error!('from_lang does not have a valid value.') unless ALLOW_LANGUAGE_CODE.include?(from_lang.to_s.downcase)
|
25
|
+
raise_error!('to_lang does not have a valid value.') unless ALLOW_LANGUAGE_CODE.include?(to_lang.to_s.downcase)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.raise_error!(message)
|
29
|
+
TranslateLib::Exception.raise_error!(message)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google-translate-free
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- datpmt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-05-10 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Google Translate Free
|
14
|
+
email: datpmt.2k@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- CHANGELOG.MD
|
20
|
+
- LICENSE
|
21
|
+
- README.md
|
22
|
+
- lib/translate.rb
|
23
|
+
- lib/translate_lib/exception.rb
|
24
|
+
- lib/translate_lib/validation.rb
|
25
|
+
homepage: https://rubygems.org/gems/google-translate-free
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata:
|
29
|
+
source_code_uri: https://github.com/datpmt/google-translate-free
|
30
|
+
changelog_uri: https://github.com/datpmt/google-translate-free/blob/main/CHANGELOG.md
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubygems_version: 3.2.3
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: Google Translate Free
|
50
|
+
test_files: []
|