Create an API Key

Check my progress

/ 100

This lab may incorporate AI tools to support your learning.

GSP049

Google Cloud 自學實驗室

總覽

Cloud Translation API 採用 Google 的神經機器翻譯技術,將文字即時翻譯為超過一百種語言。

在這個實驗室中,您將使用這個 API 翻譯文字,並偵測該段文字的語言。

課程內容

在本實驗室中,您將使用 Cloud Translation API 執行下列操作:

  • 建立 Cloud Translation API 要求,並使用 curl 呼叫 API
  • 翻譯文字
  • 使用 Premium 版
  • 偵測語言

設定和需求

瞭解以下事項後,再點選「Start Lab」按鈕

請詳閱以下操作說明。實驗室活動會計時,且中途無法暫停。點選「Start Lab」後就會開始計時,顯示可使用 Google Cloud 資源的時間。

您將在真正的雲端環境完成實作實驗室活動,而不是模擬或示範環境。為此,我們會提供新的暫時憑證,供您在實驗室活動期間登入及存取 Google Cloud。

為了順利完成這個實驗室,請先確認:

  • 可以使用標準的網際網路瀏覽器 (Chrome 瀏覽器為佳)。
注意事項:請使用無痕模式 (建議選項) 或私密瀏覽視窗執行此實驗室,這可以防止個人帳戶和學員帳戶之間的衝突,避免個人帳戶產生額外費用。
  • 是時候完成實驗室活動了!別忘了,活動一旦開始將無法暫停。
注意事項:務必使用實驗室專用的學員帳戶。如果使用其他 Google Cloud 帳戶,可能會產生額外費用。

如何開始研究室及登入 Google Cloud 控制台

  1. 點選「Start Lab」按鈕。如果實驗室會產生費用,畫面上會出現選擇付款方式的對話方塊。左側的「Lab Details」窗格會顯示下列項目:

    • 「Open Google Cloud console」按鈕
    • 剩餘時間
    • 必須在這個研究室中使用的臨時憑證
    • 完成這個實驗室所需的其他資訊 (如有)
  2. 點選「Open Google Cloud console」;如果使用 Chrome 瀏覽器,也能按一下滑鼠右鍵,選取「在無痕視窗中開啟連結」

    接著,實驗室會啟動相關資源,並開啟另一個分頁,顯示「登入」頁面。

    提示:您可以在不同的視窗中並排開啟分頁。

    注意:如果頁面中顯示「選擇帳戶」對話方塊,請點選「使用其他帳戶」
  3. 如有必要,請將下方的 Username 貼到「登入」對話方塊。

    {{{user_0.username | "Username"}}}

    您也可以在「Lab Details」窗格找到 Username。

  4. 點選「下一步」

  5. 複製下方的 Password,並貼到「歡迎使用」對話方塊。

    {{{user_0.password | "Password"}}}

    您也可以在「Lab Details」窗格找到 Password。

  6. 點選「下一步」

    重要事項:請務必使用實驗室提供的憑證,而非自己的 Google Cloud 帳戶憑證。 注意:如果使用自己的 Google Cloud 帳戶來進行這個實驗室,可能會產生額外費用。
  7. 按過後續的所有頁面:

    • 接受條款及細則。
    • 由於這是臨時帳戶,請勿新增救援選項或雙重驗證機制。
    • 請勿申請免費試用。

Google Cloud 控制台稍後會在這個分頁開啟。

注意:如要使用 Google Cloud 產品和服務,請點選「導覽選單」,或在「搜尋」欄位輸入服務或產品名稱。「導覽選單」圖示和搜尋欄位

啟動 Cloud Shell

Cloud Shell 是搭載多項開發工具的虛擬機器,提供永久的 5 GB 主目錄,而且在 Google Cloud 中運作。Cloud Shell 提供指令列存取權,方便您使用 Google Cloud 資源。

  1. 點按 Google Cloud 控制台頂端的「啟用 Cloud Shell」圖示 「啟動 Cloud Shell」圖示

  2. 系統顯示視窗時,請按照下列步驟操作:

    • 繼續操作 Cloud Shell 視窗。
    • 授權 Cloud Shell 使用您的憑證發出 Google Cloud API 呼叫。

連線建立完成即代表已通過驗證,而且專案已設為您的 Project_ID。輸出內容中有一行文字,宣告本工作階段的 Project_ID

Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}

gcloud 是 Google Cloud 的指令列工具,已預先安裝於 Cloud Shell,並支援 Tab 鍵自動完成功能。

  1. (選用) 您可以執行下列指令來列出使用中的帳戶:
gcloud auth list
  1. 點按「授權」

