Skip to content

Translate API no longer requires an API key. #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 10 additions & 26 deletions translate/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.4.0)
addressable (2.5.0)
public_suffix (~> 2.0, >= 2.0.2)
diff-lcs (1.2.5)
faraday (0.9.2)
faraday (0.10.0)
multipart-post (>= 1.2, < 3)
google-api-client (0.9.15)
addressable (~> 2.3)
googleauth (~> 0.5)
httpclient (~> 2.7)
hurley (~> 0.1)
memoist (~> 0.11)
mime-types (>= 1.6)
representable (~> 2.3.0)
retriable (~> 2.0)
google-cloud-core (0.20.1)
google-cloud-translate (0.20.1)
google-api-client (~> 0.9.11)
google-cloud-core (~> 0.20.0)
google-cloud-core (0.21.0)
google-cloud-translate (0.22.1)
google-cloud-core (~> 0.21.0)
googleauth (~> 0.5.1)
googleauth (0.5.1)
faraday (~> 0.9)
jwt (~> 1.4)
Expand All @@ -26,28 +18,21 @@ GEM
multi_json (~> 1.11)
os (~> 0.9)
signet (~> 0.7)
httpclient (2.8.2.4)
hurley (0.2)
jwt (1.5.6)
little-plugger (1.1.4)
logging (2.1.0)
little-plugger (~> 1.1)
multi_json (~> 1.10)
memoist (0.15.0)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
multi_json (1.12.1)
multipart-post (2.0.0)
os (0.9.6)
representable (2.3.0)
uber (~> 0.0.7)
retriable (2.1.0)
public_suffix (2.0.4)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.3)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
Expand All @@ -61,7 +46,6 @@ GEM
faraday (~> 0.9)
jwt (~> 1.5)
multi_json (~> 1.10)
uber (0.0.15)

PLATFORMS
ruby
Expand All @@ -71,4 +55,4 @@ DEPENDENCIES
rspec

BUNDLED WITH
1.12.5
1.13.6
8 changes: 4 additions & 4 deletions translate/quickstart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
# Imports the Google Cloud client library
require "google/cloud"

# Your Translate API key
api_key = "YOUR_API_KEY"
# Your Google Cloud Platform project ID
project_id = "YOUR_PROJECT_ID"

# Instantiates a client
gcloud = Google::Cloud.new
translate = gcloud.translate api_key
gcloud = Google::Cloud.new project_id
translate = gcloud.translate

# The text to translate
text = "Hello, world!"
Expand Down
8 changes: 3 additions & 5 deletions translate/spec/quickstart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
describe "Translate Quickstart" do

it "translates Hello, world! to Russian" do
gcloud = Google::Cloud.new
translate = gcloud.translate ENV["TRANSLATE_API_KEY"]
expect(Google::Cloud).to receive(:new).and_return(gcloud)
expect(gcloud).to receive(:translate).with("YOUR_API_KEY").
and_return(translate)
gcloud = Google::Cloud.new ENV["GOOGLE_CLOUD_PROJECT"]
expect(Google::Cloud).to receive(:new).with("YOUR_PROJECT_ID").
and_return(gcloud)

