google-cloud-translate 1.4.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +6 -2
  3. data/CHANGELOG.md +33 -0
  4. data/CONTRIBUTING.md +1 -1
  5. data/LICENSE +1 -1
  6. data/OVERVIEW.md +223 -18
  7. data/lib/google-cloud-translate.rb +79 -91
  8. data/lib/google/cloud/translate.rb +127 -114
  9. data/lib/google/cloud/translate/v2.rb +169 -0
  10. data/lib/google/cloud/translate/v2/api.rb +255 -0
  11. data/lib/google/cloud/translate/v2/credentials.rb +58 -0
  12. data/lib/google/cloud/translate/v2/detection.rb +132 -0
  13. data/lib/google/cloud/translate/v2/language.rb +68 -0
  14. data/lib/google/cloud/translate/v2/service.rb +205 -0
  15. data/lib/google/cloud/translate/v2/translation.rb +120 -0
  16. data/lib/google/cloud/translate/v3.rb +144 -0
  17. data/lib/google/cloud/translate/v3/credentials.rb +42 -0
  18. data/lib/google/cloud/translate/v3/doc/google/cloud/translate/v3/translation_service.rb +663 -0
  19. data/lib/google/cloud/translate/v3/doc/google/longrunning/operations.rb +51 -0
  20. data/lib/google/cloud/translate/v3/doc/google/protobuf/any.rb +131 -0
  21. data/lib/google/cloud/translate/v3/doc/google/protobuf/timestamp.rb +113 -0
  22. data/lib/google/cloud/translate/v3/doc/google/rpc/status.rb +87 -0
  23. data/lib/google/cloud/translate/v3/translation_service_client.rb +927 -0
  24. data/lib/google/cloud/translate/v3/translation_service_client_config.json +66 -0
  25. data/lib/google/cloud/translate/v3/translation_service_pb.rb +226 -0
  26. data/lib/google/cloud/translate/v3/translation_service_services_pb.rb +68 -0
  27. data/lib/google/cloud/translate/version.rb +1 -1
  28. metadata +47 -37
  29. data/lib/google/cloud/translate/api.rb +0 -274
  30. data/lib/google/cloud/translate/credentials.rb +0 -57
  31. data/lib/google/cloud/translate/detection.rb +0 -139
  32. data/lib/google/cloud/translate/language.rb +0 -70
  33. data/lib/google/cloud/translate/service.rb +0 -206
  34. data/lib/google/cloud/translate/translation.rb +0 -125
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 446be46e3daa8f7a75ccacfe05f26725697c67fe4f84607f4926df2fe4f9e909
4
- data.tar.gz: a29473454843607b347d16cbffe1c9a27e152e2589e9a1294fa4f0c89b959fd9
3
+ metadata.gz: 344c5421f081225ab60746aa3b2ed5c86bca62ca90f6dd0d06e72cc7ff2baf11
4
+ data.tar.gz: cdd29c5ffd9512b2cd52d3ce0d477c7a58a1551ea2a6c39cc3c2bcde9cda2a9e
5
5
  SHA512:
6
- metadata.gz: 076d7957b5fd21f68e4e7b7dafbdf1bf0e08b4da8c5ee876f008eec7c728f587bb63a26a58e6bbc6cffdf9df7369a48a87d0f01a00bed3b5b84bc807828392fc
7
- data.tar.gz: 7ef9781612d55d99564dc1691b0c689ea0d242d30804b54c48f37b1a08f44e7b33560b1ac856f42483f00e57cb6062e8f3cb7c8fcf3ea60c9c9f07b107494393
6
+ metadata.gz: 1e665e72ef315ca0339d2c439ac17f7bdef4e423bc8eaed27187ad13106734bfdbac178826821e815e5cde3b12d8a2b4d40c39aa309298ee3d434b9cbb8ed78e
7
+ data.tar.gz: ae9c103bfbc5e91be17d5a2e122a453a8326199a6f63a88e49a09d09c095aba28b66788516782e60452440b52583b6916f096bc1c9e2af286328cf076a10ebf6
@@ -2,7 +2,9 @@
2
2
 
