LoginSignup
1
0

More than 1 year has passed since last update.

Google Cloud Translation を利用したテキスト翻訳

Posted at

概要

Google Cloud Translation を利用してテキストを翻訳する手順を解説します。
Google Cloud Translation は課金設定が必要ですが、月あたり500,000 文字まで無料です。
Google Cloud Translation は有料サービスだけに、高品質で高速な翻訳結果が得られます。

[利用までの手順]

  1. Google Cloud プロジェクトの作成
  2. Cloud Translation API の有効化
  3. サービス アカウントとキーの作成
  4. サービス アカウント キー ファイルの適用
  5. クライアント ライブラリのインストール
  6. 翻訳実行

Google Cloud プロジェクトの作成

https://console.cloud.google.com/projectselector2/home/dashboard
プロジェクトの作成
image
image

Cloud Translation API の有効化

https://cloud.google.com/translate/docs/setup#api

image
image
image
image

サービス アカウントとキーの作成

https://cloud.google.com/translate/docs/setup#creating_service_accounts_and_keys

image
image
image
image
image
image
image
image
image
image
image
上の操作でダウンロードされるjsonファイルを任意の場所へ保存します。

サービス アカウント キー ファイルの適用

https://cloud.google.com/translate/docs/setup#using_the_service_account_key_file_in_your_environment

環境変数"GOOGLE_APPLICATION_CREDENTIALS"へキーファイルのパスを設定します。

image
image

クライアント ライブラリのインストール

https://cloud.google.com/translate/docs/setup#installing_client_libraries

pip install google-cloud-translate==2.0.1

翻訳実行

https://cloud.google.com/translate/docs/basic/translating-text#translating_text

from google.cloud import translate_v2 as translate

def translate():
    text = "test translate"

    print(text)
    translate_client = translate.Client()
    
    target = "ja"
    translation = translate_client.translate(text, target_language=target)
    if 'translatedText' in translation:
        translated_text = translation['translatedText']
        print(translated_text)

translate()

👇参考URL

Google Cloud Translation を利用したテキスト翻訳

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