google-cloud-translate 2.3.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +5 -8
  3. data/AUTHENTICATION.md +67 -81
  4. data/LICENSE.md +201 -0
  5. data/MIGRATING.md +302 -0
  6. data/README.md +139 -0
  7. data/lib/google-cloud-translate.rb +5 -146
  8. data/lib/google/cloud/translate.rb +79 -178
  9. data/lib/google/cloud/translate/helpers.rb +107 -0
  10. data/lib/google/cloud/translate/version.rb +6 -2
  11. metadata +38 -138
  12. data/CHANGELOG.md +0 -164
  13. data/CODE_OF_CONDUCT.md +0 -40
  14. data/CONTRIBUTING.md +0 -188
  15. data/LICENSE +0 -201
  16. data/OVERVIEW.md +0 -390
  17. data/TROUBLESHOOTING.md +0 -37
  18. data/lib/google/cloud/translate/v2.rb +0 -169
  19. data/lib/google/cloud/translate/v2/api.rb +0 -255
  20. data/lib/google/cloud/translate/v2/credentials.rb +0 -58
  21. data/lib/google/cloud/translate/v2/detection.rb +0 -132
  22. data/lib/google/cloud/translate/v2/language.rb +0 -68
  23. data/lib/google/cloud/translate/v2/service.rb +0 -209
  24. data/lib/google/cloud/translate/v2/translation.rb +0 -120
  25. data/lib/google/cloud/translate/v3.rb +0 -144
  26. data/lib/google/cloud/translate/v3/credentials.rb +0 -42
  27. data/lib/google/cloud/translate/v3/doc/google/cloud/translate/v3/translation_service.rb +0 -663
  28. data/lib/google/cloud/translate/v3/doc/google/longrunning/operations.rb +0 -51
  29. data/lib/google/cloud/translate/v3/doc/google/protobuf/any.rb +0 -131
  30. data/lib/google/cloud/translate/v3/doc/google/protobuf/timestamp.rb +0 -113
  31. data/lib/google/cloud/translate/v3/doc/google/rpc/status.rb +0 -39
  32. data/lib/google/cloud/translate/v3/translation_service_client.rb +0 -930
  33. data/lib/google/cloud/translate/v3/translation_service_client_config.json +0 -66
  34. data/lib/google/cloud/translate/v3/translation_service_pb.rb +0 -226
  35. data/lib/google/cloud/translate/v3/translation_service_services_pb.rb +0 -68
