Google – Cloud Translation API

前言

通常網站內容翻譯,我們都不推薦使用 Google Translate。但網站中一些不那麼重要的內容確實可以用 Google Translate。比如 Customer Reviews。

這篇是續 

Google Maps Embed API & JavaScript API

Google – Reviews

YouTube Data API

又一篇關於 Google Cloud API 的教程。請大家先看完上面幾篇,因爲教過的內容我是不重複的。

 

參考

Docs – Cloud Translation

 

v2 basic 和 v3 advanced 版本

Google Translate API 有兩個版本。v2 是舊的,v3 是新的。

我估計 v2 會被完全淘汰掉,所以這篇只會教 v3 而已。

2 個點要說一下:

第一、v2 和 v3 的 API 接口完全不同。

第二、v2 只需要 API Keys 就可以使用了,v3 卻一定要 OAuth 才能使用。

 

Setup

和 Google Reviews、YouTube Data API 類似。

1. Google Cloud Account(要綁定信用卡哦)

2. Google Cloud Project

3. Enable Cloud Translation API

4. OAuth(App, Client id, Client secret),不需要 API Keys 哦。

5. Login by Google Account with scopes:

https://www.googleapis.com/auth/cloud-platform

https://www.googleapis.com/auth/cloud-translation

 

Detect Langauge by Http Request

var accessToken = "access token";
var cloudProjectId = "project id";
var chineseText = "風蕭蕭兮易水寒,壯士一去兮不復還";

var httpRequestMessage = new HttpRequestMessage
{
    Method = HttpMethod.Post,
    Headers = {
        { "Accept", "application/json; charset=utf-8" },
        { "Authorization", $"Bearer {accessToken}" }
    },
    Content = JsonContent.Create(new
    {
        content = chineseText,
    }),
    RequestUri = new Uri($"https://translation.googleapis.com/v3/projects/{cloudProjectId}/locations/global:detectLanguage")
};

var httpClient = httpClientFactory.CreateClient();
var response = await httpClient.SendAsync(httpRequestMessage);
var json = await response.Content.ReadAsStringAsync();
var jObject = JsonSerializer.Deserialize<JsonObject>(json)!;
var languageCode = jObject["languages"]![0]!["languageCode"]!.GetValue<string>(); // zh-CN
var confidence = jObject["languages"]![0]!["confidence"]!.GetValue<decimal>(); // 1

1. 要先有 access token 哦.

2. 中文是 zh-CN、zh-TW(ISO-639),不是 zh-Hans、zh-Hant (ISO 15924)哦,完整的 language code 看這篇

3. 如果中英文混搭,測出來會不太準確。

4. confidence 是準確性,0-1 之間。1 表示 100% 測出語言。

 

Translate Language by Http Request

var accessToken = "access token";
var cloudProjectId = "project id";
var chineseText = "風蕭蕭兮易水寒,壯士一去兮不復還";

var httpRequestMessage = new HttpRequestMessage
{
    Method = HttpMethod.Post,
    Headers = {
        { "Accept", "application/json; charset=utf-8" },
        { "Authorization", $"Bearer {accessToken}" }
    },
    Content = JsonContent.Create(new
    {
        sourceLanguageCode = "zh-CN", // optional 不放也可以
        targetLanguageCode = "en",
        contents = new[] { chineseText }
    }),
    RequestUri = new Uri($"https://translation.googleapis.com/v3/projects/{cloudProjectId}:translateText")
};

var httpClient = httpClientFactory.CreateClient();
var response = await httpClient.SendAsync(httpRequestMessage);
var json = await response.Content.ReadAsStringAsync();
var jObject = JsonSerializer.Deserialize<JsonObject>(json)!;
var englishText = jObject["translations"]![0]!["translatedText"]!.GetValue<string>();
// The wind is rustling and the water is cold. Once the strong man is gone, he will never return.

1. sourceLanguageCode 不是必須的,它自己可以 detect language。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章