3
3
  In general, the google-cloud-translate library uses [Service
4
4
  Account](https://cloud.google.com/iam/docs/creating-managing-service-accounts)
5
- credentials to connect to Google Cloud services. When running on Compute Engine
5
+ credentials to connect to Google Cloud services. When running on Google Cloud
6
+ Platform (GCP), including Google Compute Engine (GCE), Google Kubernetes Engine
7
+ (GKE), Google App Engine (GAE), Google Cloud Functions (GCF) and Cloud Run,
6
8
  the credentials will be discovered automatically. When running on other
7
9
  environments, the Service Account credentials can be specified by providing the
8
10
  path to the [JSON
@@ -79,7 +81,9 @@ The environment variables that Translation checks for project ID are:
79
81
  2. `GOOGLE_CLOUD_PROJECT`
80
82
 
81
83
  The environment variables that Translation checks for credentials are configured
82
- on {Google::Cloud::Translate::Credentials}:
84
+ on {Google::Cloud::Translate::V3::Credentials} when using the current V3 client,
85
+ and on {Google::Cloud::Translate::V2::Credentials} when using the legacy V2
86
+ client:
83
87
 
84
88
  1. `TRANSLATE_CREDENTIALS` - Path to JSON file, or JSON contents
85
89
  2. `TRANSLATE_KEYFILE` - Path to JSON file, or JSON contents
@@ -1,5 +1,38 @@
1
1
  # Release History
2
2
 
3
+ ### 2.0.0 / 2019-10-28
4
+
5
+ #### ⚠ BREAKING CHANGES
6
+
7
+ * Add Translate V3 client
8
+ * Update google-cloud-translate to contain a generated v3 client
9
+ as well as the legacy hand-written v2 client.
10
+ * The following methods now return an instance of
11
+ Google::Cloud::Translate::V3::TranslationServiceClient:
12
+ * Google::Cloud#translate
13
+ * Google::Cloud.translate
14
+ * Google::Cloud::Translate.new
15
+ * To use the legacy v2 client specify the version when creating:
16
+ * v2_client = Google::Cloud::Translate.new version: :v2
17
+
18
+ #### Features
19
+
20
+ * Add Translate V3 client
21
+ * The v3 client includes several new features and updates:
22
+ * Glossaries - Create a custom dictionary to correctly and
23
+ consistently translate terms that are customer-specific.
24
+ * Batch requests - Make an asynchronous request to translate
25
+ large amounts of text.
26
+ * AutoML models - Cloud Translation adds support for translating
27
+ text with custom models that you create using AutoML Translation.
28
+ * Labels - The Cloud Translation API supports adding user-defined
29
+ labels (key-value pairs) to requests.
30
+ * Now requires Ruby 2.4 or later.
31
+
32
+ #### Documentation
33
+
34
+ * Update the list of GCP environments for automatic authentication
35
+
3
36
  ### 1.4.0 / 2019-10-01
4
37
 
5
38
  #### Features
@@ -24,7 +24,7 @@ be able to accept your pull requests.
24
24
  In order to use the google-cloud-translate console and run the project's tests,
25
25
  there is a small amount of setup:
26
26
 
27
- 1. Install Ruby. google-cloud-translate requires Ruby 2.3+. You may choose to
27
+ 1. Install Ruby. google-cloud-translate requires Ruby 2.4+. You may choose to
28
28
  manage your Ruby and gem installations with [RVM](https://rvm.io/),
29
29
  [rbenv](https://github.com/rbenv/rbenv), or
30
30
  [chruby](https://github.com/postmodern/chruby).
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Apache License
1
+ Apache License
2
2
  Version 2.0, January 2004
3
3
  https://www.apache.org/licenses/
4
4
 
@@ -13,6 +13,10 @@ thousands of language pairs. Also, you can send in HTML and receive HTML
13
13
  with translated text back. You don't need to extract your source text or
14
14
  reassemble the translated content.
15
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
+
16
20
  ## Authenticating
17
21
 
18
22
  Like other Cloud Platform services, Google Cloud Translation API supports
@@ -22,18 +26,219 @@ key and the project and OAuth 2.0 credentials are provided, the API key
22
26
  will be used.) Instructions and configuration options are covered in the
23
27
  {file:AUTHENTICATION.md Authentication Guide}.
24
28
 
25
- ## Translating texts
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
26
231
 
27
232
  Translating text from one language to another is easy (and extremely
28
233
  fast.) The only required arguments to
29
- {Google::Cloud::Translate::Api#translate} are a string and the [ISO
234
+ {Google::Cloud::Translate::V2::Api#translate} are a string and the [ISO
30
235
  639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) code of the
31
236
  language to which you wish to translate.
32
237
 
33
238
  ```ruby
34
239
  require "google/cloud/translate"
35
240
 
36
- translate = Google::Cloud::Translate.new
241
+ translate = Google::Cloud::Translate.new version: :v2
37
242
 
38
243
  translation = translate.translate "Hello world!", to: "la"
39
244
 
@@ -52,7 +257,7 @@ give Translation API much to work with.)
52
257
  ```ruby
53
258
  require "google/cloud/translate"
54
259
 
55
- translate = Google::Cloud::Translate.new
260
+ translate = Google::Cloud::Translate.new version: :v2
56
261
 
57
262
  translation = translate.translate "chat", to: "en"
58
263
 
@@ -67,12 +272,12 @@ translation.from #=> "fr"
67
272
  translation.text #=> "cat"
68
273
  ```
69
274
 
70
- You can pass multiple texts to {Google::Cloud::Translate::Api#translate}.
275
+ You can pass multiple texts to {Google::Cloud::Translate::V2::Api#translate}.
71
276
 
72
277
  ```ruby
73
278
  require "google/cloud/translate"
74
279
 
75
- translate = Google::Cloud::Translate.new
280
+ translate = Google::Cloud::Translate.new version: :v2
76
281
 
77
282
  translations = translate.translate "chien", "chat", from: "fr", to: "en"
78
283
 
@@ -88,23 +293,23 @@ By default, any HTML in your source text will be preserved.
88
293
  ```ruby
89
294
  require "google/cloud/translate"
90
295
 
91
- translate = Google::Cloud::Translate.new
296
+ translate = Google::Cloud::Translate.new version: :v2
92
297
 
93
298
  translation = translate.translate "<strong>Hello</strong> world!",
94
299
  to: :la
95
300
  translation.text #=> "<strong>Salve</strong> mundi!"
96
301
  ```
97
302
 
98
- ## Detecting languages
303
+ ### Detecting languages
99
304
 
100
- You can use {Google::Cloud::Translate::Api#detect} to see which language
305
+ You can use {Google::Cloud::Translate::V2::Api#detect} to see which language
101
306
  the Translation API ranks as the most likely source language for a text.
102
307
  The `confidence` score is a float value between `0` and `1`.
103
308
 
104
309
  ```ruby
105
310
  require "google/cloud/translate"
106
311
 
107
- translate = Google::Cloud::Translate.new
312
+ translate = Google::Cloud::Translate.new version: :v2
108
313
 
109
314
  detection = translate.detect "chat"
110
315
 
@@ -113,12 +318,12 @@ detection.language #=> "en"
113
318
  detection.confidence #=> 0.59922177
114
319
  ```
115
320
 
116
- You can pass multiple texts to {Google::Cloud::Translate::Api#detect}.
321
+ You can pass multiple texts to {Google::Cloud::Translate::V2::Api#detect}.
117
322
 
118
323
  ```ruby
119
324
  require "google/cloud/translate"
120
325
 
121
- translate = Google::Cloud::Translate.new
326
+ translate = Google::Cloud::Translate.new version: :v2
122
327
 
123
328
  detections = translate.detect "chien", "chat"
124
329
 
@@ -131,16 +336,16 @@ detections[1].language #=> "en"
131
336
  detections[1].confidence #=> 0.59922177
132
337
  ```
133
338
 
134
- ## Listing supported languages
339
+ ### Listing supported languages
135
340
 
136
341
  Translation API adds new languages frequently. You can use
137
- {Google::Cloud::Translate::Api#languages} to query the list of supported
342
+ {Google::Cloud::Translate::V2::Api#languages} to query the list of supported
138
343
  languages.
139
344
 
140
345
  ```ruby
141
346
  require "google/cloud/translate"
142
347
 
143
- translate = Google::Cloud::Translate.new
348
+ translate = Google::Cloud::Translate.new version: :v2
144
349
 
145
350
  languages = translate.languages
146
351
 
@@ -156,7 +361,7 @@ provide the code for the language in which you wish to receive the names.
156
361
  ```ruby
157
362
  require "google/cloud/translate"
158
363
 
159
- translate = Google::Cloud::Translate.new
364
+ translate = Google::Cloud::Translate.new version: :v2
160
365
 
161
366
  languages = translate.languages "en"
162
367
 
@@ -165,7 +370,7 @@ languages[0].code #=> "af"
165
370
  languages[0].name #=> "Afrikaans"
166
371
  ```
167
372
 
168
- ## Configuring retries and timeout
373
+ ### Configuring retries and timeout
169
374
 
170
375
  You can configure how many times API requests may be automatically
171
376
  retried. When an API request fails, the response will be inspected to see
@@ -181,5 +386,5 @@ You can also set the request `timeout` value in seconds.
181
386
  ```ruby
182
387
  require "google/cloud/translate"
183
388
 
184
- translate = Google::Cloud::Translate.new retries: 10, timeout: 120
389
+ translate = Google::Cloud::Translate.new version: :v2, retries: 10, timeout: 120
185
390
  ```
@@ -26,119 +26,112 @@ require "googleauth"
26
26
  module Google
27
27
  module Cloud
28
28
  ##
29
- # Creates a new object for connecting to the Cloud Translation API. Each
30
- # call creates a new connection.
29
+ # Creates a new object for connecting to the Cloud Translation API. Each call creates a new connection.
31
30
  #
32
- # Like other Cloud Platform services, Google Cloud Translation API supports
33
- # authentication using a project ID and OAuth 2.0 credentials. In addition,
34
- # it supports authentication using a public API access key. (If both the API
35
- # key and the project and OAuth 2.0 credentials are provided, the API key
36
- # will be used.) Instructions and configuration options are covered in the
37
- # {file:AUTHENTICATION.md Authentication Guide}.
31
+ # For more information on connecting to Google Cloud see the {file:AUTHENTICATION.md Authentication Guide}.
32
+ #
33
+ # To use the legacy v2 client, call {Google::Cloud::Translate.new} and specify `version: :v2`.
38
34
  #
39
35
  # @param [String] key a public API access key (not an OAuth 2.0 token)
40
- # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
41
- # set of resources and operations that the connection can access. See
42
- # [Using OAuth 2.0 to Access Google
36
+ # @param [String, Array<String>] scopes The OAuth 2.0 scopes controlling the set of resources and operations that
37
+ # the connection can access. See [Using OAuth 2.0 to Access Google
43
38
  # APIs](https://developers.google.com/identity/protocols/OAuth2).
44
39
  #
45
- # The default scope is:
40
+ # The default scopes are:
46
41
  #
47
42
  # * `https://www.googleapis.com/auth/cloud-platform`
48
- # @param [Integer] retries Number of times to retry requests on server
49
- # error. The default value is `3`. Optional.
43
+ # * `https://www.googleapis.com/auth/cloud-translation`
50
44
  # @param [Integer] timeout Default timeout to use in requests. Optional.
51
45
  #
52
- # @return [Google::Cloud::Translate::Api]
46
+ # @return [Google::Cloud::Translate::V3::TranslationServiceClient]
53
47
  #
54
48
  # @example
55
49
  # require "google/cloud"
56
50
  #
57
51
  # gcloud = Google::Cloud.new
58
- # translate = gcloud.translate "api-key-abc123XYZ789"
59
- #
60
- # translation = translate.translate "Hello world!", to: "la"
61
- # translation.text #=> "Salve mundi!"
62
- #
63
- # @example Using API Key from the environment variable.
64
- # require "google/cloud"
65
- #
66
- # ENV["TRANSLATE_KEY"] = "api-key-abc123XYZ789"
67
- #
68
- # gcloud = Google::Cloud.new
69
- # translate = gcloud.translate
70
- #
71
- # translation = translate.translate "Hello world!", to: "la"
72
- # translation.text #=> "Salve mundi!"
73
- #
74
- def translate key = nil, scope: nil, retries: nil, timeout: nil
75
- Google::Cloud.translate key, project_id: @project, credentials: @keyfile,
76
- scope: scope,
77
- retries: (retries || @retries),
78
- timeout: (timeout || @timeout)
52
+ # client = gcloud.translate
53
+ #
54
+ # project_id = "my-project-id"
55
+ # location_id = "us-central1"
56
+ # model_id = "my-automl-model-id"
57
+ #
58
+ # # The `model` type requested for this translation.
59
+ # model = "projects/#{project_id}/locations/#{location_id}/models/#{model_id}"
60
+ # # The content to translate in string format
61
+ # contents = ["Hello, world!"]
62
+ # # Required. The BCP-47 language code to use for translation.
63
+ # target_language = "fr"
64
+ # # Optional. The BCP-47 language code of the input text.
65
+ # source_language = "en"
66
+ # # Optional. Can be "text/plain" or "text/html".
67
+ # mime_type = "text/plain"
68
+ # parent = client.class.location_path project_id, location_id
69
+ #
70
+ # response = client.translate_text contents, target_language, parent,
71
+ # source_language_code: source_language, model: model, mime_type: mime_type
72
+ #
73
+ # # Display the translation for each input text provided
74
+ # response.translations.each do |translation|
75
+ # puts "Translated text: #{translation.translated_text}"
76
+ # end
77
+ #
78
+ def translate scopes: nil, timeout: nil
79
+ Google::Cloud.translate credentials: @keyfile, scopes: scopes, timeout: (timeout || @timeout)
79
80
  end
80
81
 
81
82
  ##
82
- # Creates a new object for connecting to the Cloud Translation API. Each
83
- # call creates a new connection.
83
+ # Creates a new object for connecting to the Cloud Translation API. Each call creates a new connection.
84
84
  #
85
- # Like other Cloud Platform services, Google Cloud Translation API supports
86
- # authentication using a project ID and OAuth 2.0 credentials. In addition,
87
- # it supports authentication using a public API access key. (If both the API
88
- # key and the project and OAuth 2.0 credentials are provided, the API key
89
- # will be used.) Instructions and configuration options are covered in the
90
- # {file:AUTHENTICATION.md Authentication Guide}.
85
+ # For more information on connecting to Google Cloud see the {file:AUTHENTICATION.md Authentication Guide}.
91
86
  #
92
- # @param [String] key a public API access key (not an OAuth 2.0 token)
93
- # @param [String] project_id Project identifier for the Cloud Translation
94
- # service you are connecting to. If not present, the default project for
95
- # the credentials is used.
96
- # @param [String, Hash, Google::Auth::Credentials] credentials The path to
97
- # the keyfile as a String, the contents of the keyfile as a Hash, or a
98
- # Google::Auth::Credentials object. (See {Translate::Credentials})
99
- # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
100
- # set of resources and operations that the connection can access. See
101
- # [Using OAuth 2.0 to Access Google
87
+ # To use the legacy v2 client, call {Google::Cloud::Translate.new} and specify `version: :v2`.
88
+ #
89
+ # @param [String, Hash, Google::Auth::Credentials] credentials The path to the keyfile as a String, the contents of
90
+ # the keyfile as a Hash, or a Google::Auth::Credentials object. (See {Google::Cloud::Translate::V3::Credentials})
91
+ # @param [String, Array<String>] scopes The OAuth 2.0 scopes controlling the set of resources and operations that
92
+ # the connection can access. See [Using OAuth 2.0 to Access Google
102
93
  # APIs](https://developers.google.com/identity/protocols/OAuth2).
103
94
  #
104
- # The default scope is:
95
+ # The default scopes are:
105
96
  #
106
97
  # * `https://www.googleapis.com/auth/cloud-platform`
107
- # @param [Integer] retries Number of times to retry requests on server
108
- # error. The default value is `3`. Optional.
98
+ # * `https://www.googleapis.com/auth/cloud-translation`
109
99
  # @param [Integer] timeout Default timeout to use in requests. Optional.
110
- # @param [String] project Alias for the `project_id` argument. Deprecated.
111
- # @param [String] keyfile Alias for the `credentials` argument.
112
- # Deprecated.
113
100
  #
114
- # @return [Google::Cloud::Translate::Api]
101
+ # @return [Google::Cloud::Translate::V3::TranslationServiceClient]
115
102
  #
116
103
  # @example
117
104
  # require "google/cloud"
118
105
  #
119
- # translate = Google::Cloud.translate "api-key-abc123XYZ789"
120
- #
121
- # translation = translate.translate "Hello world!", to: "la"
122
- # translation.text #=> "Salve mundi!"
123
- #
124
- # @example Using API Key from the environment variable.
125
- # require "google/cloud"
126
- #
127
- # ENV["TRANSLATE_KEY"] = "api-key-abc123XYZ789"
128
- #
129
- # translate = Google::Cloud.translate
130
- #
131
- # translation = translate.translate "Hello world!", to: "la"
132
- # translation.text #=> "Salve mundi!"
133
- #
134
- def self.translate key = nil, project_id: nil, credentials: nil, scope: nil,
135
- retries: nil, timeout: nil, project: nil, keyfile: nil
136
- require "google/cloud/translate"
137
- Google::Cloud::Translate.new key: key, project_id: project_id,
138
- credentials: credentials,
139
- scope: scope, retries: retries,
140
- timeout: timeout,
141
- project: project, keyfile: keyfile
106
+ # client = Google::Cloud.translate
107
+ #
108
+ # project_id = "my-project-id"
109
+ # location_id = "us-central1"
110
+ # model_id = "my-automl-model-id"
111
+ #
112
+ # # The `model` type requested for this translation.
113
+ # model = "projects/#{project_id}/locations/#{location_id}/models/#{model_id}"
114
+ # # The content to translate in string format
115
+ # contents = ["Hello, world!"]
116
+ # # Required. The BCP-47 language code to use for translation.
117
+ # target_language = "fr"
118
+ # # Optional. The BCP-47 language code of the input text.
119
+ # source_language = "en"
120
+ # # Optional. Can be "text/plain" or "text/html".
121
+ # mime_type = "text/plain"
122
+ # parent = client.class.location_path project_id, location_id
123
+ #
124
+ # response = client.translate_text contents, target_language, parent,
125
+ # source_language_code: source_language, model: model, mime_type: mime_type
126
+ #
127
+ # # Display the translation for each input text provided
128
+ # response.translations.each do |translation|
129
+ # puts "Translated text: #{translation.translated_text}"
130
+ # end
131
+ #
132
+ def self.translate credentials: nil, scopes: nil, timeout: nil
133
+ require "google/cloud/translate/v3"
134
+ Google::Cloud::Translate::V3.new credentials: credentials, scopes: scopes, timeout: timeout
142
135
  end
143
136
  end
144
137
  end
@@ -150,8 +143,7 @@ Google::Cloud.configure.add_config! :translate do |config|
150
143
  end
151
144
  default_creds = Google::Cloud::Config.deferred do
152
145
  Google::Cloud::Config.credentials_from_env(
153
- "TRANSLATE_CREDENTIALS", "TRANSLATE_CREDENTIALS_JSON",
154
- "TRANSLATE_KEYFILE", "TRANSLATE_KEYFILE_JSON"
146
+ "TRANSLATE_CREDENTIALS", "TRANSLATE_CREDENTIALS_JSON", "TRANSLATE_KEYFILE", "TRANSLATE_KEYFILE_JSON"
155
147
  )
156
148
  end
157
149
  default_key = Google::Cloud::Config.deferred do
@@ -159,11 +151,7 @@ Google::Cloud.configure.add_config! :translate do |config|
159
151
  end
160
152
 
161
153
  config.add_field! :project_id, default_project, match: String, allow_nil: true
162
- config.add_alias! :project, :project_id
163
- config.add_field! :credentials, default_creds,
164
- match: [String, Hash, Google::Auth::Credentials],
165
- allow_nil: true
166
- config.add_alias! :keyfile, :credentials
154
+ config.add_field! :credentials, default_creds, match: [String, Hash, Google::Auth::Credentials], allow_nil: true
167
155
  config.add_field! :key, default_key, match: String, allow_nil: true
168
156
  config.add_field! :scope, nil, match: [String, Array]
169
157
  config.add_field! :retries, nil, match: Integer