1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- def translate_text api_key :, text :, language_code :
15+ def translate_text project_id :, text :, language_code :
1616 # [START translate_text]
17- # api_key = "Your API access key "
17+ # project_id = "Your Google Cloud project ID "
1818 # text = "The text you would like to translate"
1919 # language_code = "The ISO 639-1 code of language to translate to, eg. 'en'"
2020
2121 require "google/cloud"
2222
23- gcloud = Google ::Cloud . new
24- translate = gcloud . translate api_key
23+ gcloud = Google ::Cloud . new project_id
24+ translate = gcloud . translate
2525 translation = translate . translate text , to : language_code
2626
2727 puts "Translated '#{ text } ' to '#{ translation . text . inspect } '"
2828 puts "Original language: #{ translation . from } translated to: #{ translation . to } "
2929 # [END translate_text]
3030end
3131
32- def detect_language api_key :, text :
32+ def detect_language project_id :, text :
3333 # [START detect_language]
34- # api_key = "Your API access key "
34+ # project_id = "Your Google Cloud project ID "
3535 # text = "The text you would like to detect the language of"
3636
3737 require "google/cloud"
3838
39- gcloud = Google ::Cloud . new
40- translate = gcloud . translate api_key
39+ gcloud = Google ::Cloud . new project_id
40+ translate = gcloud . translate
4141 detection = translate . detect text
4242
4343 puts "'#{ text } ' detected as language: #{ detection . language } "
4444 puts "Confidence: #{ detection . confidence } "
4545 # [END detect_language]
4646end
4747
48- def list_supported_language_codes api_key :
48+ def list_supported_language_codes project_id :
4949 # [START list_supported_language_codes]
50- # api_key = "Your API access key "
50+ # project_id = "Your Google Cloud project ID "
5151
5252 require "google/cloud"
5353
54- gcloud = Google ::Cloud . new
55- translate = gcloud . translate api_key
54+ gcloud = Google ::Cloud . new project_id
55+ translate = gcloud . translate
5656 languages = translate . languages
5757
5858 puts "Supported language codes:"
@@ -62,18 +62,18 @@ def list_supported_language_codes api_key:
6262 # [END list_supported_language_codes]
6363end
6464
65- def list_supported_language_names api_key :, language_code : "en"
65+ def list_supported_language_names project_id :, language_code : "en"
6666 # [START list_supported_language_names]
67- # api_key = "Your API access key "
67+ # project_id = "Your Google Cloud project ID "
6868
6969 # To receive the names of the supported languages, provide the code
7070 # for the language in which you wish to receive the names
7171 # language_code = "en"
7272
7373 require "google/cloud"
7474
75- gcloud = Google ::Cloud . new
76- translate = gcloud . translate api_key
75+ gcloud = Google ::Cloud . new project_id
76+ translate = gcloud . translate
7777 languages = translate . languages language_code
7878
7979 puts "Supported languages:"
@@ -84,20 +84,22 @@ def list_supported_language_names api_key:, language_code: "en"
8484end
8585
8686if __FILE__ == $PROGRAM_NAME
87- api_key = ENV [ "TRANSLATE_API_KEY " ]
87+ project_id = ENV [ "GOOGLE_CLOUD_PROJECT " ]
8888 command = ARGV . shift
8989
9090 case command
9191 when "translate"
92- translate_text api_key : api_key ,
92+ translate_text project_id : project_id ,
9393 language_code : ARGV . shift ,
9494 text : ARGV . shift
9595 when "detect_language"
96- detect_language api_key : api_key , text : ARGV . shift
96+ detect_language project_id : project_id ,
97+ text : ARGV . shift
9798 when "list_codes"
98- list_supported_language_codes api_key : api_key
99+ list_supported_language_codes project_id : project_id
99100 when "list_names"
100- list_supported_language_names api_key : api_key , language_code : ARGV . shift
101+ list_supported_language_names project_id : project_id ,
102+ language_code : ARGV . shift
101103 else
102104 puts <<-usage
103105Usage: ruby translate_samples.rb <command> [arguments]
0 commit comments