advanced01.rb
#! /usr/bin/ruby
# -*- encoding: utf-8 -*-
#
# advanced01.rb
#
# Nov/26/2020
# ---------------------------------------------------------------------
require "google/cloud/translate"
project_id = "project-translation"
text = "Es war einmal ein kleines Mädchen."
target_language = "ja"
location_id = "global"
contents = [text]
client = Google::Cloud::Translate.translation_service
parent = client.location_path project: project_id, location: location_id
response = client.translate_text parent: parent,
contents: contents,
target_language_code: target_language
#
response.translations.each do |translation|
puts "Translated text: #{translation.translated_text}"
end
#
# ---------------------------------------------------------------------
実行方法
export GOOGLE_APPLICATION_CREDENTIALS=./***.json
#
./advanced01.rb
実行結果
Translated text: 昔々、小さな女の子がいました。