1. 總覽
Google Cloud Translation API 提供簡單的程式介面,可使用最先進的神經機器翻譯技術,將任意字串動態翻譯成任何支援的語言。如果原文語言不明,也可以使用這項功能偵測語言。
在本程式碼研究室中,您將著重於使用 C# 搭配 Translation API。您將瞭解如何列出可用語言、翻譯文字,以及偵測指定文字的語言。
課程內容
- 如何使用 Cloud Shell
- 如何啟用 Translation API
- 如何驗證 API 要求
- 如何安裝 C# 適用的 Google Cloud 用戶端程式庫
- 如何列出可用語言
- 如何翻譯文字
- 如何偵測語言
軟硬體需求
問卷調查
您會如何使用本教學課程?
您對 C# 的體驗評價如何?
您對使用 Google Cloud Platform 服務的體驗有何評價?
2. 設定和需求
自修實驗室環境設定
- 登入 Google Cloud 控制台,然後建立新專案或重複使用現有專案。如果沒有 Gmail 或 Google Workspace 帳戶,請先建立帳戶。



- 專案名稱是這個專案參與者的顯示名稱。這是 Google API 未使用的字元字串。你隨時可以更新。
- 專案 ID 在所有 Google Cloud 專案中都是不重複的,而且設定後即無法變更。Cloud 控制台會自動產生專屬字串,通常您不需要在意該字串為何。在大多數程式碼研究室中,您需要參照專案 ID (通常標示為
PROJECT_ID)。如果您不喜歡產生的 ID,可以產生另一個隨機 ID。你也可以嘗試使用自己的名稱,看看是否可用。完成這個步驟後就無法變更,且專案期間會維持不變。 - 請注意,有些 API 會使用第三個值,也就是「專案編號」。如要進一步瞭解這三種值,請參閱說明文件。
- 接著,您需要在 Cloud 控制台中啟用帳單,才能使用 Cloud 資源/API。完成這個程式碼研究室的費用不高,甚至可能完全免費。如要關閉資源,避免在本教學課程結束後繼續產生費用,請刪除您建立的資源或專案。Google Cloud 新使用者可參加價值$300 美元的免費試用計畫。
啟動 Cloud Shell
雖然可以透過筆電遠端操作 Google Cloud,但在本程式碼研究室中,您將使用 Google Cloud Shell,這是可在雲端執行的指令列環境。
在 Google Cloud 控制台中,點選右上工具列的 Cloud Shell 圖示:

佈建並連線至環境的作業需要一些時間才能完成。完成後,您應該會看到如下的內容:

這部虛擬機器搭載各種您需要的開發工具,並提供永久的 5GB 主目錄,而且可在 Google Cloud 運作,大幅提升網路效能並強化驗證功能。您可以在瀏覽器中完成本程式碼研究室的所有作業。您不需要安裝任何軟體。
3. 啟用 Translation API
您必須先啟用 API,才能使用 Translation API。您可以在 Cloud Shell 中使用下列指令啟用 API:
gcloud services enable translate.googleapis.com
4. 安裝 C# 專用的 Google Cloud Translation API 用戶端程式庫
首先,請建立簡單的 C# 控制台應用程式,用來執行 Translation API 範例。
dotnet new console -n TranslationApiDemo
The template "Console Application" was created successfully.
Processing post-creation actions...
...
Restore succeeded.
接著前往 TranslationApiDemo 資料夾,並將 Google.Cloud.Translation.V2 NuGet 套件新增至專案:
cd TranslationApiDemo/
dotnet add package Google.Cloud.Translation.V2
info : Adding PackageReference for package 'Google.Cloud.Translation.V2' into project '/home/atameldev/TranslationDemo/TranslationDemo.csproj'.
log : Restoring packages for /home/atameldev/TranslationDemo/TranslationDemo.csproj...
...
info : PackageReference for package 'Google.Cloud.Translation.V2' version '1.0.0' added to file '/home/atameldev/TranslationDemo/TranslationDemo.csproj'.
您現在可以使用 Translation API 了!
5. 列出可用語言
在本節中,您會先列出 Translation API 中所有可用的語言。
首先,從 Cloud Shell 右上方開啟程式碼編輯器:

