Skip to content

Commit e9741dc

Browse files
author
remi Taylor
authored
Merge pull request #47 from GoogleCloudPlatform/translate
Ruby samples for Google Translate API
2 parents 10aa130 + fe28a6d commit e9741dc

8 files changed

Lines changed: 370 additions & 1 deletion

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ env:
3535
- TEST_DIR=bigquery
3636
- TEST_DIR=datastore
3737
- TEST_DIR=logging
38+
- TEST_DIR=translate
3839
- TEST_DIR=language
3940
- TEST_DIR=appengine/endpoints
4041

translate/.rspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--tty --color --format documentation
1+
--tty --color --format documentation --order random

translate/Gemfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2016 Google, Inc
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+
# http://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+
source "https://rubygems.org"
16+
17+
gem "google-cloud-translate"
18+
19+
group :test do
20+
gem "rake"
21+
gem "rubocop"
22+
gem "rspec"
23+
end

translate/Gemfile.lock

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
addressable (2.4.0)
5+
ast (2.3.0)
6+
diff-lcs (1.2.5)
7+
faraday (0.9.2)
8+
multipart-post (>= 1.2, < 3)
9+
google-api-client (0.9.12)
10+
addressable (~> 2.3)
11+
googleauth (~> 0.5)
12+
httpclient (~> 2.7)
13+
hurley (~> 0.1)
14+
memoist (~> 0.11)
15+
mime-types (>= 1.6)
16+
representable (~> 2.3.0)
17+
retriable (~> 2.0)
18+
thor (~> 0.19)
19+
google-cloud-core (0.20.1)
20+
google-cloud-translate (0.20.0)
21+
google-api-client (~> 0.9.11)
22+
google-cloud-core (~> 0.20.0)
23+
googleauth (0.5.1)
24+
faraday (~> 0.9)
25+
jwt (~> 1.4)
26+
logging (~> 2.0)
27+
memoist (~> 0.12)
28+
multi_json (~> 1.11)
29+
os (~> 0.9)
30+
signet (~> 0.7)
31+
httpclient (2.8.2.3)
32+
hurley (0.2)
33+
jwt (1.5.4)
34+
little-plugger (1.1.4)
35+
logging (2.1.0)
36+
little-plugger (~> 1.1)
37+
multi_json (~> 1.10)
38+
memoist (0.15.0)
39+
mime-types (3.1)
40+
mime-types-data (~> 3.2015)
41+
mime-types-data (3.2016.0521)
42+
multi_json (1.12.1)
43+
multipart-post (2.0.0)
44+
os (0.9.6)
45+
parser (2.3.1.2)
46+
ast (~> 2.2)
47+
powerpack (0.1.1)
48+
rainbow (2.1.0)
49+
rake (11.1.2)
50+
representable (2.3.0)
51+
uber (~> 0.0.7)
52+
retriable (2.1.0)
53+
rspec (3.4.0)
54+
rspec-core (~> 3.4.0)
55+
rspec-expectations (~> 3.4.0)
56+
rspec-mocks (~> 3.4.0)
57+
rspec-core (3.4.4)
58+
rspec-support (~> 3.4.0)
59+
rspec-expectations (3.4.0)
60+
diff-lcs (>= 1.2.0, < 2.0)
61+
rspec-support (~> 3.4.0)
62+
rspec-mocks (3.4.1)
63+
diff-lcs (>= 1.2.0, < 2.0)
64+
rspec-support (~> 3.4.0)
65+
rspec-support (3.4.1)
66+
rubocop (0.42.0)
67+
parser (>= 2.3.1.1, < 3.0)
68+
powerpack (~> 0.1)
69+
rainbow (>= 1.99.1, < 3.0)
70+
ruby-progressbar (~> 1.7)
71+
unicode-display_width (~> 1.0, >= 1.0.1)
72+
ruby-progressbar (1.8.1)
73+
signet (0.7.3)
74+
addressable (~> 2.3)
75+
faraday (~> 0.9)
76+
jwt (~> 1.5)
77+
multi_json (~> 1.10)
78+
thor (0.19.1)
79+
uber (0.0.15)
80+
unicode-display_width (1.1.0)
81+
82+
PLATFORMS
83+
ruby
84+
85+
DEPENDENCIES
86+
google-cloud-translate
87+
rake
88+
rspec
89+
rubocop
90+
91+
BUNDLED WITH
92+
1.12.5

translate/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Google Cloud Translate API Ruby Samples
4+
5+
6+
With the [Google Translate API][translate_docs], you can dynamically translate
7+
text between thousands of language pairs.
8+
9+
[translate_docs]: https://cloud.google.com/translate/docs/
10+
11+
## Run sample
12+
13+
To run the sample, first install dependencies:
14+
15+
bundle install
16+
17+
Run the sample:
18+
19+
export TRANSLATE_KEY="Your Translate API key"
20+
21+
bundle exec ruby translate_samples.rb
22+
23+
Usage:
24+
25+
Usage: ruby translate_samples.rb <command> [arguments]
26+
27+
Commands:
28+
translate <desired-language-code> <text>
29+
detect_language <text>
30+
list_names <language-code-for-display>
31+
list_codes
32+
33+
Examples:
34+
ruby translate_samples.rb translate fr "Hello World"
35+
ruby translate_samples.rb detect_language "Hello World"
36+
ruby translate_samples.rb list_codes
37+
ruby translate_samples.rb list_names en

