|
| 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