設定

如要開始使用 Cloud Translation,您需要啟用 Cloud Translation API 的專案,以及用於發出已驗證呼叫的憑證。以下各節將詳細說明如何完成設定,再發出第一個 Cloud Translation API 呼叫。

設定流程會用到 Google Cloud 控制台,這個網頁介面可讓您佈建、設定、管理及監控Google Cloud中的系統。如果您從未使用過 Google Cloud 控制台,請參閱「Cloud 控制台」頁面瞭解詳情。

事前準備

Cloud Translation 提供兩種版本 (BasicAdvanced)。請決定要使用哪個版本,因為每個版本都有專屬的用戶端程式庫。

建立或選取專案

如要使用 Google Cloud提供的服務,您必須建立專案。專案可整理您所有的 Google Cloud 資源。專案是由一組協作者、已啟用的 API (和其他資源)、監控工具、帳單資訊,以及驗證和存取權控管組成。您可以建立一個專案,也可以建立多個專案,並使用這些專案在 Google Cloud 資源階層中整理您的資源。建立專案時,請記下專案 ID,因為您需要這個 ID 才能進行 API 呼叫。 如要進一步瞭解專案,請參閱 Resource Manager 說明文件

建議您建立不同的專案,以用於實驗、測試和實際工作環境。建立專案後,請記下專案編號或 ID。呼叫 Cloud Translation API 時,您必須提供專案編號 (或 ID)。

In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

Go to project selector

啟用計費功能

您必須啟用計費功能,才能使用 Cloud Translation。如要進一步瞭解 Cloud Translation 的計價方式,請參閱定價頁面

帳單帳戶可用來定義特定資源組合費用的支付者,其可連結至一或多個專案。專案的使用費會從相連結的帳單帳戶中扣除。在大多數情況下,您會在建立專案時設定帳單。詳情請參閱帳單說明文件

Make sure that billing is enabled for your Google Cloud project.

啟用 API

您必須為專案啟用 Cloud Translation API。 如要進一步瞭解如何啟用 API,請參閱服務使用情形說明文件

Enable the Cloud Translation API.

Enable the API

設定用量配額

Cloud Translation 設有配額管理系統,可協助您和 Google 管理服務用量。根據預設,你每天可以傳送的字元數沒有限制。如果您已設定預算,建議您監控每日用量,並根據預計支出調整配額。

如要進一步瞭解配額,請參閱「配額與限制」。

設定驗證方法

如果您打算使用 Cloud Translation API,則必須設定驗證方法。任何使用該 API 的用戶端應用程式都必須經過驗證,並獲得所要求資源的存取權。詳情請參閱「驗證 Cloud Translation」。

身分與存取權管理角色

當主體 (使用者帳戶、服務帳戶等) 呼叫 API 時,Google Cloud 會要求主體具備適當的權限。您可以將角色授予主體,藉此授予權限。詳情請參閱「使用 IAM 控管存取權」。

安裝用戶端程式庫

我們提供多種常見語言的用戶端程式庫。建議您使用這些用戶端程式庫呼叫 Cloud Translation,因為這樣可以更輕鬆地存取 API。如果用戶端程式庫無法滿足您的需求,或不支援您使用的語言,請自行建構自訂程式碼。詳情請參閱使用自己的用戶端程式碼

舉例來說,使用用戶端程式庫時,程式庫可以透過 GOOGLE_APPLICATION_CREDENTIALS 環境變數,以隱含方式判斷您的憑證。您不需要在程式碼中明確指定憑證。詳情請參閱 Cloud API 說明文件中的「用戶端程式庫說明」。

Cloud Translation - Basic 用戶端程式庫

選取要用於開發作業的程式設計語言,然後執行相關指令。詳情請參閱 Cloud Translation - Basic 用戶端程式庫總覽

C#

nuget install Google.Cloud.Translation.V2

Go

go get cloud.google.com/go/translate

Java

If you are using Maven, add the following to your pom.xml file. For more information about BOMs, see The Google Cloud Platform Libraries BOM.

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>26.61.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-translate</artifactId>
  </dependency>
</dependencies>

If you are using Gradle, add the following to your dependencies:

implementation 'com.google.cloud:google-cloud-translate:2.66.0'

If you are using sbt, add the following to your dependencies:

libraryDependencies += "com.google.cloud" % "google-cloud-translate" % "2.66.0"

If you're using Visual Studio Code, IntelliJ, or Eclipse, you can add client libraries to your project using the following IDE plugins:

The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.

Node.js

如要進一步瞭解如何設定 Node.js 開發環境,請參閱 Node.js 開發環境設定指南

npm install @google-cloud/translate

PHP

composer require google/cloud-translate

Python

如要進一步瞭解如何設定 Python 開發環境,請參閱 Python 開發環境設定指南

pip install google-cloud-translate==2.0.1

Ruby

如要進一步瞭解如何設定 Ruby 開發環境,請參閱 Ruby 開發環境設定指南

gem install google-cloud-translate-v2

Cloud Translation - Advanced 用戶端程式庫

選取要用於開發作業的程式設計語言,然後執行相關指令。詳情請參閱 Cloud Translation - Advanced 用戶端程式庫總覽

C#

nuget install Google.Cloud.Translate.V3 -Pre

Go

go get cloud.google.com/go/translate/apiv3

Java

If you are using Maven, add the following to your pom.xml file. For more information about BOMs, see The Google Cloud Platform Libraries BOM.

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>26.61.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-translate</artifactId>
  </dependency>
</dependencies>

If you are using Gradle, add the following to your dependencies:

implementation 'com.google.cloud:google-cloud-translate:2.66.0'

If you are using sbt, add the following to your dependencies:

libraryDependencies += "com.google.cloud" % "google-cloud-translate" % "2.66.0"

If you're using Visual Studio Code, IntelliJ, or Eclipse, you can add client libraries to your project using the following IDE plugins:

The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.

Node.js

如要進一步瞭解如何設定 Node.js 開發環境,請參閱 Node.js 開發環境設定指南

npm install @google-cloud/translate

PHP

composer require google/cloud-translate

Python

如要進一步瞭解如何設定 Python 開發環境,請參閱 Python 開發環境設定指南

pip install --upgrade google-cloud-translate

Ruby

如要進一步瞭解如何設定 Ruby 開發環境,請參閱 Ruby 開發環境設定指南

gem install google-cloud-translate

安裝 Google Cloud CLI

如要透過指令列 (例如透過 curl 指令) 傳送 Cloud Translation 要求,請安裝並初始化 gcloud CLI,這項工具包含在 Google Cloud CLI 中。如要瞭解詳情,請點選以下連結:

After installing the Google Cloud CLI, initialize it by running the following command:

gcloud init

If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

測試 SDK 和驗證

如果您已設定 GOOGLE_APPLICATION_CREDENTIALS 環境變數,可以使用 gcloud 測試驗證環境。執行下列指令,並確認沒有發生任何錯誤,且會傳回憑證:

gcloud auth application-default print-access-token

所有 Cloud Translation 指令列 REST 範例都會使用該指令來驗證 API 呼叫。

後續步驟

歡迎試用

如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用 Cloud Translation 的成效。新客戶可以獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。

免費試用 Cloud Translation