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.
- checksums.yaml +4 -4
- data/.yardopts +5 -8
- data/AUTHENTICATION.md +67 -81
- data/LICENSE.md +201 -0
- data/MIGRATING.md +302 -0
- data/README.md +139 -0
- data/lib/google-cloud-translate.rb +5 -146
- data/lib/google/cloud/translate.rb +79 -178
- data/lib/google/cloud/translate/helpers.rb +107 -0
- data/lib/google/cloud/translate/version.rb +6 -2
- metadata +38 -138
- data/CHANGELOG.md +0 -164
- data/CODE_OF_CONDUCT.md +0 -40
- data/CONTRIBUTING.md +0 -188
- data/LICENSE +0 -201
- data/OVERVIEW.md +0 -390
- data/TROUBLESHOOTING.md +0 -37
- data/lib/google/cloud/translate/v2.rb +0 -169
- data/lib/google/cloud/translate/v2/api.rb +0 -255
- data/lib/google/cloud/translate/v2/credentials.rb +0 -58
- data/lib/google/cloud/translate/v2/detection.rb +0 -132
- data/lib/google/cloud/translate/v2/language.rb +0 -68
- data/lib/google/cloud/translate/v2/service.rb +0 -209
- data/lib/google/cloud/translate/v2/translation.rb +0 -120
- data/lib/google/cloud/translate/v3.rb +0 -144
- data/lib/google/cloud/translate/v3/credentials.rb +0 -42
- data/lib/google/cloud/translate/v3/doc/google/cloud/translate/v3/translation_service.rb +0 -663
- data/lib/google/cloud/translate/v3/doc/google/longrunning/operations.rb +0 -51
- data/lib/google/cloud/translate/v3/doc/google/protobuf/any.rb +0 -131
- data/lib/google/cloud/translate/v3/doc/google/protobuf/timestamp.rb +0 -113
- data/lib/google/cloud/translate/v3/doc/google/rpc/status.rb +0 -39
- data/lib/google/cloud/translate/v3/translation_service_client.rb +0 -930
- data/lib/google/cloud/translate/v3/translation_service_client_config.json +0 -66
- data/lib/google/cloud/translate/v3/translation_service_pb.rb +0 -226
- data/lib/google/cloud/translate/v3/translation_service_services_pb.rb +0 -68
data/README.md
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
# Ruby Client for the Cloud Translation API
|
2
|
+
|
3
|
+
API Client library for the Cloud Translation API
|
4
|
+
|
5
|
+
Cloud Translation can dynamically translate text between thousands of language pairs. Translation lets websites and programs programmatically integrate with the translation service.
|
6
|
+
|
7
|
+
Actual client classes for the various versions of this API are defined in
|
8
|
+
_versioned_ client gems, with names of the form `google-cloud-translate-v*`.
|
9
|
+
The gem `google-cloud-translate` is the main client library that brings the
|
10
|
+
verisoned gems in as dependencies, and provides high-level methods for
|
11
|
+
constructing clients. More information on versioned clients can be found below
|
12
|
+
in the section titled *Which client should I use?*.
|
13
|
+
|
14
|
+
View the [Client Library Documentation](https://googleapis.dev/ruby/google-cloud-translate/latest)
|
15
|
+
for this library, google-cloud-translate, to see the convenience methods for
|
16
|
+
constructing client objects. Reference documentation for the client objects
|
17
|
+
themselves can be found in the client library documentation for the versioned
|
18
|
+
client gems:
|
19
|
+
[google-cloud-translate-v2](https://googleapis.dev/ruby/google-cloud-translate-v2/latest),
|
20
|
+
[google-cloud-translate-v3](https://googleapis.dev/ruby/google-cloud-translate-v3/latest).
|
21
|
+
|
22
|
+
See also the [Product Documentation](https://cloud.google.com/translate)
|
23
|
+
for more usage information.
|
24
|
+
|
25
|
+
## Quick Start
|
26
|
+
|
27
|
+
```
|
28
|
+
$ gem install google-cloud-translate
|
29
|
+
```
|
30
|
+
|
31
|
+
In order to use this library, you first need to go through the following steps:
|
32
|
+
|
33
|
+
1. [Select or create a Cloud Platform project.](https://console.cloud.google.com/project)
|
34
|
+
1. [Enable billing for your project.](https://cloud.google.com/billing/docs/how-to/modify-project#enable_billing_for_a_project)
|
35
|
+
1. [Enable the API.](https://console.cloud.google.com/apis/library/translate.googleapis.com)
|
36
|
+
1. {file:AUTHENTICATION.md Set up authentication.}
|
37
|
+
|
38
|
+
## Migrating from 2.x versions
|
39
|
+
|
40
|
+
The 3.0 release of the google-cloud-translate client is a significant upgrade
|
41
|
+
based on a [next-gen code generator](https://github.com/googleapis/gapic-generator-ruby),
|
42
|
+
and includes substantial interface changes. Existing code written for earlier
|
43
|
+
versions of this library will likely require updates to use this version.
|
44
|
+
See the {file:MIGRATING.md MIGRATING.md} document for more information.
|
45
|
+
|
46
|
+
## Enabling Logging
|
47
|
+
|
48
|
+
To enable logging for this library, set the logger for the underlying [gRPC](https://github.com/grpc/grpc/tree/master/src/ruby) library.
|
49
|
+
The logger that you set may be a Ruby stdlib [`Logger`](https://ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger.html) as shown below,
|
50
|
+
or a [`Google::Cloud::Logging::Logger`](https://googleapis.dev/ruby/google-cloud-logging/latest)
|
51
|
+
that will write logs to [Cloud Logging](https://cloud.google.com/logging/). See [grpc/logconfig.rb](https://github.com/grpc/grpc/blob/master/src/ruby/lib/grpc/logconfig.rb)
|
52
|
+
and the gRPC [spec_helper.rb](https://github.com/grpc/grpc/blob/master/src/ruby/spec/spec_helper.rb) for additional information.
|
53
|
+
|
54
|
+
Configuring a Ruby stdlib logger:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
require "logger"
|
58
|
+
|
59
|
+
module MyLogger
|
60
|
+
LOGGER = Logger.new $stderr, level: Logger::WARN
|
61
|
+
def logger
|
62
|
+
LOGGER
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Define a gRPC module-level logger method before grpc/logconfig.rb loads.
|
67
|
+
module GRPC
|
68
|
+
extend MyLogger
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
## Supported Ruby Versions
|
73
|
+
|
74
|
+
This library is supported on Ruby 2.5+.
|
75
|
+
|
76
|
+
Google provides official support for Ruby versions that are actively supported
|
77
|
+
by Ruby Core—that is, Ruby versions that are either in normal maintenance or
|
78
|
+
in security maintenance, and not end of life. Currently, this means Ruby 2.5
|
79
|
+
and later. Older versions of Ruby _may_ still work, but are unsupported and not
|
80
|
+
recommended. See https://www.ruby-lang.org/en/downloads/branches/ for details
|
81
|
+
about the Ruby support schedule.
|
82
|
+
|
83
|
+
## Which client should I use?
|
84
|
+
|
85
|
+
Most modern Ruby client libraries for Google APIs come in two flavors: the main
|
86
|
+
client library with a name such as `google-cloud-translate`,
|
87
|
+
and lower-level _versioned_ client libraries with names such as
|
88
|
+
`google-cloud-translate-v2`.
|
89
|
+
_In most cases, you should install the main client._
|
90
|
+
|
91
|
+
### What's the difference between the main client and a versioned client?
|
92
|
+
|
93
|
+
A _versioned client_ provides a basic set of data types and client classes for
|
94
|
+
a _single version_ of a specific service. (That is, for a service with multiple
|
95
|
+
versions, there might be a separate versioned client for each service version.)
|
96
|
+
Most versioned clients are written and maintained by a code generator.
|
97
|
+
|
98
|
+
The _main client_ is designed to provide you with the _recommended_ client
|
99
|
+
interfaces for the service. There will be only one main client for any given
|
100
|
+
service, even a service with multiple versions. The main client includes
|
101
|
+
factory methods for constructing the client objects we recommend for most
|
102
|
+
users. In some cases, those will be classes provided by an underlying versioned
|
103
|
+
client; in other cases, they will be handwritten higher-level client objects
|
104
|
+
with additional capabilities, convenience methods, or best practices built in.
|
105
|
+
Generally, the main client will default to a recommended service version,
|
106
|
+
although in some cases you can override this if you need to talk to a specific
|
107
|
+
service version.
|
108
|
+
|
109
|
+
### Why would I want to use the main client?
|
110
|
+
|
111
|
+
We recommend that most users install the main client gem for a service. You can
|
112
|
+
identify this gem as the one _without_ a version in its name, e.g.
|
113
|
+
`google-cloud-translate`.
|
114
|
+
The main client is recommended because it will embody the best practices for
|
115
|
+
accessing the service, and may also provide more convenient interfaces or
|
116
|
+
tighter integration into frameworks and third-party libraries. In addition, the
|
117
|
+
documentation and samples published by Google will generally demonstrate use of
|
118
|
+
the main client.
|
119
|
+
|
120
|
+
### Why would I want to use a versioned client?
|
121
|
+
|
122
|
+
You can use a versioned client if you are content with a possibly lower-level
|
123
|
+
class interface, you explicitly want to avoid features provided by the main
|
124
|
+
client, or you want to access a specific service version not be covered by the
|
125
|
+
main client. You can identify versioned client gems because the service version
|
126
|
+
is part of the name, e.g. `google-cloud-translate-v2`.
|
127
|
+
|
128
|
+
### What about the google-apis-<name> clients?
|
129
|
+
|
130
|
+
Client library gems with names that begin with `google-apis-` are based on an
|
131
|
+
older code generation technology. They talk to a REST/JSON backend (whereas
|
132
|
+
most modern clients talk to a [gRPC](https://grpc.io/) backend) and they may
|
133
|
+
not offer the same performance, features, and ease of use provided by more
|
134
|
+
modern clients.
|
135
|
+
|
136
|
+
The `google-apis-` clients have wide coverage across Google services, so you
|
137
|
+
might need to use one if there is no modern client available for the service.
|
138
|
+
However, if a modern client is available, we generally recommend it over the
|
139
|
+
older `google-apis-` clients.
|
@@ -1,4 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2020 Google LLC
|
2
4
|
#
|
3
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
6
|
# you may not use this file except in compliance with the License.
|
@@ -12,149 +14,6 @@
|
|
12
14
|
# See the License for the specific language governing permissions and
|
13
15
|
# limitations under the License.
|
14
16
|
|
15
|
-
|
16
|
-
# This file is here to be autorequired by bundler, so that the
|
17
|
-
# Google::Cloud.translate and Google::Cloud#translate methods can be available,
|
18
|
-
# but the library and all dependencies won't be loaded until required and used.
|
19
|
-
|
20
|
-
|
21
|
-
gem "google-cloud-core"
|
22
|
-
require "google/cloud" unless defined? Google::Cloud.new
|
23
|
-
require "google/cloud/config"
|
24
|
-
require "googleauth"
|
25
|
-
|
26
|
-
module Google
|
27
|
-
module Cloud
|
28
|
-
##
|
29
|
-
# Creates a new object for connecting to the Cloud Translation API. Each call creates a new connection.
|
30
|
-
#
|
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`.
|
34
|
-
#
|
35
|
-
# @param [String] key a public API access key (not an OAuth 2.0 token)
|
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
|
38
|
-
# APIs](https://developers.google.com/identity/protocols/OAuth2).
|
39
|
-
#
|
40
|
-
# The default scopes are:
|
41
|
-
#
|
42
|
-
# * `https://www.googleapis.com/auth/cloud-platform`
|
43
|
-
# * `https://www.googleapis.com/auth/cloud-translation`
|
44
|
-
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
45
|
-
#
|
46
|
-
# @return [Google::Cloud::Translate::V3::TranslationServiceClient]
|
47
|
-
#
|
48
|
-
# @example
|
49
|
-
# require "google/cloud"
|
50
|
-
#
|
51
|
-
# gcloud = Google::Cloud.new
|
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)
|
80
|
-
end
|
81
|
-
|
82
|
-
##
|
83
|
-
# Creates a new object for connecting to the Cloud Translation API. Each call creates a new connection.
|
84
|
-
#
|
85
|
-
# For more information on connecting to Google Cloud see the {file:AUTHENTICATION.md Authentication Guide}.
|
86
|
-
#
|
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
|
93
|
-
# APIs](https://developers.google.com/identity/protocols/OAuth2).
|
94
|
-
#
|
95
|
-
# The default scopes are:
|
96
|
-
#
|
97
|
-
# * `https://www.googleapis.com/auth/cloud-platform`
|
98
|
-
# * `https://www.googleapis.com/auth/cloud-translation`
|
99
|
-
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
100
|
-
#
|
101
|
-
# @return [Google::Cloud::Translate::V3::TranslationServiceClient]
|
102
|
-
#
|
103
|
-
# @example
|
104
|
-
# require "google/cloud"
|
105
|
-
#
|
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
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
# Set the default translate configuration
|
140
|
-
Google::Cloud.configure.add_config! :translate do |config|
|
141
|
-
default_project = Google::Cloud::Config.deferred do
|
142
|
-
ENV["TRANSLATE_PROJECT"]
|
143
|
-
end
|
144
|
-
default_creds = Google::Cloud::Config.deferred do
|
145
|
-
Google::Cloud::Config.credentials_from_env(
|
146
|
-
"TRANSLATE_CREDENTIALS", "TRANSLATE_CREDENTIALS_JSON", "TRANSLATE_KEYFILE", "TRANSLATE_KEYFILE_JSON"
|
147
|
-
)
|
148
|
-
end
|
149
|
-
default_key = Google::Cloud::Config.deferred do
|
150
|
-
ENV["TRANSLATE_KEY"] || ENV["GOOGLE_CLOUD_KEY"]
|
151
|
-
end
|
17
|
+
# Auto-generated by gapic-generator-ruby. DO NOT EDIT!
|
152
18
|
|
153
|
-
|
154
|
-
config.add_field! :credentials, default_creds, match: [String, Hash, Google::Auth::Credentials], allow_nil: true
|
155
|
-
config.add_field! :key, default_key, match: String, allow_nil: true
|
156
|
-
config.add_field! :scope, nil, match: [String, Array]
|
157
|
-
config.add_field! :retries, nil, match: Integer
|
158
|
-
config.add_field! :timeout, nil, match: Integer
|
159
|
-
config.add_field! :endpoint, nil, match: String
|
160
|
-
end
|
19
|
+
require "google/cloud/translate" unless defined? Google::Cloud::Translate::VERSION
|
@@ -1,4 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2020 Google LLC
|
2
4
|
#
|
3
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
6
|
# you may not use this file except in compliance with the License.
|
@@ -12,203 +14,102 @@
|
|
12
14
|
# See the License for the specific language governing permissions and
|
13
15
|
# limitations under the License.
|
14
16
|
|
17
|
+
# Auto-generated by gapic-generator-ruby. DO NOT EDIT!
|
18
|
+
|
19
|
+
# Require this file early so that the version constant gets defined before
|
20
|
+
# requiring "google/cloud". This is because google-cloud-core will load the
|
21
|
+
# entrypoint (gem name) file, which in turn re-requires this file (hence
|
22
|
+
# causing a require cycle) unless the version constant is already defined.
|
23
|
+
require "google/cloud/translate/version"
|
15
24
|
|
16
|
-
require "
|
25
|
+
require "googleauth"
|
26
|
+
gem "google-cloud-core"
|
27
|
+
require "google/cloud" unless defined? ::Google::Cloud.new
|
17
28
|
require "google/cloud/config"
|
18
|
-
|
19
|
-
|
29
|
+
|
30
|
+
# Set the default configuration
|
31
|
+
::Google::Cloud.configure.add_config! :translate do |config|
|
32
|
+
config.add_field! :endpoint, "translate.googleapis.com", match: ::String
|
33
|
+
config.add_field! :credentials, nil, match: [::String, ::Hash, ::Google::Auth::Credentials]
|
34
|
+
config.add_field! :scope, nil, match: [::Array, ::String]
|
35
|
+
config.add_field! :lib_name, nil, match: ::String
|
36
|
+
config.add_field! :lib_version, nil, match: ::String
|
37
|
+
config.add_field! :interceptors, nil, match: ::Array
|
38
|
+
config.add_field! :timeout, nil, match: ::Numeric
|
39
|
+
config.add_field! :metadata, nil, match: ::Hash
|
40
|
+
config.add_field! :retry_policy, nil, match: [::Hash, ::Proc]
|
41
|
+
config.add_field! :quota_project, nil, match: ::String
|
42
|
+
end
|
20
43
|
|
21
44
|
module Google
|
22
45
|
module Cloud
|
23
|
-
##
|
24
|
-
# # Google Cloud Translation API
|
25
|
-
#
|
26
|
-
# [Google Cloud Translation API](https://cloud.google.com/translation/)
|
27
|
-
# provides a simple, programmatic interface for translating an arbitrary
|
28
|
-
# string into any supported language. It is highly responsive, so websites
|
29
|
-
# and applications can integrate with Translation API for fast, dynamic
|
30
|
-
# translation of source text. Language detection is also available in cases
|
31
|
-
# where the source language is unknown.
|
32
|
-
#
|
33
|
-
# Translation API supports more than one hundred different languages, from
|
34
|
-
# Afrikaans to Zulu. Used in combination, this enables translation between
|
35
|
-
# thousands of language pairs. Also, you can send in HTML and receive HTML
|
36
|
-
# with translated text back. You don't need to extract your source text or
|
37
|
-
# reassemble the translated content.
|
38
|
-
#
|
39
|
-
# The google-cloud-translate 2.0 gem contains a generated v3 client and a legacy hand-written v2 client.
|
40
|
-
# To use the legacy v2 client, call {Google::Cloud::Translate.new} and specify `version: :v2`.
|
41
|
-
# See [Migrating to Translation v3](https://cloud.google.com/translate/docs/migrate-to-v3) for details regarding
|
42
|
-
# differences between v2 and v3.
|
43
|
-
#
|
44
|
-
# See {file:OVERVIEW.md Translation Overview}.
|
45
|
-
#
|
46
46
|
module Translate
|
47
|
-
FILE_DIR = File.realdirpath Pathname.new(__FILE__).join("..").join("translate")
|
48
|
-
|
49
|
-
AVAILABLE_VERSIONS = Dir["#{FILE_DIR}/*"]
|
50
|
-
.select { |file| File.directory? file }
|
51
|
-
.select { |dir| Google::Gax::VERSION_MATCHER.match File.basename(dir) }
|
52
|
-
.select { |dir| File.exist? dir + ".rb" }
|
53
|
-
.map { |dir| File.basename dir }
|
54
|
-
|
55
47
|
##
|
56
|
-
#
|
57
|
-
#
|
58
|
-
# @overload new(version:, credentials:, scopes:, client_config:, timeout:)
|
59
|
-
# @param version [Symbol, String]
|
60
|
-
# The major version of the service to be used. By default `:v3` is used.
|
61
|
-
# @param credentials [Google::Auth::Credentials, String, Hash, GRPC::Core::Channel,
|
62
|
-
# GRPC::Core::ChannelCredentials, Proc]
|
63
|
-
# Provides the means for authenticating requests made by the client. This parameter can be many types.
|
64
|
-
# A `Google::Auth::Credentials` uses a the properties of its represented keyfile for authenticating requests
|
65
|
-
# made by this client.
|
66
|
-
# A `String` will be treated as the path to the keyfile to be used for the construction of credentials for
|
67
|
-
# this client.
|
68
|
-
# A `Hash` will be treated as the contents of a keyfile to be used for the construction of credentials for
|
69
|
-
# this client.
|
70
|
-
# A `GRPC::Core::Channel` will be used to make calls through.
|
71
|
-
# A `GRPC::Core::ChannelCredentials` for the setting up the RPC client. The channel credentials should already
|
72
|
-
# be composed with a `GRPC::Core::CallCredentials` object.
|
73
|
-
# A `Proc` will be used as an updater_proc for the Grpc channel. The proc transforms the metadata for
|
74
|
-
# requests, generally, to give OAuth credentials.
|
75
|
-
# @param scopes [Array<String>]
|
76
|
-
# The OAuth scopes for this service. This parameter is ignored if an updater_proc is supplied.
|
77
|
-
# @param client_config [Hash]
|
78
|
-
# A Hash for call options for each method. See Google::Gax#construct_settings for the structure of this data.
|
79
|
-
# Falls back to the default config if not specified or the specified config is missing data points.
|
80
|
-
# @param timeout [Numeric]
|
81
|
-
# The default timeout, in seconds, for calls made through this client.
|
82
|
-
# @param metadata [Hash]
|
83
|
-
# Default metadata to be sent with each request. This can be overridden on a per call basis.
|
84
|
-
# @param service_address [String]
|
85
|
-
# Override for the service hostname, or `nil` to leave as the default.
|
86
|
-
# @param service_port [Integer]
|
87
|
-
# Override for the service port, or `nil` to leave as the default.
|
88
|
-
# @param exception_transformer [Proc]
|
89
|
-
# An optional proc that intercepts any exceptions raised during an API call to inject custom error handling.
|
90
|
-
# @overload new(version:, project_id:, credentials:, key:, scope:, retries:, timeout:, endpoint:)
|
91
|
-
# @param version [Symbol, String]
|
92
|
-
# The major version of the service to be used. Specifying `:v2` will return the legacy client.
|
93
|
-
# @param [String] project_id Project identifier for the Cloud Translation service you are connecting to. If not
|
94
|
-
# present, the default project for the credentials is used.
|
95
|
-
# @param [String, Hash, Google::Auth::Credentials] credentials The path to the keyfile as a String, the contents
|
96
|
-
# of the keyfile as a Hash, or a Google::Auth::Credentials object. (See
|
97
|
-
# {Google::Cloud::Translate::V2::Credentials})
|
98
|
-
# @param [String] key a public API access key (not an OAuth 2.0 token)
|
99
|
-
# @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the set of resources and operations that
|
100
|
-
# the connection can access. See [Using OAuth 2.0 to Access Google
|
101
|
-
# APIs](https://developers.google.com/identity/protocols/OAuth2).
|
102
|
-
#
|
103
|
-
# The default scope is:
|
104
|
-
#
|
105
|
-
# * `https://www.googleapis.com/auth/cloud-platform`
|
106
|
-
# @param [Integer] retries Number of times to retry requests on server error. The default value is `3`.
|
107
|
-
# Optional.
|
108
|
-
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
109
|
-
# @param [String] endpoint Override of the endpoint host name. Optional. If the param is nil, uses the default
|
110
|
-
# endpoint.
|
111
|
-
#
|
112
|
-
# @example Using the v3 client.
|
113
|
-
# require "google/cloud/translate"
|
114
|
-
#
|
115
|
-
# client = Google::Cloud::Translate.new
|
116
|
-
#
|
117
|
-
# project_id = "my-project-id"
|
118
|
-
# location_id = "us-central1"
|
119
|
-
# model_id = "my-automl-model-id"
|
120
|
-
#
|
121
|
-
# # The `model` type requested for this translation.
|
122
|
-
# model = "projects/#{project_id}/locations/#{location_id}/models/#{model_id}"
|
123
|
-
# # The content to translate in string format
|
124
|
-
# contents = ["Hello, world!"]
|
125
|
-
# # Required. The BCP-47 language code to use for translation.
|
126
|
-
# target_language = "fr"
|
127
|
-
# # Optional. The BCP-47 language code of the input text.
|
128
|
-
# source_language = "en"
|
129
|
-
# # Optional. Can be "text/plain" or "text/html".
|
130
|
-
# mime_type = "text/plain"
|
131
|
-
# parent = client.class.location_path project_id, location_id
|
48
|
+
# Create a new client object for TranslationService.
|
132
49
|
#
|
133
|
-
#
|
134
|
-
#
|
50
|
+
# By default, this returns an instance of
|
51
|
+
# [Google::Cloud::Translate::V3::TranslationService::Client](https://googleapis.dev/ruby/google-cloud-translate-v3/latest/Google/Cloud/Translate/V3/TranslationService/Client.html)
|
52
|
+
# for version V3 of the API.
|
53
|
+
# However, you can specify specify a different API version by passing it in the
|
54
|
+
# `version` parameter. If the TranslationService service is
|
55
|
+
# supported by that API version, and the corresponding gem is available, the
|
56
|
+
# appropriate versioned client will be returned.
|
135
57
|
#
|
136
|
-
#
|
137
|
-
# response.translations.each do |translation|
|
138
|
-
# puts "Translated text: #{translation.translated_text}"
|
139
|
-
# end
|
58
|
+
# ## About TranslationService
|
140
59
|
#
|
141
|
-
#
|
142
|
-
# require "google/cloud/translate"
|
143
|
-
#
|
144
|
-
# translate = Google::Cloud::Translate.new(
|
145
|
-
# version: :v2,
|
146
|
-
# project_id: "my-todo-project",
|
147
|
-
# credentials: "/path/to/keyfile.json"
|
148
|
-
# )
|
149
|
-
#
|
150
|
-
# translation = translate.translate "Hello world!", to: "la"
|
151
|
-
# translation.text #=> "Salve mundi!"
|
152
|
-
#
|
153
|
-
# @example Using the legacy v2 client with an API Key.
|
154
|
-
# require "google/cloud/translate"
|
155
|
-
#
|
156
|
-
# translate = Google::Cloud::Translate.new(
|
157
|
-
# version: :v2,
|
158
|
-
# key: "api-key-abc123XYZ789"
|
159
|
-
# )
|
160
|
-
#
|
161
|
-
# translation = translate.translate "Hello world!", to: "la"
|
162
|
-
# translation.text #=> "Salve mundi!"
|
163
|
-
#
|
164
|
-
# @example Using API Key from the environment variable.
|
165
|
-
# require "google/cloud/translate"
|
166
|
-
#
|
167
|
-
# ENV["TRANSLATE_KEY"] = "api-key-abc123XYZ789"
|
168
|
-
#
|
169
|
-
# translate = Google::Cloud::Translate.new version: :v2
|
60
|
+
# Provides natural language translation operations.
|
170
61
|
#
|
171
|
-
#
|
172
|
-
#
|
62
|
+
# @param version [::String, ::Symbol] The API version to connect to. Optional.
|
63
|
+
# Defaults to `:v3`.
|
64
|
+
# @return [TranslationService::Client] A client object for the specified version.
|
173
65
|
#
|
174
|
-
def self.
|
175
|
-
|
176
|
-
raise "The version: #{version} is not available. The available versions " \
|
177
|
-
"are: [#{AVAILABLE_VERSIONS.join ', '}]"
|
178
|
-
end
|
66
|
+
def self.translation_service version: :v3, &block
|
67
|
+
require "google/cloud/translate/#{version.to_s.downcase}"
|
179
68
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
69
|
+
package_name = Google::Cloud::Translate
|
70
|
+
.constants
|
71
|
+
.select { |sym| sym.to_s.downcase == version.to_s.downcase.tr("_", "") }
|
72
|
+
.first
|
73
|
+
package_module = Google::Cloud::Translate.const_get package_name
|
74
|
+
package_module.const_get(:TranslationService).const_get(:Client).new(&block)
|
186
75
|
end
|
187
76
|
|
188
77
|
##
|
189
|
-
# Configure the
|
190
|
-
#
|
191
|
-
# The following
|
192
|
-
#
|
193
|
-
# * `
|
194
|
-
#
|
195
|
-
#
|
196
|
-
#
|
197
|
-
#
|
198
|
-
#
|
199
|
-
#
|
200
|
-
# * `
|
201
|
-
#
|
202
|
-
#
|
203
|
-
#
|
204
|
-
#
|
205
|
-
#
|
78
|
+
# Configure the google-cloud-translate library.
|
79
|
+
#
|
80
|
+
# The following configuration parameters are supported:
|
81
|
+
#
|
82
|
+
# * `credentials` (*type:* `String, Hash, Google::Auth::Credentials`) -
|
83
|
+
# The path to the keyfile as a String, the contents of the keyfile as a
|
84
|
+
# Hash, or a Google::Auth::Credentials object.
|
85
|
+
# * `lib_name` (*type:* `String`) -
|
86
|
+
# The library name as recorded in instrumentation and logging.
|
87
|
+
# * `lib_version` (*type:* `String`) -
|
88
|
+
# The library version as recorded in instrumentation and logging.
|
89
|
+
# * `interceptors` (*type:* `Array<GRPC::ClientInterceptor>`) -
|
90
|
+
# An array of interceptors that are run before calls are executed.
|
91
|
+
# * `timeout` (*type:* `Numeric`) -
|
92
|
+
# Default timeout in seconds.
|
93
|
+
# * `metadata` (*type:* `Hash{Symbol=>String}`) -
|
94
|
+
# Additional gRPC headers to be sent with the call.
|
95
|
+
# * `retry_policy` (*type:* `Hash`) -
|
96
|
+
# The retry policy. The value is a hash with the following keys:
|
97
|
+
# * `:initial_delay` (*type:* `Numeric`) - The initial delay in seconds.
|
98
|
+
# * `:max_delay` (*type:* `Numeric`) - The max delay in seconds.
|
99
|
+
# * `:multiplier` (*type:* `Numeric`) - The incremental backoff multiplier.
|
100
|
+
# * `:retry_codes` (*type:* `Array<String>`) -
|
101
|
+
# The error codes that should trigger a retry.
|
102
|
+
#
|
103
|
+
# @return [::Google::Cloud::Config] The default configuration used by this library
|
206
104
|
#
|
207
105
|
def self.configure
|
208
|
-
yield Google::Cloud.configure.translate if block_given?
|
106
|
+
yield ::Google::Cloud.configure.translate if block_given?
|
209
107
|
|
210
|
-
Google::Cloud.configure.translate
|
108
|
+
::Google::Cloud.configure.translate
|
211
109
|
end
|
212
110
|
end
|
213
111
|
end
|
214
112
|
end
|
113
|
+
|
114
|
+
helper_path = ::File.join __dir__, "translate", "helpers.rb"
|
115
|
+
require "google/cloud/translate/helpers" if ::File.file? helper_path
|