translate/Rakefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2016 Google, Inc
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+
# http://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+
require "rspec/core/rake_task"
16+
RSpec::Core::RakeTask.new(:spec)
17+
18+
require "rubocop/rake_task"
19+
RuboCop::RakeTask.new
20+
21+
task default: :spec
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright 2016 Google, Inc
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+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in write, 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+
require_relative "../translate_samples"
16+
require "rspec"
17+
18+
RSpec.describe "Google Translate API samples" do
19+
20+
before do
21+
@api_key = ENV["TRANSLATE_KEY"]
22+
end
23+
24+
# Capture and return STDOUT output by block
25+
def capture &block
26+
real_stdout = $stdout
27+
$stdout = StringIO.new
28+
block.call
29+
@captured_output = $stdout.string
30+
ensure
31+
$stdout = real_stdout
32+
end
33+
attr_reader :captured_output
34+
35+
example "translate text" do
36+
capture do
37+
translate_text api_key: @api_key,
38+
language_code: "fr",
39+
text: "Alice and Bob are kind"
40+
end
41+
42+
expect(captured_output).to include "Original language: en translated to: fr"
43+
expect(captured_output).to include(
44+
%{Translated 'Alice and Bob are kind' to '"Alice et Bob sont gentils"'}
45+
)
46+
end
47+
48+
example "detect language" do
49+
expect {
50+
detect_language api_key: @api_key, text: "Sample text written in English"
51+
}.to output(
52+
/'Sample text written in English' detected as language: en/
53+
).to_stdout
54+
end
55+
56+
example "list supported language codes" do
57+
capture { list_supported_language_codes api_key: @api_key }
58+
59+
# Check for a few supported language codes (first sorted alphabetically)
60+
expect(captured_output).to include "af"
61+
expect(captured_output).to include "am"
62+
expect(captured_output).to include "ar"
63+
expect(captured_output).to include "az"
64+
expect(captured_output).to include "be"
65+
end
66+
67+
example "list supported language names" do
68+
capture do
69+
list_supported_language_names api_key: @api_key, language_code: "en"
70+
end
71+
72+
# Check for a few supported language codes (first sorted alphabetically)
73+
expect(captured_output).to include "af Afrikaans"
74+
expect(captured_output).to include "am Amharic"
75+
expect(captured_output).to include "ar Arabic"
76+
expect(captured_output).to include "az Azerbaijani"
77+
expect(captured_output).to include "be Belarusian"
78+
end
79+
end

translate/translate_samples.rb

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Copyright 2016 Google, Inc
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+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in write, 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+
def translate_text api_key:, text:, language_code:
16+
# [START translate_text]
17+
# api_key = "Your API access key"
18+
# text = "The text you would like to translate"
19+
# language_code = "The ISO 639-1 code of language to translate to, eg. 'en'"
20+
21+
require "google/cloud"
22+
23+
gcloud = Google::Cloud.new
24+
translate = gcloud.translate api_key
25+
translation = translate.translate text, to: language_code
26+
27+
puts "Translated '#{text}' to '#{translation.text.inspect}'"
28+
puts "Original language: #{translation.from} translated to: #{translation.to}"
29+
# [END translate_text]
30+
end
31+
32+
def detect_language api_key:, text:
33+
# api_key = "Your API access key"
34+
# text = "The text you would like to detect the language of"
35+
36+
require "google/cloud"
37+
38+
gcloud = Google::Cloud.new
39+
translate = gcloud.translate api_key
40+
detection = translate.detect text
41+
42+
puts "'#{text}' detected as language: #{detection.language}"
43+
puts "Confidence: #{detection.confidence}"
44+
end
45+
46+
def list_supported_language_codes api_key:
47+
# [START list_supported_language_codes]
48+
# api_key = "Your API access key"
49+
50+
require "google/cloud"
51+
52+
gcloud = Google::Cloud.new
53+
translate = gcloud.translate api_key
54+
languages = translate.languages
55+
56+
puts "Supported language codes:"
57+
languages.each do |language|
58+
puts language.code
59+
end
60+
# [END list_supported_language_codes]
61+
end
62+
63+
def list_supported_language_names api_key:, language_code: "en"
64+
# [START list_supported_language_names]
65+
# api_key = "Your API access key"
66+
67+
# To receive the names of the supported languages, provide the code
68+
# for the language in which you wish to receive the names
69+
# language_code = "en"
70+
71+
require "google/cloud"
72+
73+
gcloud = Google::Cloud.new
74+
translate = gcloud.translate api_key
75+
languages = translate.languages language_code
76+
77+
puts "Supported languages:"
78+
languages.each do |language|
79+
puts "#{language.code} #{language.name}"
80+
end
81+
# [END list_supported_language_names]
82+
end
83+
84+
if __FILE__ == $PROGRAM_NAME
85+
api_key = ENV["TRANSLATE_KEY"]
86+
command = ARGV.shift
87+
88+
case command
89+
when "translate"
90+
translate_text api_key: api_key,
91+
language_code: ARGV.shift,
92+
text: ARGV.shift
93+
when "detect_language"
94+
detect_language api_key: api_key, text: ARGV.shift
95+
when "list_codes"
96+
list_supported_language_codes api_key: api_key
97+
when "list_names"
98+
list_supported_language_names api_key: api_key, language_code: ARGV.shift
99+
else
100+
puts <<-usage
101+
Usage: ruby translate_samples.rb <command> [arguments]
102+
103+
Commands:
104+
translate <desired-language-code> <text>
105+
detect_language <text>
106+
list_names <language-code-for-display>
107+
list_codes
108+
109+
Examples:
110+
ruby translate_samples.rb translate fr "Hello World"
111+
ruby translate_samples.rb detect_language "Hello World"
112+
ruby translate_samples.rb list_codes
113+
ruby translate_samples.rb list_names en
114+
usage
115+
end
116+
end

0 commit comments

Comments
 (0)