LoginSignup
0
0

More than 3 years have passed since last update.

Ruby: Google Cloud Translation API の使い方 (Basic)

Posted at
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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0