basic01.rb
# ! /usr/bin/ruby
# -*- encoding: utf-8 -*-
#
# basic01.rb
#
# Nov/26/2020
# ---------------------------------------------------------------------
project_id = "project-translation"
text = "Es war einmal ein kleines Mädchen."
language_code = "ja"
require "google/cloud/translate"
translate = Google::Cloud::Translate.translation_v2_service project_id: project_id
translation = translate.translate text, to: language_code
puts "Translated '#{text}' to '#{translation.text.inspect}'"
puts "Original language: #{translation.from} translated to: #{translation.to}"
#
# ---------------------------------------------------------------------
実行方法
export GOOGLE_APPLICATION_CREDENTIALS=./***.json
#
./basic01.rb
実行結果
Translated 'Es war einmal ein kleines Mädchen.' to '"昔々、小さな女の子がいました。"'
Original language: de translated to: ja