輸出內容:

ACTIVE: * ACCOUNT: {{{user_0.username | "ACCOUNT"}}} To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (選用) 您可以使用下列指令來列出專案 ID:
gcloud config list project

輸出內容:

[core] project = {{{project_0.project_id | "PROJECT_ID"}}} 注意:如需 gcloud 的完整說明,請前往 Google Cloud 參閱 gcloud CLI 總覽指南

設定專案區域

請執行以下指令來設定專案區域:

export REGION={{{project_0.default_region | REGION}}} gcloud config set compute/region $REGION

工作 1:建立 API 金鑰

您會在實驗室後續步驟使用 curl 傳送要求至 Translation API,因此必須產生 API 金鑰來傳遞要求網址。

  1. 建立 API 金鑰的第一步是打開「導覽選單」 (「導覽選單」圖示),依序點選「API 和服務」>「憑證」

  2. 按一下「建立憑證」

  3. 在下拉式選單中,選取「API 金鑰」

  4. 複製剛剛產生的金鑰。

接著,將 API 金鑰儲存為環境變數,這樣就不必為每個要求插入金鑰值。

  1. 在 Cloud Shell 執行下列指令:請務必將 <your_api_key> 替換成您剛才複製的金鑰:
export API_KEY= YOUR_API_KEY

點選「Check my progress」,確認目標已達成。 建立 API 金鑰

工作 2:翻譯文字

在這個例子中,您將字串「My name is Steve」翻譯成西班牙文。

  • 使用下列 curl 指令,將要翻譯的文字和 API 金鑰環境變數傳送至 Translation API:
TEXT="My%20name%20is%20Steve" curl "https://translation.googleapis.com/language/translate/v2?target=es&key=${API_KEY}&q=${TEXT}"

回應內容應如下:

{ "data": { "translations": [ { "translatedText": "Mi nombre es Steve", "detectedSourceLanguage": "en" } ] } }

在回覆中,您可以看到翻譯後的文字和 API 偵測到的來源語言。

注意:Premium 模式

Google Cloud Translation API 使用標準版模型執行大部分的翻譯工作。Google 強化翻譯服務,採用更強大的神經機器翻譯系統。如要進一步瞭解如何使用這個進階模型,請參閱 Cloud Translation 說明文件

工作 3:偵測語言

除了翻譯文字,Translation API 還能偵測文字的語言。在這個例子中,您會偵測兩個字串的語言。

  • 使用下列 curl 指令,將要檢查的文字和 API 金鑰環境變數傳送至 Translation API:
TEXT_ONE="Meu%20nome%20é%20Steven" TEXT_TWO="日本のグーグルのオフィスは、東京の六本木ヒルズにあります" curl -X POST "https://translation.googleapis.com/language/translate/v2/detect?key=${API_KEY}" -d "q=${TEXT_ONE}" -d "q=${TEXT_TWO}"

回應內容應如下:

{ "data": { "detections": [ [ { "isReliable": false, "language": "pt", "confidence": 1 } ], [ { "language": "ja", "isReliable": false, "confidence": 1 } ] ] } }

這個例子傳回的語言是「pt」和「ja」,即葡萄牙文和日文的 ISO-639-1 識別碼。這份 Translation API 支援的語言清單列出所有可能傳回的語言代碼。

恭喜!

您已學會如何使用 Cloud Translation API 翻譯文字!

挑戰下一個實驗室

後續步驟/瞭解詳情

Google Cloud 教育訓練與認證

協助您瞭解如何充分運用 Google Cloud 的技術。我們的課程會介紹專業技能和最佳做法,讓您可以快速掌握要領並持續進修。我們提供從基本到進階等級的訓練課程,並有隨選、線上和虛擬課程等選項,方便您抽空參加。認證可協助您驗證及證明自己在 Google Cloud 技術方面的技能和專業知識。

使用手冊上次更新日期:2024 年 12 月 23 日

實驗室上次測試日期:2024 年 12 月 23 日

Copyright 2026 Google LLC 保留所有權利。Google 和 Google 標誌是 Google LLC 的商標,其他公司和產品名稱則有可能是其關聯公司的商標。

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

Use private browsing

  1. Copy the provided Username and Password for the lab
  2. Click Open console in private mode

Sign in to the Console

  1. Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
  2. Accept the terms, and skip the recovery resource page
  3. Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project

This content is not currently available

We will notify you via email when it becomes available

Great!

We will contact you via email if it becomes available

One lab at a time

Confirm to end all existing labs and start this one

Use private browsing to run the lab

Using an Incognito or private browser window is the best way to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.

Complete this quick step to start your lab.