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 不放也可以,不过它 detect langauge 另外收钱,所以如果知道最好还是放
        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。

 

posted @ 2023-10-15 20:02  兴杰  阅读(991)  评论(0)    收藏  举报