data/TROUBLESHOOTING.md DELETED
@@ -1,37 +0,0 @@
1
- # Troubleshooting
2
-
3
- ## Where can I get more help?
4
-
5
- ### Ask the Community
6
-
7
- If you have a question about how to use a Google Cloud client library in your
8
- project or are stuck in the Developer's console and don't know where to turn,
9
- it's possible your questions have already been addressed by the community.
10
-
11
- First, check out the appropriate tags on StackOverflow:
12
- - [`google-cloud-platform+ruby+translate`][so-ruby]
13
-
14
- Next, try searching through the issues on GitHub:
15
-
16
- - [`api:translate` issues][gh-search-ruby]
17
-
18
- Still nothing?
19
-
20
- ### Ask the Developers
21
-
22
- If you're experiencing a bug with the code, or have an idea for how it can be
23
- improved, *please* create a new issue on GitHub so we can talk about it.
24
-
25
- - [New issue][gh-ruby]
26
-
27
- Or, you can ask questions on the [Google Cloud Platform Slack][slack-ruby]. You
28
- can use the "ruby" channel for general Ruby questions, or use the
29
- "google-cloud-ruby" channel if you have questions about this gem in particular.
30
-
31
- [so-ruby]: http://stackoverflow.com/questions/tagged/google-cloud-platform+ruby+translate
32
-
33
- [gh-search-ruby]: https://github.com/googlecloudplatform/google-cloud-ruby/issues?q=label%3A%22api%3A+translate%22
34
-
35
- [gh-ruby]: https://github.com/googlecloudplatform/google-cloud-ruby/issues/new
36
-
37
- [slack-ruby]: https://gcp-slack.appspot.com/
@@ -1,169 +0,0 @@
1
- # Copyright 2016 Google LLC
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # https://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "google-cloud-translate"
17
- require "google/cloud/translate/v2/api"
18
- require "google/cloud/config"
19
- require "google/cloud/env"
20
-
21
- module Google
22
- module Cloud
23
- module Translate
24
- ##
25
- # # Google Cloud Translation API
26
- #
27
- # [Google Cloud Translation API](https://cloud.google.com/translation/)
28
- # provides a simple, programmatic interface for translating an arbitrary
29
- # string into any supported language. It is highly responsive, so websites
30
- # and applications can integrate with Translation API for fast, dynamic
31
- # translation of source text. Language detection is also available in cases
32
- # where the source language is unknown.
33
- #
34
- # Translation API supports more than one hundred different languages, from
35
- # Afrikaans to Zulu. Used in combination, this enables translation between
36
- # thousands of language pairs. Also, you can send in HTML and receive HTML
37
- # with translated text back. You don't need to extract your source text or
38
- # reassemble the translated content.
39
- #
40
- # See {file:OVERVIEW.md Translation Overview}.
41
- #
42
- module V2
43
- ##
44
- # Creates a new object for connecting to Cloud Translation API. Each call creates a new connection.
45
- #
46
- # Like other Cloud Platform services, Google Cloud Translation API supports authentication using a project ID
47
- # and OAuth 2.0 credentials. In addition, it supports authentication using a public API access key. (If both the
48
- # API key and the project and OAuth 2.0 credentials are provided, the API key will be used.) Instructions and
49
- # configuration options are covered in the {file:AUTHENTICATION.md Authentication Guide}.
50
- #
51
- # @param [String] project_id Project identifier for the Cloud Translation service you are connecting to. If not
52
- # present, the default project for the credentials is used.
53
- # @param [String, Hash, Google::Auth::Credentials] credentials The path to the keyfile as a String, the contents
54
- # of the keyfile as a Hash, or a Google::Auth::Credentials object. (See {Translate::V2::Credentials})
55
- # @param [String] key a public API access key (not an OAuth 2.0 token)
56
- # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the set of resources and operations that
57
- # the connection can access. See [Using OAuth 2.0 to Access Google
58
- # APIs](https://developers.google.com/identity/protocols/OAuth2).
59
- #
60
- # The default scope is:
61
- #
62
- # * `https://www.googleapis.com/auth/cloud-platform`
63
- # @param [Integer] retries Number of times to retry requests on server error. The default value is `3`.
64
- # Optional.
65
- # @param [Integer] timeout Default timeout to use in requests. Optional.
66
- # @param [String] endpoint Override of the endpoint host name. Optional. If the param is nil, uses the default
67
- # endpoint.
68
- #
69
- # @return [Google::Cloud::Translate::V2::Api]
70
- #
71
- # @example
72
- # require "google/cloud/translate/v2"
73
- #
74
- # translate = Google::Cloud::Translate::V2.new(
75
- # version: :v2,
76
- # project_id: "my-todo-project",
77
- # credentials: "/path/to/keyfile.json"
78
- # )
79
- #
80
- # translation = translate.translate "Hello world!", to: "la"
81
- # translation.text #=> "Salve mundi!"
82
- #
83
- # @example Using API Key.
84
- # require "google/cloud/translate/v2"
85
- #
86
- # translate = Google::Cloud::Translate::V2.new(
87
- # key: "api-key-abc123XYZ789"
88
- # )
89
- #
90
- # translation = translate.translate "Hello world!", to: "la"
91
- # translation.text #=> "Salve mundi!"
92
- #
93
- # @example Using API Key from the environment variable.
94
- # require "google/cloud/translate/v2"
95
- #
96
- # ENV["TRANSLATE_KEY"] = "api-key-abc123XYZ789"
97
- #
98
- # translate = Google::Cloud::Translate::V2.new
99
- #
100
- # translation = translate.translate "Hello world!", to: "la"
101
- # translation.text #=> "Salve mundi!"
102
- #
103
- def self.new project_id: nil, credentials: nil, key: nil, scope: nil, retries: nil, timeout: nil, endpoint: nil
104
- project_id ||= default_project_id
105
- key ||= Google::Cloud.configure.translate.key
106
- retries ||= Google::Cloud.configure.translate.retries
107
- timeout ||= Google::Cloud.configure.translate.timeout
108
- endpoint ||= Google::Cloud.configure.translate.endpoint
109
-
110
- if key
111
- return Google::Cloud::Translate::V2::Api.new(
112
- Google::Cloud::Translate::V2::Service.new(
113
- project_id.to_s, nil, retries: retries, timeout: timeout, key: key, host: endpoint
114
- )
115
- )
116
- end
117
-
118
- scope ||= Google::Cloud.configure.translate.scope
119
- credentials ||= default_credentials scope: scope
120
-
121
- unless credentials.is_a? Google::Auth::Credentials
122
- credentials = Google::Cloud::Translate::V2::Credentials.new credentials, scope: scope
123
- end
124
-
125
- project_id = resolve_project_id project_id, credentials
126
- raise ArgumentError, "project_id is missing" if project_id.empty?
127
-
128
- Google::Cloud::Translate::V2::Api.new(
129
- Google::Cloud::Translate::V2::Service.new(
130
- project_id, credentials, retries: retries, timeout: timeout, host: endpoint
131
- )
132
- )
133
- end
134
-
135
- ##
136
- # @private Default project.
137
- def self.default_project_id
138
- Google::Cloud.configure.translate.project_id ||
139
- Google::Cloud.configure.project_id ||
140
- Google::Cloud.env.project_id
141
- end
142
-
143
- ##
144
- # @private Default credentials.
145
- def self.default_credentials scope: nil
146
- Google::Cloud.configure.translate.credentials ||
147
- Google::Cloud.configure.credentials ||
148
- Google::Cloud::Translate::V2::Credentials.default(scope: scope)
149
- end
150
-
151
- ##
152
- # @private Default configuration.
153
- def self.default_config
154
- Google::Cloud.configure
155
- end
156
-
157
- ##
158
- # @private Resolve project.
159
- def self.resolve_project_id project_id, credentials
160
- # Always cast to a string
161
- return project_id.to_s unless credentials.respond_to? :project_id
162
-
163
- # Always cast to a string
164
- project_id || credentials.project_id.to_s
165
- end
166
- end
167
- end
168
- end
169
- end
@@ -1,255 +0,0 @@
1
- # Copyright 2016 Google LLC
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # https://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "google/cloud/translate/v2/service"
17
- require "google/cloud/translate/v2/translation"
18
- require "google/cloud/translate/v2/detection"
19
- require "google/cloud/translate/v2/language"
20
-
21
- module Google
22
- module Cloud
23
- module Translate
24
- module V2
25
- ##
26
- # # Api
27
- #
28
- # Represents top-level access to the Google Cloud Translation API. Translation API supports more than one
29
- # hundred different languages, from Afrikaans to Zulu. Used in combination, this enables translation between
30
- # thousands of language pairs. Also, you can send in HTML and receive HTML with translated text back. You don't
31
- # need to extract your source text or reassemble the translated content.
32
- #
33
- # @see https://cloud.google.com/translation/docs/getting-started Cloud Translation API Quickstart
34
- #
35
- # @example
36
- # require "google/cloud/translate"
37
- #
38
- # translate = Google::Cloud::Translate.new version: :v2
39
- #
40
- # translation = translate.translate "Hello world!", to: "la"
41
- #
42
- # translation.to_s #=> "Salve mundi!"
43
- #
44
- # translation.from #=> "en"
45
- # translation.origin #=> "Hello world!"
46
- # translation.to #=> "la"
47
- # translation.text #=> "Salve mundi!"
48
- #
49
- class Api
50
- ##
51
- # @private The Service object.
52
- attr_accessor :service
53
-
54
- ##
55
- # @private Creates a new Api instance.
56
- #
57
- # See {Google::Cloud.translate}
58
- def initialize service
59
- @service = service
60
- end
61
-
62
- ##
63
- # The Cloud Translation API project connected to.
64
- #
65
- # @example
66
- # require "google/cloud/translate"
67
- #
68
- # translate = Google::Cloud::Translate.new(
69
- # version: :v2,
70
- # project_id: "my-todo-project",
71
- # credentials: "/path/to/keyfile.json"
72
- # )
73
- #
74
- # translate.project_id #=> "my-todo-project"
75
- #
76
- def project_id
77
- service.project_id
78
- end
79
- alias project project_id
80
-
81
- ##
82
- # Returns text translations from one language to another.
83
- #
84
- # @see https://cloud.google.com/translation/docs/translating-text#Translate Translating Text
85
- #
86
- # @param [String] text The text or texts to translate.
87
- # @param [String] to The target language into which the text should be translated. This is required. The value
88
- # must be an [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code.
89
- # @param [String] from The source language of the text or texts. This is an [ISO
90
- # 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code. This is optional.
91
- # @param [String] format The format of the text. Possible values include `:text` and `:html`. This is
92
- # optional. The Translation API default is `:html`.
93
- # @param [String] model The model used by the service to perform the translation. Can be either `base` to use
94
- # the Phrase-Based Machine Translation (PBMT) model, or `nmt` to use the Neural Machine Translation (NMT)
95
- # model. The default is `nmt`.
96
- #
97
- # If the model is `nmt`, and the requested language translation pair is not supported for the NMT model,
98
- # then the request is translated using the PBMT model.
99
- #
100
- # @param [String] cid The customization id for translate. This is optional.
101
- #
102
- # @return [Translation, Array<Translation>] A single {Translation} object if just one text was given, or an
103
- # array of {Translation} objects if multiple texts were given.
104
- #
105
- # @example
106
- # require "google/cloud/translate"
107
- #
108
- # translate = Google::Cloud::Translate.new version: :v2
109
- #
110
- # translation = translate.translate "Hello world!", to: "la"
111
- #
112
- # translation.to_s #=> "Salve mundi!"
113
- #
114
- # translation.detected? #=> true
115
- # translation.from #=> "en"
116
- # translation.origin #=> "Hello world!"
117
- # translation.to #=> "la"
118
- # translation.text #=> "Salve mundi!"
119
- # translation.model #=> "base"
120
- #
121
- # @example Using the neural machine translation model:
122
- # require "google/cloud/translate"
123
- #
124
- # translate = Google::Cloud::Translate.new version: :v2
125
- #
126
- # translation = translate.translate "Hello world!",
127
- # to: "la", model: "nmt"
128
- #
129
- # translation.to_s #=> "Salve mundi!"
130
- # translation.model #=> "nmt"
131
- #
132
- # @example Setting the `from` language.
133
- # require "google/cloud/translate"
134
- #
135
- # translate = Google::Cloud::Translate.new version: :v2
136
- #
137
- # translation = translate.translate "Hello world!",
138
- # from: :en, to: :la
139
- # translation.detected? #=> false
140
- # translation.text #=> "Salve mundi!"
141
- #
142
- # @example Retrieving multiple translations.
143
- # require "google/cloud/translate"
144
- #
145
- # translate = Google::Cloud::Translate.new version: :v2
146
- #
147
- # translations = translate.translate "Hello my friend.",
148
- # "See you soon.",
149
- # from: "en", to: "la"
150
- # translations.count #=> 2
151
- # translations[0].text #=> "Salve amice."
152
- # translations[1].text #=> "Vide te mox."
153
- #
154
- # @example Preserving HTML tags.
155
- # require "google/cloud/translate"
156
- #
157
- # translate = Google::Cloud::Translate.new version: :v2
158
- #
159
- # translation = translate.translate "<strong>Hello</strong> world!",
160
- # to: :la
161
- # translation.text #=> "<strong>Salve</strong> mundi!"
162
- #
163
- def translate *text, to: nil, from: nil, format: nil, model: nil, cid: nil
164
- return nil if text.empty?
165
- raise ArgumentError, "to is required" if to.nil?
166
- to = to.to_s
167
- from = from.to_s if from
168
- format = format.to_s if format
169
- text = Array(text).flatten
170
- gapi = service.translate text, to: to, from: from, format: format, model: model, cid: cid
171
- Translation.from_gapi_list gapi, text, to, from
172
- end
173
-
174
- ##
175
- # Detect the most likely language or languages of a text or multiple texts.
176
- #
177
- # @see https://cloud.google.com/translation/docs/detecting-language Detecting Language
178
- #
179
- # @param [String] text The text or texts upon which language detection should be performed.
180
- #
181
- # @return [Detection, Array<Detection>] A single {Detection} object if just one text was given, or an array
182
- # of {Detection} objects if multiple texts were given.
183
- #
184
- # @example
185
- # require "google/cloud/translate"
186
- #
187
- # translate = Google::Cloud::Translate.new version: :v2
188
- #
189
- # detection = translate.detect "Hello world!"
190
- # detection.language #=> "en"
191
- # detection.confidence #=> 0.7100697
192
- #
193
- # @example Detecting multiple texts.
194
- # require "google/cloud/translate"
195
- #
196
- # translate = Google::Cloud::Translate.new version: :v2
197
- #
198
- # detections = translate.detect "Hello world!",
199
- # "Bonjour le monde!"
200
- # detections.count #=> 2
201
- # detections.first.language #=> "en"
202
- # detections.first.confidence #=> 0.7100697
203
- # detections.last.language #=> "fr"
204
- # detections.last.confidence #=> 0.40440267
205
- #
206
- def detect *text
207
- return nil if text.empty?
208
- text = Array(text).flatten
209
- gapi = service.detect text
210
- Detection.from_gapi gapi, text
211
- end
212
-
213
- ##
214
- # List the languages supported by the API. These are the languages to and from which text can be translated.
215
- #
216
- # @see https://cloud.google.com/translation/docs/discovering-supported-languages
217
- # Discovering Supported Languages
218
- #
219
- # @param [String] language The language and collation in which the names of the languages are returned. If
220
- # this is `nil` then no names are returned.
221
- #
222
- # @return [Array<Language>] An array of {Language} objects supported by the API.
223
- #
224
- # @example
225
- # require "google/cloud/translate"
226
- #
227
- # translate = Google::Cloud::Translate.new version: :v2
228
- #
229
- # languages = translate.languages
230
- # languages.count #=> 104
231
- #
232
- # english = languages.detect { |l| l.code == "en" }
233
- # english.name #=> nil
234
- #
235
- # @example Get all languages with their names in French.
236
- # require "google/cloud/translate"
237
- #
238
- # translate = Google::Cloud::Translate.new version: :v2
239
- #
240
- # languages = translate.languages "fr"
241
- # languages.count #=> 104
242
- #
243
- # english = languages.detect { |l| l.code == "en" }
244
- # english.name #=> "Anglais"
245
- #
246
- def languages language = nil
247
- language = language.to_s if language
248
- gapi = service.languages language
249
- Array(gapi["languages"]).map { |g| Language.from_gapi g }
250
- end
251
- end
252
- end
253
- end
254
- end
255
- end