expect {
load File.expand_path("../quickstart.rb", __dir__)
Expand Down
10 changes: 5 additions & 5 deletions translate/spec/translate_samples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
describe "Google Translate API samples" do

before do
@api_key = ENV["TRANSLATE_API_KEY"]
@project_id = ENV["GOOGLE_CLOUD_PROJECT"]
end

# Capture and return STDOUT output by block
Expand All @@ -34,7 +34,7 @@ def capture &block

example "translate text" do
capture do
translate_text api_key: @api_key,
translate_text project_id: @project_id,
language_code: "fr",
text: "Alice and Bob are kind"
end
Expand All @@ -47,14 +47,14 @@ def capture &block

example "detect language" do
expect {
detect_language api_key: @api_key, text: "Sample text written in English"
detect_language project_id: @project_id, text: "Sample text written in English"
}.to output(
/'Sample text written in English' detected as language: en/
).to_stdout
end

example "list supported language codes" do
capture { list_supported_language_codes api_key: @api_key }
capture { list_supported_language_codes project_id: @project_id }

# Check for a few supported language codes (first sorted alphabetically)
expect(captured_output).to include "af"
Expand All @@ -66,7 +66,7 @@ def capture &block

example "list supported language names" do
capture do
list_supported_language_names api_key: @api_key, language_code: "en"
list_supported_language_names project_id: @project_id, language_code: "en"
end

# Check for a few supported language codes (first sorted alphabetically)
Expand Down
44 changes: 23 additions & 21 deletions translate/translate_samples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,47 @@
# See the License for the specific language governing permissions and
# limitations under the License.

def translate_text api_key:, text:, language_code:
def translate_text project_id:, text:, language_code:
# [START translate_text]
# api_key = "Your API access key"
# project_id = "Your Google Cloud project ID"
# text = "The text you would like to translate"
# language_code = "The ISO 639-1 code of language to translate to, eg. 'en'"

require "google/cloud"

gcloud = Google::Cloud.new
translate = gcloud.translate api_key
gcloud = Google::Cloud.new project_id
translate = gcloud.translate
translation = translate.translate text, to: language_code

puts "Translated '#{text}' to '#{translation.text.inspect}'"
puts "Original language: #{translation.from} translated to: #{translation.to}"
# [END translate_text]
end

def detect_language api_key:, text:
def detect_language project_id:, text:
# [START detect_language]
# api_key = "Your API access key"
# project_id = "Your Google Cloud project ID"
# text = "The text you would like to detect the language of"

require "google/cloud"

gcloud = Google::Cloud.new
translate = gcloud.translate api_key
gcloud = Google::Cloud.new project_id
translate = gcloud.translate
detection = translate.detect text

puts "'#{text}' detected as language: #{detection.language}"
puts "Confidence: #{detection.confidence}"
# [END detect_language]
end

def list_supported_language_codes api_key:
def list_supported_language_codes project_id:
# [START list_supported_language_codes]
# api_key = "Your API access key"
# project_id = "Your Google Cloud project ID"

require "google/cloud"

gcloud = Google::Cloud.new
translate = gcloud.translate api_key
gcloud = Google::Cloud.new project_id
translate = gcloud.translate
languages = translate.languages

puts "Supported language codes:"
Expand All @@ -62,18 +62,18 @@ def list_supported_language_codes api_key:
# [END list_supported_language_codes]
end

def list_supported_language_names api_key:, language_code: "en"
def list_supported_language_names project_id:, language_code: "en"
# [START list_supported_language_names]
# api_key = "Your API access key"
# project_id = "Your Google Cloud project ID"

# To receive the names of the supported languages, provide the code
# for the language in which you wish to receive the names
# language_code = "en"

require "google/cloud"

gcloud = Google::Cloud.new
translate = gcloud.translate api_key
gcloud = Google::Cloud.new project_id
translate = gcloud.translate
languages = translate.languages language_code

puts "Supported languages:"
Expand All @@ -84,20 +84,22 @@ def list_supported_language_names api_key:, language_code: "en"
end

if __FILE__ == $PROGRAM_NAME
api_key = ENV["TRANSLATE_API_KEY"]
project_id = ENV["GOOGLE_CLOUD_PROJECT"]
command = ARGV.shift

case command
when "translate"
translate_text api_key: api_key,
translate_text project_id: project_id,
language_code: ARGV.shift,
text: ARGV.shift
when "detect_language"
detect_language api_key: api_key, text: ARGV.shift
detect_language project_id: project_id,
text: ARGV.shift
when "list_codes"
list_supported_language_codes api_key: api_key
list_supported_language_codes project_id: project_id
when "list_names"
list_supported_language_names api_key: api_key, language_code: ARGV.shift
list_supported_language_names project_id: project_id,
language_code: ARGV.shift
else
puts <<-usage
Usage: ruby translate_samples.rb <command> [arguments]
Expand Down