如何在 .net 核心网站中使用 Google Translate API?

问题描述

如何在我的 .NET 核心应用程序中使用 Google Translate api?我试过基本的 html 和文本翻译都工作正常。现在我想将我的整个网站转换成多语言。我怎样才能做到这一点。作为参考,我正在使用 this link

解决方法

例如,您可以编写这样的代码来翻译一个句子:

var sentence = "This is some html text to <strong>translate</strong>!";
// Target language is french
string targetLanguage = "fr";
// Source language is automatically detected
string sourceLanguage = null; 
// We create the TranslationClient
var client = Google.Cloud.Translation.V2.TranslationClient.Create();
// We translate
var response = client.TranslateHtml(sentence,targetLanguage,sourceLanguage);
// Output
Console.WriteLine(response.TranslatedText);
// C&#39;est un texte html à <strong>traduire</strong> !

Code source

Official documentation on how to get started using the Google Translate API in .NET

.NET reference documentation for the Cloud Translation API.