前往 TranslationApiDemo 資料夾中的 Program.cs 檔案,並將程式碼替換成以下程式碼:
using System;
using Google.Cloud.Translation.V2;
namespace TranslationApiDemo
{
class Program
{
static void Main(string[] args)
{
var client = TranslationClient.Create();
foreach (var language in client.ListLanguages(LanguageCodes.English))
{
Console.WriteLine($"{language.Code}\t{language.Name}");
}
}
}
}
請花一兩分鐘研究程式碼。請注意,我們是以英文列出語言名稱,但也可以使用任何語言。
返回 Cloud Shell 執行應用程式,您應該會看到下列輸出內容:
dotnet run
af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
...
yi Yiddish
yo Yoruba
zu Zulu
摘要
在這個步驟中,您已列出 Translation API 中所有可用的語言。如需支援語言的完整清單,請前往「語言支援」頁面。
6. 翻譯文字
您可以使用 Translate API,將某種語言的文字翻譯成另一種語言。系統會使用神經機器翻譯 (NMT) 模型翻譯文字。如果 NMT 模型不支援要求的語言翻譯組合,系統便會使用詞組式機器翻譯 (PBMT) 模型。
如要翻譯文字,請前往 TranslationApiDemo 資料夾中的 Program.cs 檔案,然後將程式碼替換為下列程式碼:
using System;
using Google.Cloud.Translation.V2;
namespace TranslationApiDemo
{
class Program
{
static void Main(string[] args)
{
var client = TranslationClient.Create();
var text = "Hello World!";
var response = client.TranslateText(text, LanguageCodes.Turkish, LanguageCodes.English);
Console.WriteLine(response.TranslatedText);
}
}
}
請花一兩分鐘研究程式碼。將「Hello World」從英文翻譯成土耳其文*。*
返回 Cloud Shell 執行應用程式,您應該會看到下列輸出內容:
dotnet run
Selam Dünya!
摘要
在這個步驟中,您已使用 Translation API 將英文文字翻譯成土耳其文。進一步瞭解如何翻譯文字。
7. 偵測語言
您也可以使用 Translate API 偵測文字字串的語言。
如要偵測語言,請前往 TranslationApiDemo 資料夾中的 Program.cs 檔案,並將程式碼替換成以下程式碼:
using System;
using Google.Cloud.Translation.V2;
namespace TranslationApiDemo
{
class Program
{
static void Main(string[] args)
{
var client = TranslationClient.Create();
var text = "Selam Dünya!";
var detection = client.DetectLanguage(text);
Console.WriteLine($"Language: {detection.Language}\tConfidence: {detection.Confidence}");
}
}
}
請花一兩分鐘研究程式碼。這會偵測「Selam Dünya!」文字的語言,而這剛好是土耳其文片語。
返回 Cloud Shell 執行應用程式,您應該會看到下列輸出內容:
dotnet run
Language: tr Confidence: 1
摘要
在這個步驟中,您已使用 Translation API 偵測一段文字的語言。進一步瞭解偵測語言。
8. 恭喜!
您已瞭解如何使用 C# 呼叫 Translation API!
清除所用資源
如何避免系統向您的 Google Cloud Platform 帳戶收取您在本快速入門導覽課程中所用資源的相關費用:
- 前往 Cloud Platform Console。
- 選取要關閉的專案,然後按一下頂端的「刪除」,系統就會排定刪除專案的時間。
瞭解詳情
- Google Cloud Translation API:https://cloud.google.com/translate/docs
- Google Cloud Platform 上的 C#/.NET:https://cloud.google.com/dotnet/
- Google Cloud .NET 用戶端:https://googlecloudplatform.github.io/google-cloud-dotnet/
授權
這項內容採用的授權為 Creative Commons 姓名標示 2.0 通用授權。