Google.Cloud.Translation.V2
Google.Cloud.Translation.V2
is a.NET client library for the Google Cloud Translation API.
It wraps the Google.Apis.Translate.v2
generated library, providing a higher-level API to make it easier to use.
Note:
This documentation is for version 3.4.0
of the library.
Some samples may not work with other versions.
Installation
Install the Google.Cloud.Translation.V2
package from NuGet. Add it to
your project in the normal way (for example by right-clicking on the
project in Visual Studio and choosing "Manage NuGet Packages...").
Authentication
When running on Google Cloud, no action needs to be taken to authenticate.
Otherwise, the simplest way of authenticating your API calls is to set up Application Default Credentials. The credentials will automatically be used to authenticate. See Set up Application Default Credentials for more details.
Alternatively, you can create a client using an API key. This is primarily supported to enable smooth migration from earlier client libraries; using OAuth2 as above is the preferred authentication mechanism.
Getting started
Common operations are exposed via the TranslationClient and AdvancedTranslationClient classes.
Client life-cycle management
In many cases you don't need to worry about disposing of
TranslationClient
and AdvancedTranslationClient
objects, and can create them reasonably freely -
but be aware that this can causes issues with memory and network
connection usage. We advise you to reuse a single client object if
possible; if your architecture requires you to frequently create new
client objects, please dispose of them to help with timely resource
clean-up. See the resource clean-up guide for more
details.
Sample code
Translating a single item of text
TranslationClient client = TranslationClient.Create();
TranslationResult result = client.TranslateText("It is raining.", LanguageCodes.French);
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");
Translating a single item of HTML
TranslationClient client = TranslationClient.Create();
TranslationResult result = client.TranslateHtml("<p><strong>It is raining.</strong></p>", LanguageCodes.French);
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");
Translating multiple items of text
TranslationClient client = TranslationClient.Create();
IList<TranslationResult> results = client.TranslateText(
new[] { "It is raining.", "It is sunny." },
LanguageCodes.French);
foreach (TranslationResult result in results)
{
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");
}
Translating multiple items of HTML
TranslationClient client = TranslationClient.Create();
IList<TranslationResult> results = client.TranslateHtml(
new[] { "<p><strong>It is raining.</strong></p>", "<p><strong>It is sunny.</strong></p>" },
LanguageCodes.French);
foreach (TranslationResult result in results)
{
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");
}
Specifying a translation model
The Translation API is implemented by multiple underlying models. At the time of writing, one model is available in addition to "base": Neural Machine Translation (NMT). If you don't explicitly specify a model to use, the service will pick one.
See the API release notes for on-going changes and new models.
Both client classes allow models to be specified, but in different ways. In each case, the model can be specified
when creating the client, or on a per-request basis. The difference is that TranslationClient
uses the TranslationModel
enum, making it simple to specify commonly-used models, whereas AdvancedTranslationClient
just uses strings for the models.
For example, using a TranslationClient
-wide model:
TranslationClient client = TranslationClient.Create(model: TranslationModel.Base);
TranslationResult result = client.TranslateText("It is raining.", LanguageCodes.French);
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");
TranslationClient
specifying a model for a single operation:
TranslationClient client = TranslationClient.Create();
TranslationResult result = client.TranslateText("It is raining.", LanguageCodes.French,
model: TranslationModel.Base);
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");
Using an AdvancedTranslationClient
-wide model:
AdvancedTranslationClient client = AdvancedTranslationClient.Create(model: "base");
TranslationResult result = client.TranslateText("It is raining.", LanguageCodes.French);
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");
AdvancedTranslationClient
specifying a model for a single operation:
AdvancedTranslationClient client = AdvancedTranslationClient.Create();
TranslationResult result = client.TranslateText("It is raining.", LanguageCodes.French, model: "base");
Console.WriteLine($"Result: {result.TranslatedText}; detected language {result.DetectedSourceLanguage}");