google-translate-free 0.0.2 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +117 -1
- data/lib/google-translate-free.rb +17 -9
- data/lib/translate_lib/validation.rb +8 -0
- metadata +7 -5
- data/lib/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d07599110943c837b830702a549267348e5daafc7e6a37c4b0fd8bba7a2419b
|
4
|
+
data.tar.gz: 8bd83c3cf57e6d2c34a9ece07939288d0005205c555513d6e43babe0286a5755
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 872978126e335d6f2c4aab8fbe709ce069aff046fe4499d75d4a5ecbe6f28d832f0ae958c32f7448161d347ac009d912d39d66eaba63b8f5f16854ed8c58105f
|
7
|
+
data.tar.gz: 8670acf54fd88cf533f933532cd8d635f0e6eaa783c32fb39a7a1208cc31acaf3cd801ada3179e679d14e1c798447f800777dfdd411cdda3dded0e47ac10bc9c
|
data/README.md
CHANGED
@@ -1 +1,117 @@
|
|
1
|
-
#
|
1
|
+
# Google Translate Free
|
2
|
+
|
3
|
+
This repository is a robust translation service designed to help you with a variety of language-based features including direct translations, alternate translations, definitions, examples, transliterations, spelling suggestions, language detection, and highly relevant keyword suggestions.
|
4
|
+
|
5
|
+
Let's read the [documents](https://api.datpmt.com) before using it.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'google-translate-free', '~> 1.0'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install google-translate-free
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
To translate
|
25
|
+
```ruby
|
26
|
+
Translate.translate('summer', :vi, :en)
|
27
|
+
# => "mùa hè"
|
28
|
+
```
|
29
|
+
Without third params, Google Translate will auto-detect your source language.
|
30
|
+
```ruby
|
31
|
+
Translate.translate('summer', :vi)
|
32
|
+
# => "mùa hè"
|
33
|
+
```
|
34
|
+
For alternate Translations
|
35
|
+
```ruby
|
36
|
+
Translate.alternate_translations('summer', :vi, :en)
|
37
|
+
# =>
|
38
|
+
#[
|
39
|
+
# "mùa hè",
|
40
|
+
# "mùa hạ",
|
41
|
+
# "hè"
|
42
|
+
#]
|
43
|
+
```
|
44
|
+
Without third params, Google Translate will auto-detect your source language.
|
45
|
+
```ruby
|
46
|
+
Translate.alternate_translations('summer', :vi)
|
47
|
+
# =>
|
48
|
+
#[
|
49
|
+
# "mùa hè",
|
50
|
+
# "mùa hạ",
|
51
|
+
# "hè"
|
52
|
+
#]
|
53
|
+
```
|
54
|
+
For definitions
|
55
|
+
```ruby
|
56
|
+
Translate.definitions('summer')
|
57
|
+
# =>
|
58
|
+
#[
|
59
|
+
# {
|
60
|
+
# :noun=>[
|
61
|
+
# ["The warmest season of the year, in the northern hemisphere from june to august and in the southern hemisphere from december to february.", "<b>summer</b> vacation.", nil],
|
62
|
+
# ["A horizontal bearing beam, especially one supporting joists or rafters.", nil, nil]
|
63
|
+
# ]
|
64
|
+
# },
|
65
|
+
# {
|
66
|
+
# :verb=>[
|
67
|
+
# ["Spend the summer in a particular place.", nil, nil]
|
68
|
+
# ]
|
69
|
+
# }
|
70
|
+
#]
|
71
|
+
```
|
72
|
+
For examples
|
73
|
+
```ruby
|
74
|
+
Translate.examples('summer')
|
75
|
+
# =>
|
76
|
+
#[
|
77
|
+
# "The plant flowers in late <b>summer</b>.",
|
78
|
+
# "The golden <b>summer</b> of her life.",
|
79
|
+
# "A long hot <b>summer</b>.",
|
80
|
+
# "<b>summer</b> vacation."
|
81
|
+
#]
|
82
|
+
```
|
83
|
+
For transliteration
|
84
|
+
```ruby
|
85
|
+
Translate.transliteration('summer')
|
86
|
+
# => "ˈsəmər"
|
87
|
+
```
|
88
|
+
For spelling suggestions
|
89
|
+
```ruby
|
90
|
+
Translate.suggest('summmer')
|
91
|
+
# => "summer"
|
92
|
+
```
|
93
|
+
For language detection
|
94
|
+
```ruby
|
95
|
+
Translate.detection('summer')
|
96
|
+
# => ["en", 0.93254006]
|
97
|
+
```
|
98
|
+
For keyword suggestions
|
99
|
+
```ruby
|
100
|
+
Translate.see_more('summers')
|
101
|
+
# => "summer"
|
102
|
+
```
|
103
|
+
|
104
|
+
## Contributors
|
105
|
+
|
106
|
+
- [datpmt](https://github.com/datpmt)
|
107
|
+
|
108
|
+
I welcome contributions to this project.
|
109
|
+
|
110
|
+
1. Fork it.
|
111
|
+
2. Create your feature branch (`git checkout -b your-feature`).
|
112
|
+
3. Commit your changes (`git commit -am 'Add some feature'`).
|
113
|
+
4. Push to the branch (`git push origin your-feature`).
|
114
|
+
5. Create a new pull request.
|
115
|
+
|
116
|
+
## License
|
117
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -4,6 +4,8 @@ require 'json'
|
|
4
4
|
require_relative 'translate_lib/validation'
|
5
5
|
|
6
6
|
module Translate
|
7
|
+
VERSION = '1.0.1'.freeze
|
8
|
+
|
7
9
|
def self.translate(string, to_lang, from_lang = :auto)
|
8
10
|
validate_language_code(from_lang, to_lang)
|
9
11
|
encode = encode_url(string)
|
@@ -22,6 +24,7 @@ module Translate
|
|
22
24
|
end
|
23
25
|
|
24
26
|
def self.definitions(keyword)
|
27
|
+
validate_keyword(keyword)
|
25
28
|
# [ preposition: [desc ex sysnonyms] ]
|
26
29
|
uri = URI(url(:md, keyword, :auto))
|
27
30
|
result = net_get(uri)[12]
|
@@ -32,7 +35,7 @@ module Translate
|
|
32
35
|
{
|
33
36
|
"#{pre}": preposition[1].map do |arr|
|
34
37
|
code = arr.last
|
35
|
-
[arr[0]
|
38
|
+
[arr[0], examples(keyword, code)&.first, synonyms(pre, keyword, code)]
|
36
39
|
end
|
37
40
|
}
|
38
41
|
end
|
@@ -53,6 +56,7 @@ module Translate
|
|
53
56
|
end
|
54
57
|
|
55
58
|
def self.examples(keyword, code = nil)
|
59
|
+
validate_keyword(keyword)
|
56
60
|
uri = URI(url(:ex, keyword, :auto))
|
57
61
|
result = net_get(uri)[13]
|
58
62
|
return unless result
|
@@ -63,31 +67,31 @@ module Translate
|
|
63
67
|
result[0].map { |ex| ex[0] if ex.last == code }.compact
|
64
68
|
end
|
65
69
|
last_rs.map do |ex|
|
66
|
-
"#{ex
|
70
|
+
"#{ex}."
|
67
71
|
end
|
68
72
|
end
|
69
73
|
|
70
|
-
def self.transliteration(
|
71
|
-
|
72
|
-
uri = URI(url(:rm,
|
74
|
+
def self.transliteration(keyword)
|
75
|
+
validate_keyword(keyword)
|
76
|
+
uri = URI(url(:rm, keyword, :auto))
|
73
77
|
net_get(uri)&.dig(0, 0, 3)
|
74
78
|
end
|
75
79
|
|
76
80
|
def self.suggest(string)
|
77
81
|
encode = encode_url(string)
|
78
|
-
uri = URI(url(:qca, encode, :auto))
|
82
|
+
uri = URI(url(:qca, encode, :auto, detection(string).first == 'en' ? :vi : :en))
|
79
83
|
net_get(uri)&.dig(7, 1) || string
|
80
84
|
end
|
81
85
|
|
82
86
|
def self.see_more(keyword)
|
83
87
|
# ex [kids -> kid, tables -> table]
|
84
|
-
|
85
|
-
uri = URI(url(:rw,
|
88
|
+
validate_keyword(keyword)
|
89
|
+
uri = URI(url(:rw, keyword, :auto))
|
86
90
|
net_get(uri)&.dig(14, 0, 0) || keyword
|
87
91
|
end
|
88
92
|
|
89
93
|
def self.detection(string)
|
90
|
-
uri = URI(
|
94
|
+
uri = URI(url(:rm, encode_url(string), :auto))
|
91
95
|
response = net_get(uri)
|
92
96
|
[response&.dig(2), response&.dig(6)]
|
93
97
|
end
|
@@ -115,4 +119,8 @@ module Translate
|
|
115
119
|
def self.validate_language_code(from_lang, to_lang)
|
116
120
|
TranslateLib::Validation.validate_language_code(from_lang, to_lang)
|
117
121
|
end
|
122
|
+
|
123
|
+
def self.validate_keyword(keyword)
|
124
|
+
TranslateLib::Validation.validate_keyword(keyword)
|
125
|
+
end
|
118
126
|
end
|
@@ -20,11 +20,19 @@ module TranslateLib
|
|
20
20
|
raise_error!('Translate strings shorter than or equal to 5000 characters.') if string.length > 5000
|
21
21
|
end
|
22
22
|
|
23
|
+
def self.validate_keyword(keyword)
|
24
|
+
raise_error!('keyword does not have a valid value.') unless single_word?(keyword)
|
25
|
+
end
|
26
|
+
|
23
27
|
def self.validate_language_code(from_lang, to_lang)
|
24
28
|
raise_error!('from_lang does not have a valid value.') unless ALLOW_LANGUAGE_CODE.include?(from_lang.to_s.downcase)
|
25
29
|
raise_error!('to_lang does not have a valid value.') unless ALLOW_LANGUAGE_CODE.include?(to_lang.to_s.downcase)
|
26
30
|
end
|
27
31
|
|
32
|
+
def self.single_word?(str)
|
33
|
+
!!(str =~ /^\p{L}+$/)
|
34
|
+
end
|
35
|
+
|
28
36
|
def self.raise_error!(message)
|
29
37
|
TranslateLib::Exception.raise_error!(message)
|
30
38
|
end
|
metadata
CHANGED
@@ -1,16 +1,19 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-translate-free
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- datpmt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-08-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: Translation service designed to help you with a variety of language-based
|
14
|
+
features including direct translations, alternate translations, definitions, examples,
|
15
|
+
transliterations, spelling suggestions, language detection, and highly relevant
|
16
|
+
keyword suggestions.
|
14
17
|
email: datpmt.2k@gmail.com
|
15
18
|
executables: []
|
16
19
|
extensions: []
|
@@ -22,7 +25,6 @@ files:
|
|
22
25
|
- lib/google-translate-free.rb
|
23
26
|
- lib/translate_lib/exception.rb
|
24
27
|
- lib/translate_lib/validation.rb
|
25
|
-
- lib/version.rb
|
26
28
|
homepage: https://rubygems.org/gems/google-translate-free
|
27
29
|
licenses:
|
28
30
|
- MIT
|
@@ -47,5 +49,5 @@ requirements: []
|
|
47
49
|
rubygems_version: 3.2.3
|
48
50
|
signing_key:
|
49
51
|
specification_version: 4
|
50
|
-
summary: Google Translate
|
52
|
+
summary: Supports APIs related to Google Translate and more!
|
51
53
|
test_files: []
|
data/lib/version.rb
DELETED