google-cloud-translate 2.3.0 → 3.0.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 +203 -0
  5. data/MIGRATING.md +302 -0
  6. data/README.md +80 -0
  7. data/lib/google-cloud-translate.rb +5 -146
  8. data/lib/google/cloud/translate.rb +81 -178
  9. data/lib/google/cloud/translate/helpers.rb +100 -0
  10. data/lib/google/cloud/translate/version.rb +6 -2
  11. metadata +35 -135
  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
@@ -1,390 +0,0 @@
1
- # Google Cloud Translation API
2
-
3
- [Google Cloud Translation API](https://cloud.google.com/translation/)
4
- provides a simple, programmatic interface for translating an arbitrary
5
- string into any supported language. It is highly responsive, so websites
6
- and applications can integrate with Translation API for fast, dynamic
7
- translation of source text. Language detection is also available in cases
8
- where the source language is unknown.
9
-
10
- Translation API supports more than one hundred different languages, from
11
- Afrikaans to Zulu. Used in combination, this enables translation between
12
- thousands of language pairs. Also, you can send in HTML and receive HTML
13
- with translated text back. You don't need to extract your source text or
14
- reassemble the translated content.
15
-
16
- The google-cloud-translate 2.0 gem contains a generated v3 client and a legacy hand-written v2 client.
17
- To use the legacy v2 client, call {Google::Cloud::Translate.new} and specify `version: :v2`.
18
- See [Migrating to Translation v3](https://cloud.google.com/translate/docs/migrate-to-v3) for details regarding differences between v2 and v3.
19
-
20
- ## Authenticating
21
-
22
- Like other Cloud Platform services, Google Cloud Translation API supports
23
- authentication using a project ID and OAuth 2.0 credentials. In addition,
24
- it supports authentication using a public API access key. (If both the API
25
- key and the project and OAuth 2.0 credentials are provided, the API key
26
- will be used.) Instructions and configuration options are covered in the
27
- {file:AUTHENTICATION.md Authentication Guide}.
28
-
29
- ## Using the v3 client
30
-
31
- The Cloud Translation API v3 includes several new features and updates:
32
-
33
- * Glossaries - Create a custom dictionary to correctly and consistently translate terms that are customer-specific.
34
- * Batch requests - Make an asynchronous request to translate large amounts of text.
35
- * AutoML models - Cloud Translation adds support for translating text with custom models that you create using AutoML Translation.
36
- * Labels - The Cloud Translation API supports adding user-defined labels (key-value pairs) to requests.
37
-
38
- ### Translating texts
39
-
40
- Cloud Translation v3 introduces support for translating text using custom AutoML Translation models, and for creating glossaries to ensure that the Cloud Translation API translates a customer's domain-specific terminology correctly.
41
-
42
- Performing a default translation:
43
-
44
- ```ruby
45
- require "google/cloud/translate"
46
-
47
- client = Google::Cloud::Translate.new
48
-
49
- project_id = "my-project-id"
50
- location_id = "us-central1"
51
-
52
- # The content to translate in string format
53
- contents = ["Hello, world!"]
54
- # Required. The BCP-47 language code to use for translation.
55
- target_language = "fr"
56
- parent = client.class.location_path project_id, location_id
57
-
58
- response = client.translate_text contents, target_language, parent
59
-
60
- # Display the translation for each input text provided
61
- response.translations.each do |translation|
62
- puts "Translated text: #{translation.translated_text}"
63
- end
64
- ```
65
-
66
- To use AutoML custom models you enable the [AutoML API](automl.googleapis.com) for your project before translating as follows:
67
-
68
- ```ruby
69
- require "google/cloud/translate"
70
-
71
- client = Google::Cloud::Translate.new
72
-
73
- project_id = "my-project-id"
74
- location_id = "us-central1"
75
- model_id = "my-automl-model-id"
76
-
77
- # The `model` type requested for this translation.
78
- model = "projects/#{project_id}/locations/#{location_id}/models/#{model_id}"
79
- # The content to translate in string format
80
- contents = ["Hello, world!"]
81
- # Required. The BCP-47 language code to use for translation.
82
- target_language = "fr"
83
- # Optional. The BCP-47 language code of the input text.
84
- source_language = "en"
85
- # Optional. Can be "text/plain" or "text/html".
86
- mime_type = "text/plain"
87
- parent = client.class.location_path project_id, location_id
88
-
89
- response = client.translate_text contents, target_language, parent,
90
- source_language_code: source_language, model: model, mime_type: mime_type
91
-
92
- # Display the translation for each input text provided
93
- response.translations.each do |translation|
94
- puts "Translated text: #{translation.translated_text}"
95
- end
96
- ```
97
-
98
- To use a glossary you need to create a Google Cloud Storage bucket and grant your service account access to it before translating as follows:
99
-
100
- ```ruby
101
- require "google/cloud/translate"
102
-
103
- client = Google::Cloud::Translate.new
104
-
105
- project_id = "my-project-id"
106
- location_id = "us-central1"
107
- glossary_id = "my-glossary-id"
108
-
109
- # The content to translate in string format
110
- contents = ["Hello, world!"]
111
- # Required. The BCP-47 language code to use for translation.
112
- target_language = "fr"
113
- # Optional. The BCP-47 language code of the input text.
114
- source_language = "en"
115
- glossary_config = {
116
- # Specifies the glossary used for this translation.
117
- glossary: client.class.glossary_path(project_id, location_id, glossary_id)
118
- }
119
- # Optional. Can be "text/plain" or "text/html".
120
- mime_type = "text/plain"
121
- parent = client.class.location_path project_id, location_id
122
-
123
- response = client.translate_text contents, target_language, parent,
124
- source_language_code: source_language, glossary_config: glossary_config, mime_type: mime_type
125
-
126
- # Display the translation for each input text provided
127
- response.translations.each do |translation|
128
- puts "Translated text: #{translation.translated_text}"
129
- end
130
- ```
131
-
132
- ### Batch translating texts
133
-
134
- Batch translation allows you to translate large amounts of text (with a limit of 1,000 files per batch), and to up to 10 different target languages.
135
- Batch translation also supports AutoML models and glossaries.
136
- To make batch requests you need to create a Google Cloud Storage bucket and grant your service account access to it before translating as follows:
137
-
138
- ```ruby
139
- require "google/cloud/translate"
140
-
141
- client = Google::Cloud::Translate.new
142
-
143
- input_uri = "gs://cloud-samples-data/text.txt"
144
- output_uri = "gs://my-bucket-id/path_to_store_results/"
145
- project_id = "my-project-id"
146
- location_id = "us-central1"
147
- source_lang = "en"
148
- target_lang = "ja"
149
-
150
- input_config = {
151
- gcs_source: {
152
- input_uri: input_uri
153
- },
154
- # Optional. Can be "text/plain" or "text/html".
155
- mime_type: "text/plain"
156
- }
157
- output_config = {
158
- gcs_destination: {
159
- output_uri_prefix: output_uri
160
- }
161
- }
162
- parent = client.class.location_path project_id, location_id
163
-
164
- operation = client.batch_translate_text \
165
- parent, source_lang, [target_lang], [input_config], output_config
166
-
167
- # Wait until the long running operation is done
168
- operation.wait_until_done!
169
-
170
- response = operation.response
171
-
172
- puts "Total Characters: #{response.total_characters}"
173
- puts "Translated Characters: #{response.translated_characters}"
174
- ```
175
-
176
- ### Detecting languages
177
-
178
- You can detect the language of a text string:
179
-
180
- ```ruby
181
- require "google/cloud/translate"
182
-
183
- client = Google::Cloud::Translate.new
184
-
185
- project_id = "my-project-id"
186
- location_id = "us-central1"
187
- # The text string for performing language detection
188
- content = "Hello, world!"
189
- # Optional. Can be "text/plain" or "text/html".
190
- mime_type = "text/plain"
191
-
192
- parent = client.class.location_path project_id, location_id
193
-
194
- response = client.detect_language parent, content: content, mime_type: mime_type
195
-
196
- # Display list of detected languages sorted by detection confidence.
197
- # The most probable language is first.
198
- response.languages.each do |language|
199
- # The language detected
200
- puts "Language Code: #{language.language_code}"
201
- # Confidence of detection result for this language
202
- puts "Confidence: #{language.confidence}"
203
- end
204
- ```
205
-
206
- ### Listing supported languages
207
-
208
- You can discover the [supported languages](https://cloud.google.com/translate/docs/languages) of the v3 API:
209
-
210
- ```ruby
211
- require "google/cloud/translate"
212
-
213
- client = Google::Cloud::Translate.new
214
-
215
- project_id = "my-project-id"
216
- location_id = "us-central1"
217
-
218
- parent = client.class.location_path project_id, location_id
219
-
220
- response = client.get_supported_languages parent
221
-
222
- # List language codes of supported languages
223
- response.languages.each do |language|
224
- puts "Language Code: #{language.language_code}"
225
- end
226
- ```
227
-
228
- ## Using the legacy v2 client
229
-
230
- ### Translating texts
231
-
232
- Translating text from one language to another is easy (and extremely
233
- fast.) The only required arguments to
234
- {Google::Cloud::Translate::V2::Api#translate} are a string and the [ISO
235
- 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) code of the
236
- language to which you wish to translate.
237
-
238
- ```ruby
239
- require "google/cloud/translate"
240
-
241
- translate = Google::Cloud::Translate.new version: :v2
242
-
243
- translation = translate.translate "Hello world!", to: "la"
244
-
245
- puts translation #=> Salve mundi!
246
-
247
- translation.from #=> "en"
248
- translation.origin #=> "Hello world!"
249
- translation.to #=> "la"
250
- translation.text #=> "Salve mundi!"
251
- ```
252
-
253
- You may want to use the `from` option to specify the language of the
254
- source text, as the following example illustrates. (Single words do not
255
- give Translation API much to work with.)
256
-
257
- ```ruby
258
- require "google/cloud/translate"
259
-
260
- translate = Google::Cloud::Translate.new version: :v2
261
-
262
- translation = translate.translate "chat", to: "en"
263
-
264
- translation.detected? #=> true
265
- translation.from #=> "en"
266
- translation.text #=> "chat"
267
-
268
- translation = translate.translate "chat", from: "fr", to: "en"
269
-
270
- translation.detected? #=> false
271
- translation.from #=> "fr"
272
- translation.text #=> "cat"
273
- ```
274
-
275
- You can pass multiple texts to {Google::Cloud::Translate::V2::Api#translate}.
276
-
277
- ```ruby
278
- require "google/cloud/translate"
279
-
280
- translate = Google::Cloud::Translate.new version: :v2
281
-
282
- translations = translate.translate "chien", "chat", from: "fr", to: "en"
283
-
284
- translations.size #=> 2
285
- translations[0].origin #=> "chien"
286
- translations[0].text #=> "dog"
287
- translations[1].origin #=> "chat"
288
- translations[1].text #=> "cat"
289
- ```
290
-
291
- By default, any HTML in your source text will be preserved.
292
-
293
- ```ruby
294
- require "google/cloud/translate"
295
-
296
- translate = Google::Cloud::Translate.new version: :v2
297
-
298
- translation = translate.translate "<strong>Hello</strong> world!",
299
- to: :la
300
- translation.text #=> "<strong>Salve</strong> mundi!"
301
- ```
302
-
303
- ### Detecting languages
304
-
305
- You can use {Google::Cloud::Translate::V2::Api#detect} to see which language
306
- the Translation API ranks as the most likely source language for a text.
307
- The `confidence` score is a float value between `0` and `1`.
308
-
309
- ```ruby
310
- require "google/cloud/translate"
311
-
312
- translate = Google::Cloud::Translate.new version: :v2
313
-
314
- detection = translate.detect "chat"
315
-
316
- detection.text #=> "chat"
317
- detection.language #=> "en"
318
- detection.confidence #=> 0.59922177
319
- ```
320
-
321
- You can pass multiple texts to {Google::Cloud::Translate::V2::Api#detect}.
322
-
323
- ```ruby
324
- require "google/cloud/translate"
325
-
326
- translate = Google::Cloud::Translate.new version: :v2
327
-
328
- detections = translate.detect "chien", "chat"
329
-
330
- detections.size #=> 2
331
- detections[0].text #=> "chien"
332
- detections[0].language #=> "fr"
333
- detections[0].confidence #=> 0.7109375
334
- detections[1].text #=> "chat"
335
- detections[1].language #=> "en"
336
- detections[1].confidence #=> 0.59922177
337
- ```
338
-
339
- ### Listing supported languages
340
-
341
- Translation API adds new languages frequently. You can use
342
- {Google::Cloud::Translate::V2::Api#languages} to query the list of supported
343
- languages.
344
-
345
- ```ruby
346
- require "google/cloud/translate"
347
-
348
- translate = Google::Cloud::Translate.new version: :v2
349
-
350
- languages = translate.languages
351
-
352
- languages.size #=> 104
353
- languages[0].code #=> "af"
354
- languages[0].name #=> nil
355
- ```
356
-
357
- To receive the names of the supported languages, as well as their [ISO
358
- 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) codes,
359
- provide the code for the language in which you wish to receive the names.
360
-
361
- ```ruby
362
- require "google/cloud/translate"
363
-
364
- translate = Google::Cloud::Translate.new version: :v2
365
-
366
- languages = translate.languages "en"
367
-
368
- languages.size #=> 104
369
- languages[0].code #=> "af"
370
- languages[0].name #=> "Afrikaans"
371
- ```
372
-
373
- ### Configuring retries and timeout
374
-
375
- You can configure how many times API requests may be automatically
376
- retried. When an API request fails, the response will be inspected to see
377
- if the request meets criteria indicating that it may succeed on retry,
378
- such as `500` and `503` status codes or a specific internal error code
379
- such as `rateLimitExceeded`. If it meets the criteria, the request will be
380
- retried after a delay. If another error occurs, the delay will be
381
- increased before a subsequent attempt, until the `retries` limit is
382
- reached.
383
-
384
- You can also set the request `timeout` value in seconds.
385
-
386
- ```ruby
387
- require "google/cloud/translate"
388
-
389
- translate = Google::Cloud::Translate.new version: :v2, retries: 10, timeout: 120
390
- ```
@@ -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